forked from bytedeco/javacv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HoughLines.java
120 lines (106 loc) · 5.48 KB
/
HoughLines.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import javax.swing.JFrame;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacv.*;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
/**
* C to Java translation of the houghlines.c sample provided in the c sample directory of OpenCV 2.1,
* using the JavaCV Java wrapper of OpenCV 2.2 developped by Samuel Audet.
*
* @author Jeremy Nicola
*/
public class HoughLines {
/**
* usage: java HoughLines imageDir\imageName TransformType
*/
public static void main(String[] args) {
String fileName = args.length >= 1 ? args[0] : "pic1.png"; // if no params provided, compute the defaut image
IplImage src = cvLoadImage(fileName, 0);
IplImage dst;
IplImage colorDst;
CvMemStorage storage = cvCreateMemStorage(0);
CvSeq lines = new CvSeq();
CanvasFrame source = new CanvasFrame("Source");
CanvasFrame hough = new CanvasFrame("Hough");
OpenCVFrameConverter.ToIplImage sourceConverter = new OpenCVFrameConverter.ToIplImage();
OpenCVFrameConverter.ToIplImage houghConverter = new OpenCVFrameConverter.ToIplImage();
if (src == null) {
System.out.println("Couldn't load source image.");
return;
}
dst = cvCreateImage(cvGetSize(src), src.depth(), 1);
colorDst = cvCreateImage(cvGetSize(src), src.depth(), 3);
cvCanny(src, dst, 50, 200, 3);
cvCvtColor(dst, colorDst, CV_GRAY2BGR);
/*
* apply the probabilistic hough transform
* which returns for each line deteced two points ((x1, y1); (x2,y2))
* defining the detected segment
*/
if (args.length == 2 && args[1].contentEquals("probabilistic")) {
System.out.println("Using the Probabilistic Hough Transform");
lines = cvHoughLines2(dst, storage, CV_HOUGH_PROBABILISTIC, 1, Math.PI / 180, 40, 50, 10, 0, CV_PI);
for (int i = 0; i < lines.total(); i++) {
// Based on JavaCPP, the equivalent of the C code:
// CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
// CvPoint first=line[0], second=line[1]
// is:
Pointer line = cvGetSeqElem(lines, i);
CvPoint pt1 = new CvPoint(line).position(0);
CvPoint pt2 = new CvPoint(line).position(1);
System.out.println("Line spotted: ");
System.out.println("\t pt1: " + pt1);
System.out.println("\t pt2: " + pt2);
cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0); // draw the segment on the image
}
}
/*
* Apply the multiscale hough transform which returns for each line two float parameters (rho, theta)
* rho: distance from the origin of the image to the line
* theta: angle between the x-axis and the normal line of the detected line
*/
else if(args.length==2 && args[1].contentEquals("multiscale")){
System.out.println("Using the multiscale Hough Transform"); //
lines = cvHoughLines2(dst, storage, CV_HOUGH_MULTI_SCALE, 1, Math.PI / 180, 40, 1, 1, 0, CV_PI);
for (int i = 0; i < lines.total(); i++) {
CvPoint2D32f point = new CvPoint2D32f(cvGetSeqElem(lines, i));
float rho=point.x();
float theta=point.y();
double a = Math.cos((double) theta), b = Math.sin((double) theta);
double x0 = a * rho, y0 = b * rho;
CvPoint pt1 = cvPoint((int) Math.round(x0 + 1000 * (-b)), (int) Math.round(y0 + 1000 * (a))), pt2 = cvPoint((int) Math.round(x0 - 1000 * (-b)), (int) Math.round(y0 - 1000 * (a)));
System.out.println("Line spoted: ");
System.out.println("\t rho= " + rho);
System.out.println("\t theta= " + theta);
cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0);
}
}
/*
* Default: apply the standard hough transform. Outputs: same as the multiscale output.
*/
else {
System.out.println("Using the Standard Hough Transform");
lines = cvHoughLines2(dst, storage, CV_HOUGH_STANDARD, 1, Math.PI / 180, 90, 0, 0, 0, CV_PI);
for (int i = 0; i < lines.total(); i++) {
CvPoint2D32f point = new CvPoint2D32f(cvGetSeqElem(lines, i));
float rho=point.x();
float theta=point.y();
double a = Math.cos((double) theta), b = Math.sin((double) theta);
double x0 = a * rho, y0 = b * rho;
CvPoint pt1 = cvPoint((int) Math.round(x0 + 1000 * (-b)), (int) Math.round(y0 + 1000 * (a))), pt2 = cvPoint((int) Math.round(x0 - 1000 * (-b)), (int) Math.round(y0 - 1000 * (a)));
System.out.println("Line spotted: ");
System.out.println("\t rho= " + rho);
System.out.println("\t theta= " + theta);
cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0);
}
}
source.showImage(sourceConverter.convert(src));
hough.showImage(houghConverter.convert(colorDst));
source.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hough.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}