【问题标题】:CIFilter convolution skews CIImage dimensions into infinityCIFilter 卷积将 CIImage 维度倾斜到无穷大
【发布时间】:2020-10-02 16:26:32
【问题描述】:

将卷积核应用于输入图像应生成具有完全相同尺寸的输出图像。然而,当在CIImage 上使用具有非零偏差的CIFilter.convolution3x3 时,检查输出会发现宽度、高度和原点坐标已经倾斜到无穷大,特别是CGFloat.greatestFiniteMagnitude。我试过这个过滤器的 5x5 和 7x7 版本,我尝试设置不同的权重和偏差,结论是一样的——如果偏差不是零,输出图像的大小和原点坐标似乎被破坏了。

这个过滤器的文档是here

这是一些代码...

// create the filter
let convolutionFilter = CIFilter.convolution3X3()
convolutionFilter.bias = 1 // any non zero bias will do

// I'll skip setting convolutionFilter.weights because the filter's default weights (an identity matrix) should be fine

// make your CIImage input
let input = CIImage(...) // I'm making mine from data I got from the camera

// lets print the size and position so we can compare it with the output
print(input.extent.width, input.extent.height, input.extent.origin) // -> 960.0 540.0 (0.0, 0.0)

// pass the input through the filter
convolutionFilter.inputImage = input
guard let output = convolutionFilter.outputImage else {
    print("the filter failed for some reason")
}

// the output image now contains the instructions necessary to perform the convolution,
// but no processing has actually occurred; even so, the extent property will have
// been updated if a change in size or position was described

// examine the output's size (it's just another CIImage - virtual, not real)
print(output.extent.width, output.extent.height, output.extent.origin) // -> 1.7976931348623157e+308 1.7976931348623157e+308 (-8.988465674311579e+307, -8.988465674311579e+307)

注意1.7976931348623157e+308CGFloat.greatestFiniteMagnitude

这不应该发生。我可以提供的唯一其他信息是我正在 iOS 13.5 上运行此代码,并且我正在过滤的 CIImages 是从 CVPixelBuffers 中实例化的,这些 CVPixelBuffers 是从 CMSampleBuffers 中获取的,这些信息由设备的摄像头馈送自动传送到我的代码中。通过过滤器之前的宽度和高度为 960x540。

【问题讨论】:

  • 对我来说,这听起来像是正确的行为,如果你想要硬边,你应该剪回原来的范围。
  • 另请参阅此处对clamp-filter-crop 的讨论:stackoverflow.com/questions/12839729/…
  • @matt 但只要将偏差设置为零,我们就不会出现这种行为;程度保持不变。另请注意,现存的起源严重倾斜。如果您渲染到另一个像素缓冲区,事情将非常错位。为了正确使用它而不得不用笨拙的裁剪来跟进你的卷积似乎是错误的。

标签: ios swift convolution core-image cifilter


【解决方案1】:

虽然它似乎没有在任何地方记录,但这似乎是@matt 建议的正常行为,尽管我不知道为什么bias 是决定因素。一般来说,我怀疑这与 CIFilter 的卷积在处理边缘像素时必须图像的初始边界之外运行这一事实有关。内核与边缘及其外部的未定义区域重叠,将其视为虚拟 RGBA(0,0,0,0) 像素的无限空间。

将extent更改为无穷大后,原始图像像素本身仍然在其原始原点和宽度/高度,因此您可以轻松地将它们渲染到具有相同原点和宽度/高度的目标像素缓冲区;您用于此渲染的CIContext 将忽略那些超出目标像素缓冲区范围的“虚拟”像素。

请记住,由于与相邻的虚拟 RGBA(0,0,0,0) 像素的交互,卷积可能会在图像边缘产生意想不到的效果,从而使您认为渲染出错或错位的东西。通常,如果您在应用卷积之前使用 CIImage 的 clampedToExtent() 方法,则可以避免此类问题。

【讨论】:

    猜你喜欢
    • 2016-12-13
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2021-12-15
    • 2013-11-12
    • 2017-01-06
    相关资源
    最近更新 更多