I'm trying to tackle a problem where an image is read. I need to exclude the Blue channel in the pixel so the it will result in an RG image.
This is what I have so far and not working D;
Would I need to create a new BufferedImage and apply each averaged pixel (Without the blue channel) to it? I'm not sure how to do it though.
For starters, change your second-to-last line of code to this and see if it helps:
image.setRGB(i, j, (redData[i][j] << 16) | (greenData[i][j] << 8));
But with this, you will end up with an image that is the same size as the original, except that you drop the blue channel and you average over the other channels (sort-of like a blur), but only in the top left corner of each 2x2-block.
If you want to actually create an image of a quarter size, you do indeed need to create a new BufferedImage
with half the width and height and then change the above mentioned line to:
newImage.setRGB(i/2, j/2, (redData[i][j] << 16) | (greenData[i][j] << 8));
Here's an updated version of your code that should do it (untested):
public static BufferedImage isolateBlueChannelAndResize(BufferedImage image) {
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
// Round down for odd-sized images to prevent indexing outside image
if ((imageWidth & 1) > 0) imageWidth --;
if ((imageHeight & 1) > 0) imageHeight--;
BufferedImage newImage = new BufferedImage(imageWidth/2, imageHeight/2, image.getType());
for (int i = 0; i < imageWidth; i += 2) {
for (int j = 0; j < imageHeight; j += 2) {
int r1 = (image.getRGB(i , j ) >> 16) & 0xff;
int r2 = (image.getRGB(i+1, j ) >> 16) & 0xff;
int r3 = (image.getRGB(i , j+1) >> 16) & 0xff;
int r4 = (image.getRGB(i+1, j+1) >> 16) & 0xff;
int red = (r1 + r2 + r3 + r4 + 3) / 4; // +3 rounds up (equivalent to ceil())
int g1 = (image.getRGB(i , j ) >> 8) & 0xff;
int g2 = (image.getRGB(i+1, j ) >> 8) & 0xff;
int g3 = (image.getRGB(i , j+1) >> 8) & 0xff;
int g4 = (image.getRGB(i+1, j+1) >> 8) & 0xff;
int green = (g1 + g2 + g3 + g4 + 3) / 4; // +3 rounds up (equivalent to ceil())
newImage.setRGB(i/2, j/2, (red << 16) | (green << 8));
}
}
return newImage;
}