首页 > 解决方案 > CoreImage CIColorClamp 调整为错误值

问题描述

我正在图像上尝试 CIColorClamp,它应该只将像素保持在指定范围内(https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/ uid/TP30000136-SW42 ) ,但是当我在黑色像素上打印前后结果时,它给出的数字与我在参数中设置的数字不同。这是代码:

let ciImage = CIImage.init(cgImage: image.cgImage!)
let filter = CIFilter("CIColorClamp")!
filter.setDefaults()
filter.setValue(ciImage, forKey: kCIInputImageKey)
filter.setValue(CIVector(x: 0.8, y: 0.8, z: 0.8, w: 0), forKey: "inputMinComponents")
filter.setValue(CIVector(x: 1, y: 1, z: 1, w: 1), forKey: "inputMaxComponents")

let outputCIImage = filter.outputImage!
let context = CIContext.init()
let outputCGImage = context.createCGImage(outputCIImage, from: outputCIImage.extent)
let outputImage = UIImage(cgImage: outputCGImage, scale: image.scale, orientation: image.imageOrientation)

这应该在黑色像素 (0, 0, 0, 1) 上给出 (0.8, 0.8, 0.8, 1),但是当我打印它时,它会给出以下内容:

<CIColorClamp: inputImage=<CIImage: 0x28165d590 extent [0 0 800 160]>
inputMinComponents=[0.8 0.8 0.8 0] inputMaxComponents=[1 1 1 1]>
BEFORE 0.0, 0.0, 0.0, 1.0
AFTER 0.9058823529411765, 0.9058823529411765, 0.9058823529411765, 1.0

CoreImage 是否在做其他事情,然后只是最小/最大?或者我不知道这里还有什么问题。

标签: iosswiftimagecore-image

解决方案


估计是配色问题。Core Image 默认执行从输入到工作到输出颜色空间的颜色匹配。如果输入图像的颜色空间与输出颜色空间不同,您可能会得到不同的像素值。

CIContext尝试以根本不进行任何颜色匹配的方式设置您的:

let context = CIContext(options: [.workingColorSpace: NSNull(), .outputColorSpace: NSNull()])

对于颜色钳位过滤器,这可能会起作用,因为它只是钳位值。不过,我不确定颜色矩阵过滤器是否需要一些特定的颜色空间。

您还可以 outputCIImage在调试器中执行快速查看,以查看 Core Image 实际在图像上执行的操作图。


推荐阅读