Page 223 - The Definitive Guide to Building Java Robots
P. 223
Preston_5564C06.fm Page 204 Friday, September 23, 2005 5:13 AM
204 CHAPTER 6 ■ VISION
public int colorRatioCount(BufferedImage srcImg, ColorGram cg) {
int h = srcImg.getHeight();
int w = srcImg.getWidth();
int count = 0;
BufferedImage dstImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
int srcPixel = srcImg.getRGB(x, y);
Color c = new Color(srcPixel);
if (cg.isMatch(c)) {
count++;
}
}
}
return count;
}
These sample ColorGrams represent the cans of soda. In the first ColorGram, you see that
red is optimized with two thresholds: 104 and 158. Second, the green thresholds are set so that
they’re 22 points lower than the red pixel. Third, the blue threshold is 31 points lower than the
red pixel.
For the 7-Up, the optimized color is green, followed by the tuning of red and blue with
respect to the green pixel. And finally for Pepsi, the optimized color is blue, followed by the
tuning of the red and green pixels with respect to blue. See Example 6-22 and Figures 6-17
through 6-19.
Example 6-22. Sample ColorGrams (Coke, 7-Up, and Pepsi)
public static ColorGram COKE = new ColorGram (new double[] {
0.0,0.0,0.0,104.0,
0.0,0.0,0.0,158.0,
0.0,0.0,0.0,0.0,
1.0,0.0,0.0,-22.0,
0.0,0.0,0.0,0.0,
1.0,0.0,0.0,-31.0
});
public static ColorGram SEVEN_UP = new ColorGram (new double[] {
0.0,0.0,0.0,0.0,
0.0,1.0,0.0,-38.0,
0.0,0.0,0.0,90.0,
0.0,0.0,0.0,147.0,
0.0,0.0,0.0,0.0,
0.0,1.0,0.0,-6.0
});