0

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.

BK Prajapati
  • 38
  • 2
  • 7
  • you should be inform that firefox and chrome does not render png with the same colors and it can come from the java rendering engine too. – Kiwy Nov 27 '13 at 14:25
  • @Kiwy I am using javax.swing as my project is a stand alone project. – BK Prajapati Nov 27 '13 at 15:15
  • See also http://stackoverflow.com/questions/3106269/how-to-use-type-byte-gray-to-efficiently-create-a-grayscale-bufferedimage-using – halfbit Nov 27 '13 at 15:21

1 Answers1

6

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, i.e. 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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks for your response. Now I am getting original image back. And sorry I can't vote you up because my reputation doesn't allow me... – BK Prajapati Nov 27 '13 at 15:19