Web Analytics Made Easy -
StatCounter Retrieve amount of red colored pixels in a set area - CodingForum

Announcement

Collapse
No announcement yet.

Retrieve amount of red colored pixels in a set area

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Retrieve amount of red colored pixels in a set area

    I arrived here because searching google didn't give me anything and I figured it might not be that hard to realize.

    My question:
    I need to create a box of coordinates x, y, width, height. Which I need to be able to search for a specific color (let´s say red) and then add up these pixel counts.

    I figure it works something like this: It starts at the top left, if pixel is red (255, 0, 0) then count= count+1, next pixel. Else next pixel etc.

    The background is plain white, and there is only 1 actual color used (red).

    What I know how to do:
    Get pixel color at specific coordinate (robot.getPixelColor(x, y))

    What I probably need to know:
    How to increment the coordinates (x+1 untill end, then y+1, and x+1 untill the end etc. while continuously checking for pixelcolor).

    Perhaps there is a total solution, maybe it can check the entire box as a whole and give the amount of red colored pixels within it?

    I know it might come across as vague but that's mostly because I have no idea how to do this and am trying to paint a picture of it from what I see as logical. Now if someone would be so kind to push me into the right direction (maybe there is a whole other solution to this) I would be grateful.

  • #2
    I think I did it, coordinates and RGB code was set for testing purposes. If anyone knows a less memory/time consuming way or just an all-round better solution, I'd love to hear it.

    Code:
    public static void main(String[] args) throws AWTException {
    
            int y = 500;
            int x = 300;
            int red;
            int count = 0;
            Robot robot = new Robot();
    
            for ( x = 300;  x < 901; x++ ) {
    
            Color color = robot.getPixelColor(x, y);
            red = color.getGreen();
    
                //If pixel is colored, then add 1 to count.
                if (red == 128) {
                    count = count+1;
                    System.out.println("Amount of colored pixels:" +count);
                }
    
                //If x coordinate reaches 900, then set it back to original value
                //and increment y coordinate with 1.
                if (x == 900) {
                    x = 300;
                    y = y + 1;
                }
    
                //If y coordinates reaches the set ending, then break off.
                if (y == 801) {
                   break;
                }
            }
        }
    }

    Comment

    Working...
    X