/Users/lyon/j4p/src/j2d/edge/HoughCircles.java

1    package j2d.edge; 
2     
3    import j2d.ImageProcessorInterface; 
4    import j2d.ImageProcessorFactory; 
5     
6    import java.awt.*; 
7     
8     
9    public class HoughCircles 
10           implements ImageProcessorInterface, 
11           ImageProcessorFactory { 
12       public float radius; // Radius of circles to be found 
13       public int maxCircles; // Numbers of circles to be found 
14       public int threshold = -1; // An alternative to maxCircles 
15       // must lies in the interval: [-125,+125]. 
16       // Higher values - fewer circles. 
17       byte imageValues[]; // Raw image (returned by ip.getPixels()) 
18       double houghValues[][]; // Hough Space Values 
19       public int width; // Hough Space width 
20       public int height;  // Hough Space heigh 
21       public int offset; // Image Width 
22       public int offx = 0;   // ROI x offset 
23       public int offy = 0;   // ROI y offset 
24       Point centerPoint[]; // Center Points of the Circles Found. 
25       private int vectorMaxSize = 500; 
26       boolean useThreshold = false; 
27    
28    
29       public HoughCircles() { 
30    
31       } 
32    
33       public ImageProcessorInterface getProcessor(int a) { 
34           return new HoughCircles(); 
35       } 
36    
37       public Image process(Image img) { 
38           return hough(img); 
39    
40       } 
41    
42       private Image hough(Image img) { 
43    
44           int i = 0; 
45           int j = 1; 
46           int k = width - j; 
47           int l = height - j; 
48           houghValues = new double[width][height]; 
49           int i2 = Math.round(8F * radius); 
50           int ai[][] = new int[2][i2]; 
51           for (int j2 = 0; j2 < i2; j2++) { 
52               double d1 = (6.2831853071795862D * (double) j2) / (double) i2; 
53               int k1 = (int) Math.round((double) radius * Math.cos(d1)); 
54               int l1 = (int) Math.round((double) radius * Math.sin(d1)); 
55               if ((i == 0) | (k1 != ai[0][i]) & (l1 != ai[1][i])) { 
56                   ai[0][i] = k1; 
57                   ai[1][i] = l1; 
58                   i++; 
59               } 
60           } 
61    
62           double d; 
63    
64           for (int y = j; y < l; y++) { 
65               for (int x = j; x < k; x++) { 
66                   if (imageValues[(x + offx) + (y + offy) * offset] == 0) 
67                       d = 0; 
68                   else 
69                       d = 1; 
70                   if (d != 0.0D) { 
71                       for (int i3 = 0; i3 < i; i3++) { 
72                           int i1 = y + ai[0][i3]; 
73                           int j1 = x + ai[1][i3]; 
74                           if ((i1 >= 0) & (i1 < height) & (j1 >= 0) & (j1 < width)) { 
75                               houghValues[j1][i1] += d; 
76                           } 
77                       } 
78    
79                   } 
80               } 
81    
82           } 
83    
84    
85           return 
86                   null; 
87       } 
88    
89   } 
90    
91