17
17
18
18
public class HoughTransformer {
19
19
20
+ int centerTolerance , houghTolerance ;
21
+
22
+ /**
23
+ * The first constructor for HoughTransformer which
24
+ * will set the tolerance for the center of the circle.
25
+ *
26
+ * @param centerTolerance The tolerance in pixels for center of the circle.
27
+ * @param houghTolerance Tolerance for the hough transform (0.0 - 1.0)
28
+ */
29
+ public HoughTransformer (int centerTolerance , double houghTolerance ){
30
+ this .centerTolerance = centerTolerance ;
31
+ this .houghTolerance = houghTolerance ;
32
+ }
33
+
20
34
/**
21
35
* This method will try to find a circle of a
22
36
* certain size, and then return true or
@@ -26,9 +40,9 @@ public class HoughTransformer{
26
40
* @return boolean Whether or not a circle was found.
27
41
*/
28
42
public void process (RenderedImage img ){
29
- float [] imgData = new float [ img .getWidth () * img .getHeight ()] ;
30
- img .getData ().getPixels (0 ,0 , img . getWidth () ,
31
- img . getHeight (), imgData );
43
+ int width = img .getWidth (), height = img .getHeight ();
44
+ float [] imgData = img .getData ().getPixels (0 ,0 , width ,
45
+ height , ( float []) null );
32
46
33
47
/*Find out more about what threshold does
34
48
I'm referring to this site: http://homepages.inf.ed.ac.uk/rbf/HIPR2/hough.htm
@@ -38,9 +52,22 @@ public void process(RenderedImage img){
38
52
39
53
LocatedCircle circle = hough .getLargestCircle (imgData ,
40
54
img .getWidth (), img .getHeight ());
55
+
56
+ /**
57
+ * Check that the circle we're looking for is near the center.
58
+ * If it is not, then the hough transform probably found
59
+ * a larger object than the one in question and failed.
60
+ */
61
+ int circX = circle .getX (), circY = circle .getY ();
62
+ if ((circX < width -centerTolerance || circX > width -centerTolerance )
63
+ || (circY < height -centerTolerance || circY >height +centerTolerance ))
64
+ return Category .UNDEFINED ;
41
65
42
66
System .out .println (circle .getRadius ());
67
+ System .out .println ("Location: (" +circle .getX () +"," +circle .getY ());
68
+ System .out .println ("Confidence: " + circle .getConfidence ());
43
69
}
70
+
44
71
45
72
public static void main (String args []){
46
73
if (args .length < 1 )
0 commit comments