I want to convert a colored image to grayscale without too much-predefined methods.
the code of reading the colored image is here
The main method:
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();
}
Another method:
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;
}
I need to convert the output to grayscale... Here is some of the output
rgb: 255 255 255
rgb: 255 255 255
rgb: 255 255 255
rgb: 255 255 255
rgb: 255 255 255
rgb: 255 255 255
I need to check every pixel for it's RGB and convert it into grayscale.