3

是否存在可用作平滑阈值的现有过滤器?我的意思是像素值应该在两个值之间进行阈值处理,但中间值是插值的。

基本上,我正在尝试实现这一点:

https://www.filterforge.com/wiki/index.php/Thresholds#Interpolation_.28.22Blending.22.29

4

2 回答 2

9

您需要使用 Core Image Kernel Language 和CIColorKernel.

我将使用 asmoothstep并传入两个边缘值以及当前像素的亮度。CIKL 可能看起来像这样:

kernel vec4 color(__sample pixel, float inputEdgeO, float inputEdge1)
{
    float luma = dot(pixel.rgb, vec3(0.2126, 0.7152, 0.0722));
    float threshold = smoothstep(inputEdgeO, inputEdge1, luma);
    return vec4(threshold, threshold, threshold, 1.0);
}

CIFilter您可以使用以下代码将其包装成 a :

class SmoothThreshold: CIFilter
{
    var inputImage : CIImage?
    var inputEdgeO: CGFloat = 0.25
    var inputEdge1: CGFloat = 0.75

    var colorKernel = CIColorKernel(string:
        "kernel vec4 color(__sample pixel, float inputEdgeO, float inputEdge1)" +
        "{" +
        "    float luma = dot(pixel.rgb, vec3(0.2126, 0.7152, 0.0722));" +
        "    float threshold = smoothstep(inputEdgeO, inputEdge1, luma);" +
        "    return vec4(threshold, threshold, threshold, 1.0);" +
        "}"
    )

    override var outputImage: CIImage!
    {
        guard let inputImage = inputImage,
            colorKernel = colorKernel else
        {
            return nil
        }

        let extent = inputImage.extent
        let arguments = [inputImage,
                         inputEdgeO,
                         inputEdge1]

        return colorKernel.applyWithExtent(extent,
                                           arguments: arguments)
    }
}

请注意,您需要确保inputEdge0小于inputEdge1,否则smoothstep会有点疯狂。您可能也想要saturate它们,以确保它们在 0 和 1 之间。

看看CIFilter.registerFilterName哪个可以让您创建一个SmoothThreshold具有熟悉语法的实例,尽管这样可以正常工作:

let image = CIImage(image: UIImage(named: "monalisa.jpg")!)!

let filter = SmoothThreshold()
filter.inputImage = image

let final = filter.outputImage

干杯!

西蒙

于 2016-03-29T16:54:21.653 回答
1

这最终为我工作:

[CIColorKernel kernelWithString:
              @"kernel vec4 threshold(__sample s, float thresh) {\n"
                "float avg = (s.r + s.g + s.b) / 3.0;\n"
                "float val = smoothstep(0.0, thresh, avg);"
                "return vec4(val, val, val, 1.0);\n}"];
于 2016-03-29T16:30:42.197 回答