用位移操作替换分支语句
我正在编写一个图像二值化算法,它只需将每个像素的亮度值(灰度图像)转换为黑色或白色。目前,对每个像素进行二值化的算法大致是这样的
if( grayscale[x] < thresholdValue)
{
bitonal[x] = 1;
}
(这实际上是 ACTUAL 算法的简化,因为双色调图像实际上是位打包图像(每个数组索引保存 8 个像素),所以我实际上将当前数组索引内的 1 位打包。 .但我不认为这改变了我的问题。
我想做的是消除对 if 语句的需要,
按照这个思路做一些事情。然后执行一些位操作技巧来清除或移动位,这样如果 (grayscale[x]-threshold) 的结果小于 0,我会得到 0。否则我会得到 1
如果用相反的方法更容易(如果灰度[x]-阈值<0 +按位欺骗得到1,否则得到0)也可以...只要因为我可以摆脱分支语句...任何帮助表示赞赏..
I'm writing an image binarization algorithm which simply converts each pixel's luminance value (grayscale image) to a black or white. Currently the algorithm for binarizing each pixel is roughly this
if( grayscale[x] < thresholdValue)
{
bitonal[x] = 1;
}
(this is actually a simplification of the ACTUAL algorithm because the bitonal image is actually a bitpacked image (each array index holds 8 pixels) so I actually bitpack the 1 inside the current array index...but I don't think that changes my question.
What I'm attempting to do is to remove the need for the if-statement.
What I was thinking was to do something along the lines of this. Subtract the thresholdValue by the grayscale and then perform some bit manipulation trickery to clear out or shift bits such that if the result of (grayscale[x]-threshold) is less than 0, I get a 0. otherwise I would get a 1
. If it's easier to do it the other way around (if grayscale[x]-threshold < 0 + bitwise trickery get a 1, else get a 0)
that would also work... as long as I can get rid of the branch statements...any help appreciated..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
bitonal[x] = (灰度[x] <阈值);
bitonal[x] = (grayscale[x] < thresholdValue);
如果亮度是一个 8 位值,那么您可以拥有一个包含 0 或 1 的 256 个元素的数组(数组的第一个 threshold 元素包含
1
,并且其余元素包含0
。If luminance is an 8-bit value, then you can have an array of 256 elements which contain either 0 or 1 (the first threshold elements of the array contain
1
, and the remaining elements contain0
.我刚刚在我的嵌入式应用程序中寻找类似的时间关键循环的东西。
我发现的一件有趣的事情是,这样的代码
仍然在我的平台(TI F2812)上生成分支指令。编译器似乎没有直接的方法将状态标志从比较移动到寄存器中,因此它会生成类似的内容
但是,处理器确实有内置的最大和最小运算符。由于分支的开销相当大,因此这段代码实际上运行得更快:
只有当 a
max
的结果为非零时,max
才会为非零。 b.然后min
会将任何非零值转换为 1。这是特定于处理器的,可能根本不适用于 X86。
I've just been looking at similar stuff for a time-critical loop in my embedded app.
One interesting thing I found is that code like this
still generates a branch instruction on my platform (TI F2812). It seems the compiler has no direct way to move the status flag from a comparison into a register, so instead it generates something like
However, the processor does have built-in max and min operators. Since branching is quite expensive, this code actually runs faster:
The result of
max
will only be non-zero if a < b. And then themin
will convert any non-zero to 1.This is very processor specific, and may not apply at all on an X86.
您使用什么语言?该语言有最小/最大函数吗? (例如,C++ 和 Java 都这样做)如果语言这样做,这将是消除 if...eg Min(grayscale[x]
What language are you working with? Does the language have min/max functions? (C++ and Java for example both do) If the language does, that would be one way to elminate the if...eg Min(grayscale[x] < thresholdValue, 0)
也许:
假设您的语言不将布尔值和整数值等同(如 C 和 C++ 所做的那样)。
Perhaps:
Assuming your language doesn't equate boolean and integer values (as C and C++ do).