Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java
16354 views
1
package sample;
2
/**
3
* @file HoughCircles.java
4
* @brief This program demonstrates circle finding with the Hough transform
5
*/
6
7
import org.opencv.core.*;
8
import org.opencv.core.Point;
9
import org.opencv.highgui.HighGui;
10
import org.opencv.imgcodecs.Imgcodecs;
11
import org.opencv.imgproc.Imgproc;
12
13
class HoughCirclesRun {
14
15
public void run(String[] args) {
16
17
//! [load]
18
String default_file = "../../../../data/smarties.png";
19
String filename = ((args.length > 0) ? args[0] : default_file);
20
21
// Load an image
22
Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
23
24
// Check if image is loaded fine
25
if( src.empty() ) {
26
System.out.println("Error opening image!");
27
System.out.println("Program Arguments: [image_name -- default "
28
+ default_file +"] \n");
29
System.exit(-1);
30
}
31
//! [load]
32
33
//! [convert_to_gray]
34
Mat gray = new Mat();
35
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
36
//! [convert_to_gray]
37
38
//![reduce_noise]
39
Imgproc.medianBlur(gray, gray, 5);
40
//![reduce_noise]
41
42
//! [houghcircles]
43
Mat circles = new Mat();
44
Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
45
(double)gray.rows()/16, // change this value to detect circles with different distances to each other
46
100.0, 30.0, 1, 30); // change the last two parameters
47
// (min_radius & max_radius) to detect larger circles
48
//! [houghcircles]
49
50
//! [draw]
51
for (int x = 0; x < circles.cols(); x++) {
52
double[] c = circles.get(0, x);
53
Point center = new Point(Math.round(c[0]), Math.round(c[1]));
54
// circle center
55
Imgproc.circle(src, center, 1, new Scalar(0,100,100), 3, 8, 0 );
56
// circle outline
57
int radius = (int) Math.round(c[2]);
58
Imgproc.circle(src, center, radius, new Scalar(255,0,255), 3, 8, 0 );
59
}
60
//! [draw]
61
62
//! [display]
63
HighGui.imshow("detected circles", src);
64
HighGui.waitKey();
65
//! [display]
66
67
System.exit(0);
68
}
69
}
70
71
public class HoughCircles {
72
public static void main(String[] args) {
73
// Load the native library.
74
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
75
new HoughCirclesRun().run(args);
76
}
77
}
78
79