使用还原 CIFilter 的输出作为另一个过滤器的颜色输入

问题描述

我是 CoreImage / Metal 的新手,所以如果我的问题很幼稚,请提前道歉。我花了一周时间浏览 CoreImage 文档和示例,但我无法弄清楚这一点。

假设我有一个像 CIAreaAverage 这样的缩减过滤器,它输出一个 1x1 的图像。是否可以将该图像转换为可以作为另一个 CIFilter 参数传递的颜色?我知道我可以通过将 CIAreaAverage 输出渲染到 CVPixelBuffer 中来做到这一点,但我试图在一次渲染过程中做到这一点。

编辑 #1(澄清):

假设我想通过允许用户从图像中采样一个灰色像素来校正白平衡:

let pixelImage = inputImage.applyingFilter("CICrop",arguments: [
  "inputRectangle": CGRect(origin: pixelCoordinates,size: CGSize(width: 1,height: 1)) 
])

// We know now that the extent of pixelImage is 1x1. 
// Do something to convert the image into a pixel to be passed as an argument in the filter below.
let pixelColor = ???

let outputImage = inputImage.applyingFilter("CIWhitePointAdjust",arguments: [
  "inputColor": pixelColor
])

有没有办法告诉 CIContext 将 1x1 CIImage 转换为 CIColor

解决方法

如果您想在 custom CIAreaAverage 中使用 CIFilter 的结果(即您不需要它作为 CIColor 参数),您可以直接将其作为 CIImage 传递给该过滤器并通过内核中的 sampler 读取值:

extern "C" float4 myKernel(sampler someOtherInput,sampler average) {
    float4 avg = average.sample(float2(0.5,0.5)); // average only contains one pixel,so always sample that
    // ...
}

您还可以在将 .clampedToExtent() 传递给另一个过滤器/内核之前平均调用 CIImage。这将导致 Core Image 将平均图像视为无限大,到处都包含相同的值。那么在哪个坐标处对值进行采样并不重要。如果您想在自定义 CIColorKernel 中使用平均值,这可能很有用。

,

你可以做的不涉及 Metal 的事情是使用 CoreImage 本身。假设您想要 CIAreaAverage 输出的 640x640 图像,名为 ciPixel

let crop = CIFilter(name: "CICrop")
crop?.setValue(ciPixel,forKey: "inputImage")
crop?.setValue(CIVector(x: 0,y: 0,z: 640,w: 640),forKey: "inputRectangle")
ciOutput = crop?.outputImage

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...