CoreImage CIColorClamp 调整为错误的值

CoreImage CIColorClamp adjusting to wrong values

我在图像上尝试 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 是不是在做别的事情 min/max?或者我不知道这里还有什么问题。

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

尝试以完全不进行任何颜色匹配的方式设置您的 CIContext

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

对于颜色钳位过滤器,这可能有效,因为它只是钳位值。不过,我不确定颜色矩阵滤镜是否需要某些特定颜色 space。

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