Reputation: 221
Im trying to colourise some Landsat images, when looking online for image fusion algorithms I found one of specific interest which I am trying to implement. I have 3 algorithms already implemented and they are fine but I cant seem to get this one to work.
I have the following code in place:
int red1 = ((redImage.getRGB(x, y)>>> 16) & 0xFF);
int green1 = ((greenImage.getRGB(x, y)>>> 8) & 0xFF);
int blue1 = ((blueImage.getRGB(x, y)) & 0xFF);
Which retrieves the red, green and blue values of the red, green and blue images respectively. I also have the following code:
int pan1 = ((panImage.getRGB(x, y)>>> 16) & 0xFF);
int pan2 = ((panImage.getRGB(x, y)>>> 8) & 0xFF);
int pan3 = ((panImage.getRGB(x, y)) & 0xFF);
This code retrieves the red, green and blue values of the single panchromatic image. This is fairly irrelevant to the problem but its worth outlining just in case.
The algorithm I have been looking at is fairly simple, it says this algorithm is a simple multiplication of each multispectral band with the panchromatic image. Sounded easy enough so I proceeded to do the following:
int red = red1 * pan1;
int green = green1 * pan2;
int blue = blue1 * pan3;
int rgb = ((red << 16) | (green << 8) | blue);
colouredImage.setRGB(x, y, (rgb | 0xFF000000));
The resulting image looks as if its suffered from some serious noise, i.e. theres purple and green speckles everywhere which makes the image pretty useless. I dont know how to attach images but its essentially random colours everywhere which doesnt make sense to me.
So im thinking theres something wrong with the arithmetic of the colours where I multiply them together, is something happening with the alpha channel which is causing these random colours?
Any help is massively appreciated.
Upvotes: 3
Views: 1069
Reputation: 22342
This looks like a simple overflow problem. When you multiply your red1 * pan1
, it's going over the allowed 0 ... 255
range.
There are a couple of ways to avoid this:
[0...1]
range before multiplicationYou can convert your color values to a floating point type between 0.0 ... 1.0
. This way the results of multiplication stay within the correct range. Just remember to convert the result back to 0 ... 255
range when you're done.
Or
This is the cleaner method in my opinion. Take the result and simply divide it by 255. You can use integer division, and there's no need for the float conversion. Sorry for not mentioning this in my original answer, my brain wasn't working correctly.
int red = (red1 * pan1) / 255;
// same for other colors
Upvotes: 3