How to calculate the pixels of the image area [closed]

0

I would like to calculate the pixels of the area of an image is not width and height because it is an irregular figure, I want to know how many pixels there are inside this figure, example a triangle, if you calculate the height x width of a non triangle will give the value of your area because it will take into account as a square. I already looked for a lot on the internet and I did not find anything that would help me right now!

    
asked by anonymous 16.01.2018 / 12:20

1 answer

0
Assuming that the background of the image is white and the object of the image (triangle, for example) is black, I imagine it is necessary to go through each pixel of the image and check that the color of the image is black. If it is, add 1 to the area variable. In the end, it will have the amount of pixels that make up the object of the image (area).

It would look something like this:

public static double percentualBrancos(BufferedImage img)
{
    double area = 0;
    for (int y = 0; y < img.getHeight(); y++)
        for (int x = 0; x < img.getWidth(); x++)
        {
            Color pixel = new Color(img.getRGB(x, y));
            if ((pixel.getRed() == 0) && (pixel.getGreen() == 0) && (pixel.getBlue() == 0))
                area++;
        }
    return area; 
}
    
16.01.2018 / 13:12