Page 216 - The Definitive Guide to Building Java Robots
P. 216
Preston_5564C06.fm Page 197 Friday, September 23, 2005 5:13 AM
CHAPTER 6 ■ VISION 197
} else if (c == Color.GREEN) {
colorValue = new Color(srcPixel).getGreen();
} else if (c == Color.BLUE) {
colorValue = new Color(srcPixel).getBlue();
}
// set that color as grey version
dstImg.setRGB(x, y, new Color(colorValue, colorValue, colorValue)
.getRGB());
}
}
// return image
return dstImg;
}
Figure 6-16. The red filter
One way to study the color of an image is by computing image statistics. If I wanted to find
the average red, average green, or average blue of an image, there are two ways to do it: I can go
through pixel by pixel and count the colors, or I can use Java Advanced Imaging.
Code Objective
The objective in this example is to calculate the average red, green, and blue values of an image
pixel by pixel.
Code Discussion
I’ve found this method to be slightly faster than using Java Advanced Imaging. The following
method sums all the color values of the image together and then divides the quantities by the
total pixels in the image. See Example 6-18.