2016-04-17 149 views
-4

我对后续代码的一些错误,我不知道为什么我在这里enter image description here方法彩色图像转换为灰度图像

// A method to convert color image to grayscale image 

public static BufferedImage toGrayScale(Image img) { 
I  // Convert image from type Image to BufferedImage 
    BufferedImage bufImg = convert(img); 

    // Scan through each row of the image 
    for (int j = 0; j < bufImg.getHeight(); j++) { 

     // Scan through each columns of the image 
     for (int i = 0; i < bufImg.getWidth(); i++) { 
      // Returns an integer pixel in the default RGB color model 
      int values = bufImg.getRGB(i, j); 

      // Convert the single integer pixel value to RGB color 
      Color oldColor = new Color(values); 
      int red = oldColor.getRed(); 

    // get red value 
      int green = oldColor.getGreen(); 

    // get green value 
      int blue = oldColor.getBlue(); 

    // get blue value 
      // Convert RGB to grayscale using formula 
      // gray = 0.299 * R + 0.587 * G + 0.114 * B 
      double grayVal = 0.299 * red + 0.587 * green + 0.114 * blue; 

      // Assign each channel of RGB with the same value 
      Color newColor = new Color((int) grayVal, (int) grayVal, (int) grayVal); 

      // Get back the integer representation of RGB color 
      // and assign it back to the original position 
      bufImg.setRGB(i, j, newColor.getRGB()); 
     } 
    } 
    // return back the resulting image in BufferedImage type 
    return bufImg; 
} 
+1

请不要张贴代码为图像,避免unnessary换行符。 –

+0

图片显示图片下方的错误部分有代码 –

回答

2

有一个随机I得到errorenter图片说明挂在左边// Convert image from type Image to BufferedImage评论。

enter image description here

+1

那么我能做些什么来解决这个错误 –

+1

@TLam好好清除吧 – Nico

+0

好的非常感谢你 –

相关问题