在iOS上使用核心图像二值化图片

问题描述 投票:0回答:2

我想知道是否可以使用Core Image二值化图像(仅转换为黑白)?

我使用OpenCV和GPUImage制作它,但如果可能的话,我更喜欢使用Apple Core Image

swift core-image
2个回答
3
投票

是。您至少有两个选项,CIPhotoEffectMono或一个小的自定义CIColorKernel。

CIPhotoEffectMono:

func createMonoImage(image:UIImage) -> UIImage {
    let filter = CIFilter(name: "CIPhotoEffectMono")
    filter!.setValue(CIImage(image: image), forKey: "inputImage")
    let outputImage = filter!.outputImage
    let cgimg = ciCtx.createCGImage(outputImage!, from: (outputImage?.extent)!)
    return UIImage(cgImage: cgimg!)
}

请注意,我写的很快,你可能需要收紧零回报。

CIColorKernel:

FadeToBW GLSL(0.0因子全彩色,1.0因子没有颜色):

kernel vec4 fadeToBW(__sample s, float factor) {
    vec3 lum = vec3(0.299,0.587,0.114);
    vec3 bw = vec3(dot(s.rgb,lum));
    vec3 pixel = s.rgb + (bw - s.rgb) * factor;
    return vec4(pixel,s.a);
}

下面的代码将此作为名为FadeToBW.cikernel的文件打开。您也可以将此字符串直接发布到openKernelFile调用中。

Swift代码:

func createMonoImage(image:UIImage, inputColorFade:NSNumber) -> UIImage {
    let ciKernel = CIColorKernel(string: openKernelFile("FadeToBW"))
    let extent = image.extent
    let arguments = [image, inputColorFade]        
    let outputImage = ciKernel.applyWithExtent(extent, arguments: arguments)
    let cgimg = ciCtx.createCGImage(outputImage!, from: (outputImage?.extent)!)
    return UIImage(cgImage: cgimg!)
}

再次,添加一些警卫等。


1
投票

您可以使用MetalPerformanceShaders。和CIImageProcessingKernel。 https://developer.apple.com/documentation/coreimage/ciimageprocessorkernel

这是所需类的代码。

 class ThresholdImageProcessorKernel: CIImageProcessorKernel {
    static let device = MTLCreateSystemDefaultDevice()
    override class func process(with inputs: [CIImageProcessorInput]?, arguments: [String : Any]?, output: CIImageProcessorOutput) throws {
        guard
            let device = device,
            let commandBuffer = output.metalCommandBuffer,
            let input = inputs?.first,
            let sourceTexture = input.metalTexture,
            let destinationTexture = output.metalTexture,
            let thresholdValue = arguments?["thresholdValue"] as? Float else  {
                return
        }

        let threshold = MPSImageThresholdBinary(
            device: device,
            thresholdValue: thresholdValue,
            maximumValue: 1.0,
            linearGrayColorTransform: nil)

        threshold.encode(
            commandBuffer: commandBuffer,
            sourceTexture: sourceTexture,
            destinationTexture: destinationTexture)
    }
}

这就是你如何使用它:

    let context = CIContext(options: nil)

            if let binaryCIImage = try? ThresholdImageProcessorKernel.apply(
                withExtent: croppedCIImage.extent,
                inputs: [croppedCIImage],
                arguments: ["thresholdValue": Float(0.2)]) {
                if let cgImage = context.createCGImage(binaryCIImage, from: binary.extent) {
                    DispatchQueue.main.async {
                        let resultingImage = UIImage(cgImage: cgImage)
                        if resultingImage.size.width > 100 {
                            print("Received an image \(resultingImage.size)")
                        }
                    }
                }
            }
© www.soinside.com 2019 - 2024. All rights reserved.