TIFF and LibTiff Mail List Archive

# 2005.05.25 16:29 "Re: [Tiff] Help me on convert from TIFF 24 bit or 8 bit grayscaletobilevel image", by Joris Van Damme

Tuan,

Thanks for your reply quickly. You're alright, but right now i have a problem on threshold algorithm. If you can, please help me. Below, this is the my threshold algorithm (convert 8 bit grayscale to one bit image), the result image is very bad.

I'm not a C coder, I'm sorry. From a quick skim of the code, you don't seem to be dithering, but posterizing. Which is fine, if that is what you want, otherwise it will result in something you might feel is 'very bad'.

Suppose this is a block of grayscale pixels:
127 127 127
127 127 127
127 127 127

Suppose furthermore that we're talking 8bit grayscale, values range from 0 to 255. Posterizing means that every pixel below a certain threshold should become white (or black), above a certain threshold it should become black (or white). Thus, you may end up with something like this:

0 0 0
0 0 0
0 0 0

That is fine if that's what you want. But if you feel that is 'very bad', chances are what you actually want is something along these lines:

0 255 0
255 0 255
0 255 0

That is dithering. Rounding the first pixel (say upper left), to 0, since it is below a certain threshold (say 200) you next take the error to the second pixel. The error is 127, so the second pixel takes on the value 254 instead of its original value. This gets rounded to 255, since it's more then 200. The error, thus, is -55. Taking that to the next pixel, this value becomes 127-55, etc.

Dithering in a straight line is not the smart thing to do, many times it will create artefacts that the human eye picks up. Thus, there's floyd-steinberg (essentially spreading the error over several neighbouring pixels instead of a single one), and hilbert (dithering not along a straight line, but a weird zigzagging all over so-called 'space-filling curve'), and a number of other options that do better then dithering along a straight line.

If this is not your problem, please be more descriptive. For instance the description 'very bad' forces me to merely guess what you really want to achieve.

Other then this, I cannot immediatelly spot a problem in your code (but like I said, I'm not a C coder). Except maybe that I'm wondering why you go hunting for a min and max... In 8bit grayscale, the min and max are 0 and 255 (or the other way around, depending on the PhotometricInterpretation). You might want to honour that, otherwise you force everything to be 'normalized' around mid-gray. For instance, it is natural for a grayscale picture of lightbulb in action to be quite bright, and chances are that there's no black at all in that picture. Nevertheless, you may not want to 'shift' it towards midgray, which is what you would do if you determine a min and max from the actual picture.

Joris Van Damme
info@awaresystems.be
http://www.awaresystems.be/