for(int x = 0; x < w; x++)
        {
            for(int y = 0; y < h; y++)
            {
              int rgb = bimg.getRGB(x,y);
              //byte r = (byte) ((rgb & 0x00ff0000) >> 16);
              //byte g = (byte) ((rgb & 0x0000ff00) >> 8);
              //byte b = (byte) (rgb & 0x000000ff);
              pixels[x][y] = ((int) rgb) & 0xff;
            }
        }
      
     
        
        processImage();
        for(int x = 0; x < w; x++)
        {
            for(int y = 0; y < h; y++)
            {
              int rgb = (pixels[x][y] | 0xff);
              bimg.setRGB(x, y, rgb);
              //byte r = (byte) ((rgb & 0x00ff0000) >> 16);
              //byte g = (byte) ((rgb & 0x0000ff00) >> 8);
              //byte b = (byte) (rgb & 0x000000ff);
            }
        }

I am learning how to process image in java. I want to know if the codes above are correct in processing grayscale jpeg image?

Simply create a BufferedImage of the right size of type "BYTE_GRAY" and draw the colored image into it by retreiving the Graphics from the new BufferedImage and using drawImage with the coloredImage as the argument.

//Retrieve Image
        BufferedImage bimg = ImageIO.read( new File( "c:\\lena512.jpg" ) );
        bimg = bimg.getSubimage(0, 0, 512, 512);

        //Access it's pixel
        int w =  bimg.getWidth(null);
        int h =  bimg.getHeight(null);
        pixels = new int[w][h];
        for(int x = 0; x < w; x++)
        {
            for(int y = 0; y < h; y++)
            {
              int rgb = bimg.getRGB(x,y);
              byte r = (byte) ((rgb & 0x00ff0000) >> 16);
              byte g = (byte) ((rgb & 0x0000ff00) >> 8);
              byte b = (byte) (rgb & 0x000000ff);
              pixels[x][y] = (int)Math.round( 0.299 * r + 0.587 * g + 0.114 * b);
            }
        }
      
     
        
        processImage();

        for(int x = 0; x < w; x++)
        {
            for(int y = 0; y < h; y++)
            {
              int rgb = (pixels[x][y]);
              bimg.setRGB(x, y, rgb);
              //byte r = (byte) ((rgb & 0x00ff0000) >> 16);
              //byte g = (byte) ((rgb & 0x0000ff00) >> 8);
              //byte b = (byte) (rgb & 0x000000ff);
            }
        }
            //Draw back
            // Save as JPEG
            File file = new File("c:\\newimage.jpg");
            ImageIO.write(bimg, "jpg", file);       
    }

I am so sorry sir, but I can't get you. This is my full codes. The initial image is gray scale and I want to output back to newimage.jpg.

Edited 6 Years Ago by takercena: n/a

See the second part here.

P.S. Did you even try looking at the API docs, or finding tutorials for the classes listed in my post? No, you didn't, did you?

Edit: Also, you find the above tutorial with a simple Google search for Java grayscale as a link within the first result.

Edited 6 Years Ago by masijade: n/a

to convert an image (any type)to grey scale you can use the function :
ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
colorConvert.filter(image, image);

This article has been dead for over six months. Start a new discussion instead.