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

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



                                                                                   CHAPTER 6  ■  VISION  183



                            public void setImage(BufferedImage image) {
                                this.image = image;
                            }

                            public ArrayList getParameters() {
                                return parameters;
                            }

                            public void addParameters(Object parameter) {
                                parameters.add(parameter);
                            }

                            public String getName() {
                                return name;
                            }
                        }
                            Next, for basic image processing, I’ll perform the following operations:

                           • Change an image to greyscale (pixel)
                           • Threshold an image (pixel)

                           • Resize an image (area)
                           • Detect motion (pixel)
                            But to render these, I want to create a class that displays two images. The first is the original
                        image from the webcam, and the second is the image after it’s processed. While you need to do
                        this for your robot, it’s nice for debugging.


                        Code Objective
                        The code objective in this section is to create a class that can view the images before and after
                        they are processed.

                        Code Discussion
                        First, I’ll reuse the WebCamViewer created in the last section by extending it. The fields in this
                        class are two ImagePanels: one for the original image and one for the processed image. The
                        final field is an ArrayList that I’ll use to keep the list of FilterParameters.
                            The constructor calls super() for the camera and then calls init2(), which initializes the
                        current class to a later size and adds the two panels side by side.
                            The getPic() method from the parent class is overloaded so I can, one, set the current image
                        into the srcPanel, and two, call the doProcessing() method for the dstPanel. By overriding the
                        parent method, I can reuse the call to getPic() from the timer created in the parent class.
                            The doProcessing() method creates a class ImageProcessor, and then based on the number
                        of filters in the list, it iterates and processes all the filters before returning the image to the
                        getPic() method, where it can be set in the dstPanel or right pane.
   197   198   199   200   201   202   203   204   205   206   207