Sunday, November 03, 2013

Remember your BITS

So I guess I've stumbled upon rule number 1 of image processing.... REMEMBER YOUR BITS. I've been working on a project that utilizes some simple image processing.... and a number of times I've been bitten by the fact that I've forgotten my bits. Basically my project includes reading in a 16-bit JPEG2000 image using the Kakadu library, and then doing a simple histogram equalization, then spitting it out as a PNG. So while there are numerous libraries out there that can do this.... the project I'm on has some licensing issues that prevent me from going out and grabbing them.

Anyways... first off Kakadu's documentation is a bit frustratingly lacking.... or at least I thought it was.... I did what I thought I was suppose to do... and well it didn't seem to be working... so I brought in Boost's GIL package to develop some quick debug tools.... and when I wrote out the png.... well it looked completely screwy.... my first thought was that the image that I thought was 16 bit was in fact 8 bit.... no... I was just doing something dumb.... I was attempting to read in the image with Kakadu's 8 bit reader instead of the 16 bit read...BIT FAIL 1.

But that was not all.... then I remembered that I was reading in the image to a 2d array of doubles... well that needed to be translated to GILS pixel types.... BIT FAIL 2.... converted to 8bit instead of 16 bit for printing....

So then I got all that working and went on to my equalization routine.... well BIT FAIL 3... I scaled the image from 16bit to an intensity value needlessly causing my histogram normalization to go wonky.

Got that working.... my image still looked wonky... BIT FAIL 4.... the whole point of equalizing the image basically turns the image into an 8 bit image.... but I was scaling it up as if it was an intensity image when I wrote it out.... BIT FAIL 5.... disabled all use of intensity but still didn't work... well that was again because now I was attempting to write out an 8 bit image as a 16 bit image.... BIT FAIL BIT FAIL BIT FAIL 8..... well basically once I remembered that I needed to convert from 8-bit to 16-bit to properly print... it all worked....

The worst part is .... the project really doesn't need to output any images... the output was all for verification that the various steps were working.... so basically everything was working except that well.... my debug tools made me think I had implemented everything wrong.... sorry I can't post images of my dumb mistakes, but I don't know the distribution of the source data and don't want to inadvertently post something I shouldn't.... but lesson learned... with doing any kind of image processing.... remember your BITS. 

No comments: