我可以帮助您将一张彩色图片转为灰度图,而且不需要太多预定义的方法。
读取彩色图片的代码如下:
主要方法:
读取彩色图片的代码如下:
主要方法:
BufferedImage img = null;
try {
img = ImageIO.read(new File(IMG));
int[][] pixelData = new int[img.getHeight() * img.getWidth()][3];
int[] rgb;
int counter = 0;
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
rgb = getPixelData(img, i, j);
for (int k = 0; k < rgb.length; k++) {
pixelData[counter][k] = rgb[k];
}
counter++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
另一种方法:
private static int[] getPixelData(BufferedImage img, int x, int y) {
int argb = img.getRGB(x, y);
int rgb[] = new int[] {
(argb >> 16) & 0xff, //red
(argb >> 8) & 0xff, //green
(argb ) & 0xff //blue
};
System.out.println("rgb: " + rgb[0] + " " + rgb[1] + " " + rgb[2]);
return rgb;
}
我需要将输出转换为灰度图像... 这是一些输出示例
rgb: 255 255 255
rgb: 255 255 255
rgb: 255 255 255
rgb: 255 255 255
rgb: 255 255 255
rgb: 255 255 255
我需要检查每个像素的RGB值并将其转换为灰度。