微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用还原 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。