Page 207 - The Definitive Guide to Building Java Robots
P. 207
Preston_5564C06.fm Page 188 Friday, September 23, 2005 5:13 AM
188 CHAPTER 6 ■ VISION
if (parms.getName().equalsIgnoreCase(FILTER_RESIZE)) {
int w = ((Integer) parms.getParameters().get(0)).intValue();
int h = ((Integer) parms.getParameters().get(1)).intValue();
dstImg = resize(parms.getImage(), w, h);
}
if (parms.getName().equalsIgnoreCase(FILTER_HOUGH_LINES)) {
dstImg = getHoughLines(parms.getImage());
}
return dstImg;
}
....
}
In the rgbToGrey() method, I iterate through each pixel of the image and get its color. I
then average the Red, Green, and Blue components of the color to get a grey via the first getGrey
method. Then, by setting all three color components to this average, you get a 256-color greyscale
image of the original. You can see the output of Example 6-12 in Figure 6-9.
Example 6-12. rgbToGrey(), getGrey()
// to greyscale image
public BufferedImage rgbToGrey(BufferedImage srcImg) {
int h = srcImg.getHeight();
int w = srcImg.getWidth();
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);
Color g = getGrey(c);
dstImg.setRGB(x, y, g.getRGB());
}
}
return dstImg;
}
// return greyscale equivalent of pixel as color
public Color getGrey(Color color) {
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
int gray = (int) ((r + g + b) / 3.0);