title: Implementing Matlab's rgb2gray in Java
body: <p>I'm trying to implement Matlab's <code>rgb2gray</code> in Java according to <a href="http://www.mathworks.com/help/toolbox/images/ref/rgb2gray.html" rel="nofollow">http://www.mathworks.com/help/toolbox/images/ref/rgb2gray.html</a> . I have the following code:</p>
  
  <pre><code>public BufferedImage convert(BufferedImage bi){
      int heightLimit = bi.getHeight();
      int widthLimit = bi.getWidth();
      BufferedImage converted = new BufferedImage(widthLimit, heightLimit,
          BufferedImage.TYPE_BYTE_GRAY);
  
      for(int height = 0; height &lt; heightLimit; height++){
          for(int width = 0; width &lt; widthLimit; width++){
              // Remove the alpha component
              Color c = new Color(bi.getRGB(width, height) &amp; 0x00ffffff);
              // Normalize
              int newRed = (int) 0.2989f * c.getRed();
              int newGreen = (int) 0.5870f * c.getGreen();
              int newBlue = (int) 0.1140f * c.getBlue();
              int roOffset = newRed + newGreen + newBlue;
              converted.setRGB(width, height, roOffset);
          }
      }
  
      return converted;
  }
  </code></pre>
  
  <p>Now, I do get a grayscale image but it is too dark compared to what I get from Matlab. AFAIK, the easiest way to turn an image to grayscale is have a BufferedImage of type TYPE_BYTE_GRAY and then just copy the pixels of a BufferedImage of TYPE_INT_(A)RGB. But even this method gives an image that is darker than Matlab's though grayscale decently enough. I've also looked into using <code>RescaleOp</code>. However, I don't find anyway in RescaleOp to set the grayness per pixel.</p>
  
  <p>As an added test, I print out the image matrices produced by Java nad by Matlab. In Java, I get figures like 6316128 6250335 6118749 6118749 6250335 6447714 while in Matlab, I only get something like 116 117 119 120 119 115 (first six figures of both matrices).</p>
  
  <p>How do I get an output similar to Matlab's?</p>