我正在研究一种算法,该算法将 8 位灰度图像执行全局阈值化为 1 位(位压缩,这样 1 个字节包含 8 个像素)单色图像。灰度图像中的每个像素都可以具有 0 - 255 的亮度值。
我的环境是 Microsoft Visual Studio C++ 中的 Win32。
出于好奇,我有兴趣尽可能优化算法,1 位图像将变成 TIFF。目前我将 FillOrder 设置为 MSB2LSB(最高有效位到最低有效位)只是因为 TIFF 规范表明了这一点(它不一定需要是 MSB2LSB)
为那些不知道的人提供一些背景知识:
MSB2LSB 在一个字节中从左到右对像素进行排序,就像像素在图像中随着 X 坐标增加而定向一样。如果您在 X 轴上从左到右遍历灰度图像,这显然需要您在将位打包到当前字节中时考虑“向后”。话虽如此,让我向您展示一下我目前拥有的东西(这是在 C 中,我还没有尝试过 ASM 或编译器内部函数只是因为我对它的经验很少,但这是有可能的)。
因为单色图像每字节有 8 个像素,所以单色图像的宽度将为
(grayscaleWidth+7)/8;
FYI, I assume my largest image to be 6000 pixels wide:
First thing I do (before any image is processed) is
1) calculate a look up table of amounts I need to shift into a specific byte given an X coordinate from my grayscale image:
int _shift_lut[6000];
for( int x = 0 ; x < 6000; x++)
{
_shift_lut[x] = 7-(x%8);
}
使用这个查找表,我可以将单色位值打包到我正在处理的当前字节中,例如:
monochrome_pixel |= 1 << _shift_lut[ grayX ];
这最终会大大提高速度
monochrome_pixel |= 1 << _shift_lut[ 7-(x%8)];
我计算的第二个查找表是一个查找表,它告诉我给定灰度像素上的 X 像素的单色像素的 X 索引。这个非常简单的 LUT 是这样计算的:
int xOffsetLut[6000];
int element_size=8; //8 bits
for( int x = 0; x < 6000; x++)
{
xOffsetLut[x]=x/element_size;
}
这个 LUT 允许我做类似的事情
monochrome_image[ xOffsetLut[ GrayX ] ] = packed_byte; //packed byte contains 8 pixels
我的灰度图像是一个简单的 unsigned char*,我的单色图像也是;
这是我初始化单色图像的方式:
int bitPackedScanlineStride = (grayscaleWidth+7)/8;
int bitpackedLength=bitPackedScanlineStride * grayscaleHeight;
unsigned char * bitpack_image = new unsigned char[bitpackedLength];
memset(bitpack_image,0,bitpackedLength);
然后我这样调用我的二值化函数:
binarize(
gray_image.DataPtr(),
bitpack_image,
globalFormThreshold,
grayscaleWidth,
grayscaleHeight,
bitPackedScanlineStride,
bitpackedLength,
_shift_lut,
xOffsetLut);
这是我的 Binarize 函数(如您所见,我做了一些循环展开,这可能有帮助也可能没有帮助)。
void binarize( unsigned char grayImage[], unsigned char bitPackImage[], int threshold, int grayscaleWidth, int grayscaleHeight, int bitPackedScanlineStride, int bitpackedLength, int shiftLUT[], int xOffsetLUT[] )
{
int yoff;
int byoff;
unsigned char bitpackPel=0;
unsigned char pel1=0;
unsigned char pel2=0;
unsigned char pel3=0;
unsigned char pel4=0;
unsigned char pel5=0;
unsigned char pel6=0;
unsigned char pel7=0;
unsigned char pel8=0;
int checkX=grayscaleWidth;
int checkY=grayscaleHeight;
for ( int by = 0 ; by < checkY; by++)
{
yoff=by*grayscaleWidth;
byoff=by*bitPackedScanlineStride;
for( int bx = 0; bx < checkX; bx+=32)
{
bitpackPel = 0;
//pixel 1 in bitpack image
pel1=grayImage[yoff+bx];
pel2=grayImage[yoff+bx+1];
pel3=grayImage[yoff+bx+2];
pel4=grayImage[yoff+bx+3];
pel5=grayImage[yoff+bx+4];
pel6=grayImage[yoff+bx+5];
pel7=grayImage[yoff+bx+6];
pel8=grayImage[yoff+bx+7];
bitpackPel |= ( (pel1<=threshold) << shiftLUT[bx]);
bitpackPel |= ( (pel2<=threshold) << shiftLUT[bx+1] );
bitpackPel |= ( (pel3<=threshold) << shiftLUT[bx+2] );
bitpackPel |= ( (pel4<=threshold) << shiftLUT[bx+3] );
bitpackPel |= ( (pel5<=threshold) << shiftLUT[bx+4] );
bitpackPel |= ( (pel6<=threshold) << shiftLUT[bx+5] );
bitpackPel |= ( (pel7<=threshold) << shiftLUT[bx+6] );
bitpackPel |= ( (pel8<=threshold) << shiftLUT[bx+7] );
bitPackImage[byoff+(xOffsetLUT[bx])] = bitpackPel;
//pixel 2 in bitpack image
pel1=grayImage[yoff+bx+8];
pel2=grayImage[yoff+bx+9];
pel3=grayImage[yoff+bx+10];
pel4=grayImage[yoff+bx+11];
pel5=grayImage[yoff+bx+12];
pel6=grayImage[yoff+bx+13];
pel7=grayImage[yoff+bx+14];
pel8=grayImage[yoff+bx+15];
bitpackPel = 0;
bitpackPel |= ( (pel1<=threshold) << shiftLUT[bx+8] );
bitpackPel |= ( (pel2<=threshold) << shiftLUT[bx+9] );
bitpackPel |= ( (pel3<=threshold) << shiftLUT[bx+10] );
bitpackPel |= ( (pel4<=threshold) << shiftLUT[bx+11] );
bitpackPel |= ( (pel5<=threshold) << shiftLUT[bx+12] );
bitpackPel |= ( (pel6<=threshold) << shiftLUT[bx+13] );
bitpackPel |= ( (pel7<=threshold) << shiftLUT[bx+14] );
bitpackPel |= ( (pel8<=threshold) << shiftLUT[bx+15] );
bitPackImage[byoff+(xOffsetLUT[bx+8])] = bitpackPel;
//pixel 3 in bitpack image
pel1=grayImage[yoff+bx+16];
pel2=grayImage[yoff+bx+17];
pel3=grayImage[yoff+bx+18];
pel4=grayImage[yoff+bx+19];
pel5=grayImage[yoff+bx+20];
pel6=grayImage[yoff+bx+21];
pel7=grayImage[yoff+bx+22];
pel8=grayImage[yoff+bx+23];
bitpackPel = 0;
bitpackPel |= ( (pel1<=threshold) << shiftLUT[bx+16] );
bitpackPel |= ( (pel2<=threshold) << shiftLUT[bx+17] );
bitpackPel |= ( (pel3<=threshold) << shiftLUT[bx+18] );
bitpackPel |= ( (pel4<=threshold) << shiftLUT[bx+19] );
bitpackPel |= ( (pel5<=threshold) << shiftLUT[bx+20] );
bitpackPel |= ( (pel6<=threshold) << shiftLUT[bx+21] );
bitpackPel |= ( (pel7<=threshold) << shiftLUT[bx+22] );
bitpackPel |= ( (pel8<=threshold) << shiftLUT[bx+23] );
bitPackImage[byoff+(xOffsetLUT[bx+16])] = bitpackPel;
//pixel 4 in bitpack image
pel1=grayImage[yoff+bx+24];
pel2=grayImage[yoff+bx+25];
pel3=grayImage[yoff+bx+26];
pel4=grayImage[yoff+bx+27];
pel5=grayImage[yoff+bx+28];
pel6=grayImage[yoff+bx+29];
pel7=grayImage[yoff+bx+30];
pel8=grayImage[yoff+bx+31];
bitpackPel = 0;
bitpackPel |= ( (pel1<=threshold) << shiftLUT[bx+24] );
bitpackPel |= ( (pel2<=threshold) << shiftLUT[bx+25] );
bitpackPel |= ( (pel3<=threshold) << shiftLUT[bx+26] );
bitpackPel |= ( (pel4<=threshold) << shiftLUT[bx+27] );
bitpackPel |= ( (pel5<=threshold) << shiftLUT[bx+28] );
bitpackPel |= ( (pel6<=threshold) << shiftLUT[bx+29] );
bitpackPel |= ( (pel7<=threshold) << shiftLUT[bx+30] );
bitpackPel |= ( (pel8<=threshold) << shiftLUT[bx+31] );
bitPackImage[byoff+(xOffsetLUT[bx+24])] = bitpackPel;
}
}
}
我知道此算法可能会遗漏每行中的一些尾随像素,但请不要担心。
如您所见,对于每个单色字节,我处理了 8 个灰度像素。
你在哪里看到 pel8<=阈值 是一个巧妙的小技巧,可以解析为 0 或 1,并且比 if{} else{}
快得多对于 X 的每个增量,我将一个位打包成比前一个 X 更高阶的位
所以对于灰度图像中的第一组 8 个像素
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
这就是字节中的位的样子(显然每个编号位只是处理相应编号像素的阈值结果,但你明白了)
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
PHEW 应该是这样。随意玩一些漂亮的小技巧,从这个算法中榨取更多的汁液。
启用编译器优化后,在 core 2 duo 机器上处理大约 5000 x 2200 像素的图像时,此函数平均需要 16 毫秒。
编辑:
R.. 的建议是删除移位 LUT 并只使用实际上完全合乎逻辑的常量......我已经将每个像素的 OR'ing 修改为:
void binarize( unsigned char grayImage[], unsigned char bitPackImage[], int threshold, int grayscaleWidth, int grayscaleHeight, int bitPackedScanlineStride, int bitpackedLength, int shiftLUT[], int xOffsetLUT[] )
{
int yoff;
int byoff;
unsigned char bitpackPel=0;
unsigned char pel1=0;
unsigned char pel2=0;
unsigned char pel3=0;
unsigned char pel4=0;
unsigned char pel5=0;
unsigned char pel6=0;
unsigned char pel7=0;
unsigned char pel8=0;
int checkX=grayscaleWidth-32;
int checkY=grayscaleHeight;
for ( int by = 0 ; by < checkY; by++)
{
yoff=by*grayscaleWidth;
byoff=by*bitPackedScanlineStride;
for( int bx = 0; bx < checkX; bx+=32)
{
bitpackPel = 0;
//pixel 1 in bitpack image
pel1=grayImage[yoff+bx];
pel2=grayImage[yoff+bx+1];
pel3=grayImage[yoff+bx+2];
pel4=grayImage[yoff+bx+3];
pel5=grayImage[yoff+bx+4];
pel6=grayImage[yoff+bx+5];
pel7=grayImage[yoff+bx+6];
pel8=grayImage[yoff+bx+7];
/*bitpackPel |= ( (pel1<=threshold) << shiftLUT[bx]);
bitpackPel |= ( (pel2<=threshold) << shiftLUT[bx+1] );
bitpackPel |= ( (pel3<=threshold) << shiftLUT[bx+2] );
bitpackPel |= ( (pel4<=threshold) << shiftLUT[bx+3] );
bitpackPel |= ( (pel5<=threshold) << shiftLUT[bx+4] );
bitpackPel |= ( (pel6<=threshold) << shiftLUT[bx+5] );
bitpackPel |= ( (pel7<=threshold) << shiftLUT[bx+6] );
bitpackPel |= ( (pel8<=threshold) << shiftLUT[bx+7] );*/
bitpackPel |= ( (pel1<=threshold) << 7);
bitpackPel |= ( (pel2<=threshold) << 6 );
bitpackPel |= ( (pel3<=threshold) << 5 );
bitpackPel |= ( (pel4<=threshold) << 4 );
bitpackPel |= ( (pel5<=threshold) << 3 );
bitpackPel |= ( (pel6<=threshold) << 2 );
bitpackPel |= ( (pel7<=threshold) << 1 );
bitpackPel |= ( (pel8<=threshold) );
bitPackImage[byoff+(xOffsetLUT[bx])] = bitpackPel;
//pixel 2 in bitpack image
pel1=grayImage[yoff+bx+8];
pel2=grayImage[yoff+bx+9];
pel3=grayImage[yoff+bx+10];
pel4=grayImage[yoff+bx+11];
pel5=grayImage[yoff+bx+12];
pel6=grayImage[yoff+bx+13];
pel7=grayImage[yoff+bx+14];
pel8=grayImage[yoff+bx+15];
bitpackPel = 0;
/*bitpackPel |= ( (pel1<=threshold) << shiftLUT[bx+8] );
bitpackPel |= ( (pel2<=threshold) << shiftLUT[bx+9] );
bitpackPel |= ( (pel3<=threshold) << shiftLUT[bx+10] );
bitpackPel |= ( (pel4<=threshold) << shiftLUT[bx+11] );
bitpackPel |= ( (pel5<=threshold) << shiftLUT[bx+12] );
bitpackPel |= ( (pel6<=threshold) << shiftLUT[bx+13] );
bitpackPel |= ( (pel7<=threshold) << shiftLUT[bx+14] );
bitpackPel |= ( (pel8<=threshold) << shiftLUT[bx+15] );*/
bitpackPel |= ( (pel1<=threshold) << 7);
bitpackPel |= ( (pel2<=threshold) << 6 );
bitpackPel |= ( (pel3<=threshold) << 5 );
bitpackPel |= ( (pel4<=threshold) << 4 );
bitpackPel |= ( (pel5<=threshold) << 3 );
bitpackPel |= ( (pel6<=threshold) << 2 );
bitpackPel |= ( (pel7<=threshold) << 1 );
bitpackPel |= ( (pel8<=threshold) );
bitPackImage[byoff+(xOffsetLUT[bx+8])] = bitpackPel;
//pixel 3 in bitpack image
pel1=grayImage[yoff+bx+16];
pel2=grayImage[yoff+bx+17];
pel3=grayImage[yoff+bx+18];
pel4=grayImage[yoff+bx+19];
pel5=grayImage[yoff+bx+20];
pel6=grayImage[yoff+bx+21];
pel7=grayImage[yoff+bx+22];
pel8=grayImage[yoff+bx+23];
bitpackPel = 0;
/*bitpackPel |= ( (pel1<=threshold) << shiftLUT[bx+16] );
bitpackPel |= ( (pel2<=threshold) << shiftLUT[bx+17] );
bitpackPel |= ( (pel3<=threshold) << shiftLUT[bx+18] );
bitpackPel |= ( (pel4<=threshold) << shiftLUT[bx+19] );
bitpackPel |= ( (pel5<=threshold) << shiftLUT[bx+20] );
bitpackPel |= ( (pel6<=threshold) << shiftLUT[bx+21] );
bitpackPel |= ( (pel7<=threshold) << shiftLUT[bx+22] );
bitpackPel |= ( (pel8<=threshold) << shiftLUT[bx+23] );*/
bitpackPel |= ( (pel1<=threshold) << 7);
bitpackPel |= ( (pel2<=threshold) << 6 );
bitpackPel |= ( (pel3<=threshold) << 5 );
bitpackPel |= ( (pel4<=threshold) << 4 );
bitpackPel |= ( (pel5<=threshold) << 3 );
bitpackPel |= ( (pel6<=threshold) << 2 );
bitpackPel |= ( (pel7<=threshold) << 1 );
bitpackPel |= ( (pel8<=threshold) );
bitPackImage[byoff+(xOffsetLUT[bx+16])] = bitpackPel;
//pixel 4 in bitpack image
pel1=grayImage[yoff+bx+24];
pel2=grayImage[yoff+bx+25];
pel3=grayImage[yoff+bx+26];
pel4=grayImage[yoff+bx+27];
pel5=grayImage[yoff+bx+28];
pel6=grayImage[yoff+bx+29];
pel7=grayImage[yoff+bx+30];
pel8=grayImage[yoff+bx+31];
bitpackPel = 0;
/*bitpackPel |= ( (pel1<=threshold) << shiftLUT[bx+24] );
bitpackPel |= ( (pel2<=threshold) << shiftLUT[bx+25] );
bitpackPel |= ( (pel3<=threshold) << shiftLUT[bx+26] );
bitpackPel |= ( (pel4<=threshold) << shiftLUT[bx+27] );
bitpackPel |= ( (pel5<=threshold) << shiftLUT[bx+28] );
bitpackPel |= ( (pel6<=threshold) << shiftLUT[bx+29] );
bitpackPel |= ( (pel7<=threshold) << shiftLUT[bx+30] );
bitpackPel |= ( (pel8<=threshold) << shiftLUT[bx+31] );*/
bitpackPel |= ( (pel1<=threshold) << 7);
bitpackPel |= ( (pel2<=threshold) << 6 );
bitpackPel |= ( (pel3<=threshold) << 5 );
bitpackPel |= ( (pel4<=threshold) << 4 );
bitpackPel |= ( (pel5<=threshold) << 3 );
bitpackPel |= ( (pel6<=threshold) << 2 );
bitpackPel |= ( (pel7<=threshold) << 1 );
bitpackPel |= ( (pel8<=threshold) );
bitPackImage[byoff+(xOffsetLUT[bx+24])] = bitpackPel;
}
}
}
我现在正在使用 (GCC) 4.1.2 在 Intel Xeon 5670 上进行测试。在这些规范下,硬编码位移比使用我原来的 LUT 算法慢 4 毫秒。在 Xeon 和 GCC 中,LUT 算法平均耗时 8.61 ms,硬编码位移平均耗时 12.285 ms。
最佳答案
尝试这样的事情:
unsigned i, w8=w>>3, x;
for (i=0; i<w8; i++) {
x = thres-src[0]>>1&0x80;
x |= thres-src[1]>>2&0x40;
x |= thres-src[2]>>3&0x20;
x |= thres-src[3]>>4&0x10;
x |= thres-src[4]>>5&0x08;
x |= thres-src[5]>>6&0x04;
x |= thres-src[6]>>7&0x02;
x |= thres-src[7]>>8&0x01;
out[i] = x;
src += 8;
}
您可以计算出宽度行末尾的余数不是 8 的倍数的额外代码,或者您可以填充/对齐源以确保它是 8 的倍数。
关于c - 快速阈值和位打包算法(可能的改进?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3705320/