自适应阈值CIKernel/CIFilter iOS [英] Adaptive Threshold CIKernel/CIFilter iOS
问题描述
我已经进行了全面研究,以找到可以在iOS上执行自适应阈值处理的内核.不幸的是,我不了解内核语言或其背后的逻辑.在下面,我发现了一个执行阈值设置的例程( https://gist.github.com/xhruso00/a3f8a9c8ae7e33b8b23d )
I have researched all over in order to find a kernel that performs adaptive thresholding on iOS. Unfortunately I do not understand the kernel language or the logic behind it. Below, I have found a routine that performs thresholding (https://gist.github.com/xhruso00/a3f8a9c8ae7e33b8b23d)
static NSString * const kKernelSource = @"kernel vec4 thresholdKernel(sampler image)\n"
"{\n"
" float inputThreshold = 0.05;\n"
" float pass = 1.0;\n"
" float fail = 0.0;\n"
" const vec4 vec_Y = vec4( 0.299, 0.587, 0.114, 0.0 );\n"
" vec4 src = unpremultiply( sample(image, samplerCoord(image)) );\n"
" float Y = dot( src, vec_Y );\n"
" src.rgb = vec3( compare( Y - inputThreshold, fail, pass));\n"
" return premultiply(src);\n"
"}";
是否可以将其重写为自适应阈值内核?我提供给它的图像已变成黑白,并且已经模糊.您有什么资源可以指向我吗?我想坚持使用CoreImage,因为我的整个堆栈都是围绕它构建的.
Is it possible to rewrite this into an adaptive thresholding kernel? The image I am supplying to it has been turned into B&W and has already been blurred. Are there any resources you could point me to? I would like to stick with CoreImage as my whole stack is built around it.
我试图实现的最佳示例/参考已在GPUImage的GPUImageAdaptiveThresholdFilter中实现-
The best example / reference from what I am trying to achieve has been implemented in GPUImage's GPUImageAdaptiveThresholdFilter - https://github.com/BradLarson/GPUImage/blob/c5f0914152419437869c35e29858773b1a06083c/framework/Source/GPUImageAdaptiveThresholdFilter.m
推荐答案
西蒙滤镜是实现所需效果的正确方法,但是,您需要做一些修改.
Simon's Filter is the right approach to achieve the desired effect, however, you have to modify a couple of things.
首先,切换imageLuma
和thresholdLuma
的顺序,因为我们希望黑色字母保持黑色而不是相反.另外,您应该添加一个常数(我选择了0.01
)以消除噪声.
First of all, switch the order of imageLuma
and thresholdLuma
, since we want black letters to remain black and not the other way around. Also, you should add a constant (I chose 0.01
) to remove noise.
var thresholdKernel = CIColorKernel(string:
"kernel vec4 thresholdFilter(__sample image, __sample threshold)" +
"{" +
" float imageLuma = dot(image.rgb, vec3(0.2126, 0.7152, 0.0722));" +
" float thresholdLuma = dot(threshold.rgb, vec3(0.2126, 0.7152, 0.0722));" +
" return vec4(vec3(step(thresholdLuma, imageLuma+0.001)), 1);"
"}"
override var outputImage: CIImage! {
guard let inputImage = inputImage,
let thresholdKernel = thresholdKernel else {
return nil
}
let blurred = inputImage.applyingFilter("CIBoxBlur", withInputParameters: [kCIInputRadiusKey: 5]) // block size
let extent = inputImage.extent
let arguments = [inputImage, blurred]
return thresholdKernel.apply(withExtent: extent, arguments: arguments)
}
这就是您仅使用Apple的Core Image所获得的,而无需安装任何外部库:)
And this is, what you get Only using Apple's Core Image, without having to install any external libraries :)
当然,您可以使用常量和块大小的值来玩些游戏.
Of course, you can play around a little with the values of constant and block size.
这篇关于自适应阈值CIKernel/CIFilter iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!