Page 209 - The Definitive Guide to Building Java Robots
P. 209
Preston_5564C06.fm Page 190 Friday, September 23, 2005 5:13 AM
190 CHAPTER 6 ■ VISION
Example 6-13. resize()
public BufferedImage resize(BufferedImage srcImg, int targetW, int targetH) {
// create new bufferedImage
BufferedImage dstImg = new BufferedImage(targetW, targetH,
BufferedImage.TYPE_INT_RGB);
// create new canvas
Graphics2D g = dstImg.createGraphics();
g.setBackground(Color.BLACK);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
double sx = (double) targetW / srcImg.getWidth();
double sy = (double) targetH / srcImg.getHeight();
// draw src image on new object
g.drawRenderedImage(srcImg, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
// return new image
return dstImg;
}
Figure 6-10. The image resized to 80×60
Another basic technique used in image processing is a process called thresholding.
Thresholding is a valuable processing technique used in all types of image processing algorithms.
In its basic form, it can be used to remove pixels from their background. When combined with
other processing techniques, it’s used to identify specific features like edges or lines.
Code Objective
The objective here is to demonstrate thresholding of an image. I’ll remove the top half of the
colors and convert them to black, leaving the bottom half as white.