-
Counting Pixels
I am using a buffered image and wish to count the pixels in the image to determine the most prominent colors. I have done some research and think i need to use the ColorModel class. If anyone could tell me how to implement this
and use the ColorModel class it would be greatly appreciated.
Thanks
-
the most prominent colors
By this do you mean the colors that occur with the highest frequency, colors with particular qualities, or specific/target colors?
-
I would like to obtain a count of the number of red blue and green colours
-
Code:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
public class ColorCount {
public static void main(String[] args) throws IOException {
String path = pathToImage
BufferedImage image = ImageIO.read(new File(path));
int redCount = 0, greenCount = 0, blueCount = 0;
int red = Color.red.getRGB();
int green = Color.green.getRGB();
int blue = Color.blue.getRGB();
System.out.printf("red = %d green = %d blue = %d%n",
red, green, blue);
int w = image.getWidth();
int h = image.getHeight();
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
int pixel = image.getRGB(x, y);
if(pixel == red) redCount++;
if(pixel == green) greenCount++;
if(pixel == blue) blueCount++;
}
}
System.out.printf("redCount = %d greenCount = %d blueCount = %d%n",
redCount, greenCount, blueCount);
}
}
-
Thanks
Thanks for the excellent help your code was very useful. Could you please explain what the following code does especially the role of "d" and "n".
("red = %d green = %d blue = %d%n",
red, green, blue);
-
Formatter class was introduced in j2se 1.5
d: format the value to an integer
n: new line.