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

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



                                                                                   CHAPTER 6  ■  VISION  217



                                int maxPoints = 0;
                                double totalPoints = 0;
                                // move through image row by row from top to bottom

                                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);
                                        // build accumulator image
                                        // this will get the grey value of the image
                                        // even though i get red here, they are all same value.
                                        int colorValue = getGrey(c).getRed();
                                        // if color is white, then we want to move through all
                                        // lines at this point
                                        if (colorValue == 255) {
                                            // moving through each line from zero to max angle
                                            // at resolution defined.
                                            for (int theta = 0; theta < angle_range; theta = theta + aRes) {
                                                // get the angle 0-90
                                                double radians = (theta / 180.0) * Math.PI ;
                                                // get potential line
                                                // p = radius
                                                // radians = angle
                                                // x = x-coordinate
                                                // y = y-coordinate
                                                int p = (int) (Math.cos(radians) * x + Math
                                                        .sin(radians)
                                                        * y);
                                                // get absolute radius
                                                p = Math.abs(p);
                                                // add the accumulator at this angle and radius
                                                acc[p][theta] = acc[p][theta] + 1;
                                                // want to add the total points accumulated
                                                totalPoints = totalPoints + acc[p][theta];
                                                // get the maximum number of points accumulated
                                                // for a particular bin
                                                if (acc[p][theta] > maxPoints) {
                                                    maxPoints = acc[p][theta];
                                                }
                                            }
                                        }
                                    }
                                }
                                // now work with the parameters space of the accumulator to find the x,y
                                // coordinates of the lines
                                // a = normalized to width
                                // b = normalized height
   231   232   233   234   235   236   237   238   239   240   241