KuwaharaFilter

专注于在 Swift 中为 Apple 平台实现 KuwaharaFilter 的项目

功能

当前的功能列表是

图像示例

建议放大查看!

- 照片由 Nejc Košir 拍摄于 Pexels


- 照片由 Josh Hild 拍摄于 Pexels

如何使用

要注册滤镜以像其他 CI 一样使用,请执行以下操作

    //AppDelegate
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        DispatchQueue.global().async {
            FilterRegister.registerFilters()
        }
        return true
    }

如果您不想或不能,可以使用

    let filter = Kuwara()
    kuwahara.setValue(image, forKey: "inputImage")
    let img = kuwahara?.outputImage //also optional remember to guard or don't I'm dev not a cop
    

全部通过 CIFilters 完成,这意味着您可以在任何平台上使用

macOS

import SwiftDithering

function yourFunction() {
//Highly Recommended
    DispatchQueue.global().async{
        guard let data: Data =  NSImage(named:"testImage").tiffRepresentation,
        let bit = NSBitmapImageRep(data: data),
        let cImage: CIImage = CIImage(bitmapImageRep: bit),
        let filter = CIFilter(name: "Kuwahara", parameters: ["inputImage": image])
         else {
        // handle this case
        }
        //can also add values like this
        filter.setValue(value, forKey: "keyHere")
        ...
        guard let out = filter.outputImage else {
            //handle this
        }
        let rep = NSCIImageRep(ciImage: out)
        let img = NSImage(size: rep.size)
        img.addRepresentation(rep)
    }
  
}

iOS

import SwiftDithering

function yourFunction() {
//Highly Recommended
    DispatchQueue.global().async{
        guard let image: UIImage =  UIImage(named:"testImage") else {
        // handle this case
        }
        var ciImage:CIImage
        if image.ciImage == nil {
            guard let cg = image.cgImage else{
                //handle
            }
            ciImage = CIImage(cgImage: image.cgImage)
        } else{
            ciImage = image.ciImage!
        }
        
        guard let filter = CIFilter(name: "Kuwahara", parameters: ["inputImage": ciImage]) else {
            //handle this
        }
        //can also add values like this
        filter.setValue(value, forKey: "keyHere")
        ...
        guard let out = filter.outputImage,
            let cg = CIContext().createCGImage(out, from: out.extent) else {
            //handle this
        }
        
        let result = UIImage(cgImage: cg)
    }
}

软件包中还有一个示例应用程序(目前我计划稍后移动它),展示了一个基本实现。

感谢

这个项目得益于 acerola 的精彩视频和实现,以及我在网上找到的其他资源。

其他

任何附加的图像都不是我创作的,我尝试从 Pexels 获取所有内容并感谢所有人,如果我忘记这样做,请在此处的 git 上创建一个 issue,虽然根据 Pexels 规则和许可这不是强制性的,但这对于这些人创作的惊人艺术来说是我至少可以做的。