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

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



                                                                                   CHAPTER 6  ■  VISION  189



                                return new Color(gray, gray, gray);
                            }

                            //return greyscale equivalent of pixel as int
                            public int getGrey(int colorInt) {
                                return getGrey(new Color(colorInt)).getRed();
                            }



















                        Figure 6-9. A color image converted to GreyScale

                            One of the things you might notice from the last example is that iterating through 320×240
                        pixels might be really fast for converting to grey, but not if you’re using a few different filters
                        and hope to maintain your desired frame rate. Currently, it takes about 63 milliseconds from
                        the start of this method to the end. Since the frame rate is 15 frames per second, the maximum
                        processing time per frame is 67 milliseconds. We’re getting close. If I add a 10-millisecond
                        pause in the middle, I’d notice the frame-rate decrease. We can get around this by resizing the
                        image. By resizing the image from 320×240 to 160×120, the time to process the image is reduced
                        by four to about 15 milliseconds.

                        Code Objective

                        The objective in this example is to resize an image.

                        Code Discussion
                        This uses the java.awt.Graphics2D class. First, we create a destination image. Second, we give
                        the class rendering hints on how to render the new image. Third, we draw the new, scaled
                        version of the image using the AffineTransformation class. Finally, we return the new image.
                        You can see the results of Example 6-13 in Figure 6-10.



                        ■Note  You’ll see in later sections that if we iterate through all the pixels of an image, resizing them by
                        one-half, it will improve the performance of our algorithm by a minimum of 400 percent.
   203   204   205   206   207   208   209   210   211   212   213