Re: Fast Image access for binarization



Hi,
If pixel value range is 0..255 than you can efficiently remove checks
by lookup-table. Assuming that image has type unsigned char*, than:

unsigned char *curpixel, *stop;
unsigned char bin_tbl[256];

for(i=0;i<256;i++)
if(i>threshold)
bin_tbl[i] = 1;
else
bin_tbl[i] = 0;

stop = image+width*height;
for(curpixel = image; curpixel<stop; curpixel++)
*curpixel = bin_tbl[*curpixel];

Since bin_tbl small it will be in CPU cache all the time during
processing. So, memory access time to bin_tbl is negligible. Also
bin_tbl initialization loop is fast. It doesn't add any processing
time.In the same time the main
bottleneck is improved - no comparisons in loop body.

.



Relevant Pages

  • Re: Use of unions
    ... that might not have the same data format, ... component last used before this assignment is the character array, ... If you use unsigned char ... treated as an intermediate byte array of type unsigned char. ...
    (comp.std.c)
  • Re: Help me understand this compiler warning.
    ... ANDing an unsigned char with an ... int /could/ produce a signed int - which, according to 6.5.7, could ... 6.3.1.8- rules for integer promotions - adds a bit of ... which is of type unsigned char. ...
    (comp.lang.c)
  • Re: Help me understand this compiler warning.
    ... | The left operand is uc, which is of type unsigned char. ... | type unsigned char from being promoted to type int. ...
    (comp.lang.c)
  • Re: [Lit.] Buffer overruns
    ... > value of which shall be representable as an unsigned char or shall ... > equal the value of the macro EOF. ... I had been going by Harbison and Steele's ... char be signed and also support a type unsigned char, ...
    (sci.crypt)