Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pixel Count in Java

Ok, after battling for the entire afternoon, I cant seem to get the right answer. Basically, I have a very simple setup that fills my canvas, a BufferedImage, with a white background. I then draw a Polygon from points coming from my args array. Display-wise, this works perfectly. The problem comes in when I want to count the number of pixels the Polygon (which is filled), uses up.

To do this, I looped through the canvas and compared each pixel using the getRGB method, and if it wasn't white (the background color), I incremented a counter. However, the result I always get is the dimension of the canvas (640*480) which means the entire canvas is white.

So I'm assuming the Polygon that gets drawn to the screen isn't added to the canvas, and is floating on top? Thats the the only answer I can come up with, but could be entirely wrong.

What I'd like is to be able to count the number of pixels that aren't white. Any suggestions?

Code:

public class Application extends JPanel {

public static int[] xCoord = null;
public static int[] yCoord = null;
private static int xRef = 250;
private static int yRef = 250;
private static int xOld = 0;
private static int yOld = 0;
private static BufferedImage canvas;
private static Polygon poly;

public Application(int width, int height) {
    canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    fillCanvas(Color.WHITE);
    poly = new Polygon(xCoord, yCoord, xCoord.length);     

    //Loop through each pixel
    int count = 0;
    for (int i = 0; i < canvas.getWidth(); i++)
        for (int j = 0; j < canvas.getHeight(); j++) {
            Color c = new Color(canvas.getRGB(i, j));
            if (c.equals(Color.WHITE))
                count++;
        }
    System.out.println(count);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(canvas, null, null);
    g2.fillPolygon(poly);

}

public void fillCanvas(Color c) {
    int color = c.getRGB();
    for (int x = 0; x < canvas.getWidth(); x++) {
        for (int y = 0; y < canvas.getHeight(); y++) {
            canvas.setRGB(x, y, color);
        }
    }
    repaint();
}


public static void main(String[] args) {       
    if (args != null)
        findPoints(args);

    JFrame window = new JFrame("Draw");
    Application panel = new Application(640, 480);

    window.add(panel);
    Dimension SIZE = new Dimension(640, 480);
    window.setPreferredSize(SIZE);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
    window.pack();    
}//end main

The method 'findPoints(args)' is too long to post, but basically all it does it takes the argument values and compiles a list of points.

Thanks in advance, Boots

like image 321
Boots Avatar asked Sep 05 '26 08:09

Boots


1 Answers

Just add an exclamation mark to invert the boolean value inside your condition:

if (!c.equals(Color.WHITE))

A faster way is to check the rgb value instead of first creating a Color object of it:

if ((rgb & 0xFFFFFF) != 0xFFFFFF)

Create a BufferedImage, draw the polygon, and count. Basically, this:

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g = img.createGraphics();
g.fill(polygon);
g.dispose();
countThePixels(img);
like image 131
Martijn Courteaux Avatar answered Sep 06 '26 22:09

Martijn Courteaux