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

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



                                                                                   CHAPTER 6  ■  VISION  203



                            public double[] getColorGram() {
                                return colorGram;
                            }
                            public void setColorGram(double[] colorGram) {
                                this.colorGram = colorGram;
                            }

                            public Object clone() {
                                double[] newCg = new double[24];
                                for (int d=0;d<24;d++) {
                                    newCg[d] = colorGram[d];
                                }
                                return new ColorGram(newCg);
                            }
                        }

                            In Example 6-21, this is the filter applied in the ImageProcessing class. Again, it’s very
                        similar to the thresholding class, except I call isMatch() by passing in the ColorGram.


                        Example 6-21. ImageProcessing.colorRatio() and colorRatioCount()
                            public BufferedImage colorRatio(BufferedImage srcImg, ColorGram cg) {

                                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);
                                        // calls hard work done here.
                                        if (cg.isMatch(c)) {
                                            // for real color
                                            dstImg.setRGB(x, y, c.getRGB());
                                            // for binary color
                                            //dstImg.setRGB(x, y, Color.BLACK.getRGB());
                                        } else {
                                            dstImg.setRGB(x, y, Color.BLACK.getRGB());
                                        }


                                    }
                                }
                                return dstImg;
                            }
   217   218   219   220   221   222   223   224   225   226   227