Reading a color value from webcam.

edited April 2016 in Library Questions

Hello, I am making a small program that reads what color hat you are wearing and then performs a virtual key press via a robot to interact with a game. I got it sort of working so far in that it will detect red colors but not blue or green. I am using a hue color index to track the colors. Any idea of how I could do it differently or if there is anything I am missing.

// check for red pixels int redPixelCount = 0; int bluePixelCount = 0; int greenPixelCount = 0; int yellowPixelCount= 0; for (int x=0; x<640; x++) { for (int y=0; y<240; y++) { color c = get(x, y); int px = cam.pixels[y*width+x]; float h = hue(px); float s = saturation(px); float b = brightness(px); // if it's bright, saturated and a specific color if (b > 100 && s > 100 && (h < 20 || h > 230)) { redPixelCount++; } if (b > 100 && s > 100 && (h < 240 && h > 180)) { bluePixelCount++; } if (b > 100 && s > 100 && (h < 80 && h > 140)) { greenPixelCount++; } if (b > 100 && s > 100 && (h < 50 && h > 70)) { yellowPixelCount++; } } }

Answers

  • Some of your equations aren't right. For example h<50 && h>70 will always be false. You want to do flip this around. Instead try h>50 && h<70

Sign In or Register to comment.