简体   繁体   中英

Write pixel data in a grayscale image in java

I am developing a project on image processing where I have to fill the digitized images of cracked paintings. I have to convert a color image to grayscale, performing some calculations on the 2D Array of the gray image and writing it back as gray image. The code for this is:

BufferedImage colorImage=ImageIO.read(new File(strImagePath));

            BufferedImage image = new BufferedImage(colorImage.getWidth(),colorImage.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
            Graphics g = image.getGraphics();
            g.drawImage(colorImage, 0, 0, null);
            g.dispose();

            ImageIO.write(image,"PNG",new File("Image.PNG"));

            BufferedImage imgOriginal=ImageIO.read(new File("Image.PNG"));

            int width=image.getWidth();
            int height=image.getHeight();

            BufferedImage im=new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);

            int arrOriginal[][]=new int[height][width];


            for(int i=0;i<height;i++)
                for(int j=0;j<width;j++)
                    arrOriginal[i][j]=imgOriginal.getRGB(j,i)& 0xFF;

            for(int i=0;i<height;i++)
                for(int j=0;j<width;j++)
                    im.setRGB(j,i,arrOriginal[i][j]);

            ImageIO.write(im,"PNG",new File("Image1.PNG"));

But the output image is very much darker, I am not getting the original image back (I have not done any changes yet).

I think there should be some changes in setRGB() statement but I don't know what.

To write image back, I have also tried:

`

BufferedImage im = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
 WritableRaster raster = im.getRaster();
 for(int i=0;i<height;i++)
     for(int j=0;j<width;j++)
         raster.setSample(j,i,0,arrOriginal[i][j]); 

`

But it also don't give me original image back.

Can anyone provide me the solution of this problem? Thanks in advance.

I don't know anything about Java-based image processing, but I do know quite a lot about image processing in general, so I will see if I can give you any ideas. Please don't shoot me if I am wrong - I am just suggesting an idea.

In a greyscale image, the red, green and blue values are all the same, ie Red=Green=Blue. So, when you call getRGB and do the AND with 0xff, you are probably getting the blue component only, but that is ok as the red and green are the same - because it's greyscale.

I suspect the problem is that when you write it back to create your new output image, you are only setting the blue component and not the red and green - which should still be the same. Try writing back

original pixel + (original pixel << 8 ) + (original pixel <<16)

so that you set not only the Blue, but also the Red and Green components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM