Pixel count in Java

Well, after an all-day battle, I can’t find the right answer. Basically, I have a very simple setup that fills my BufferedImage canvas on a white background. Then I draw a polygon from the points coming from the args array. This seems to work just fine. The problem occurs when I want to count the number of pixels that Polygon fills (which fills).

To do this, I looped around the canvas and compared each pixel using the getRGB method, and if it was not white (background color), I increased the counter. However, the result that I always get is the canvas dimension (640 * 480), which means that the entire canvas is white.

So, I assume that Polygon, which refers to the screen, is not added to the canvas and floats on top? This is the only answer I can come up with, but it can be completely wrong.

I would like to be able to count the number of pixels that are not white. Any suggestions?

the 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 "findPoints (args)" method is too long to publish, but basically everything it does takes argument values ​​and compiles a list of points.

Thanks in advance Boots

+3
source share
2 answers

Just add an exclamation mark to invert the boolean in your state:

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

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

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

BufferedImage, . :

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g = img.createGraphics();
g.fill(polygon);
g.dispose();
countThePixels(img);
+1

/ , BufferedImage. Polygon. Graphics, ""; : , , Graphics .

Graphics

0

All Articles