Page 232 - The Definitive Guide to Building Java Robots
P. 232

Preston_5564C06.fm  Page 213  Friday, September 23, 2005  5:13 AM



                                                                                   CHAPTER 6  ■  VISION  213



                           • ColorGram: A data structure that represents the color ratios as a linear equation
                           • ColorGramCalibration: A program that creates a ColorGram from a thumbnail of the
                             desired image

                            Next, I’ll describe image processing using Java’s Advanced Imaging API.


                        6.4 Advanced Image Processing

                        To find the edge of an image requires area-level processing. Area-level processing is the process
                        of looking at a specific pixel in relation to the pixels around it. This is done by applying a kernel
                        to an image. Some operations we will show are low-pass filters or smoothing, high-pass filters
                        or sharpening, and line-finding filters known as Sobel Gradient masks.

                        Code Objective

                        The following objectives are handled in this example:
                           • Smooth an image by passing it through a low-pass filter.

                           • Sharpen an image by passing it through a high-pass filter.
                           • Get the edges of an image by passing it through a Sobel Gradient filter.

                        Code Discussion

                        Smoothing involves a kernel containing values of 1/9 in a 3×3 matrix. When applying this
                        kernel to the image, you get the results shown in Figure 6-21.
                            Sharpening involves a kernel of 0, –1, 0, –1, 5, –1, 0, –1, 0 in a 3×3 matrix. When applying
                        this kernel to the image, you get the results shown in Figure 6-22.
                            The Sobel Gradient filter involves taking the gradient of neighboring pixels to find
                        edges. When applying this kernel to an image, you get the results shown in Figure 6-23. (See
                        Examples 6-24 through 6-27.)

                        Example 6-24. bufferedToPlanar() and planarToBuffered()
                            private PlanarImage bufferedToPlanar(BufferedImage bImg) {
                                Image awtImg = Toolkit.getDefaultToolkit().createImage(bImg.getSource());
                                return JAI.create("awtimage", awtImg);
                            }


                            private BufferedImage planarToBuffered(PlanarImage pImg) {
                                return pImg.getAsBufferedImage();
                            }










                   97022d2480fe4a63cfdfa123a6e70098
   227   228   229   230   231   232   233   234   235   236   237