首页 > 解决方案 > 使用减少 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
])

有没有办法告诉CIContext1x1CIImage转换成CIColor

标签: metalcore-imagecifilterciimage

解决方案


如果您想CIAreaAverage自定义 CIFilter中使用结果(即您不需要它作为CIColor参数),您可以直接将其作为 a 传递给该过滤器并通过内核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


推荐阅读