Skip to content

Commit 1b6a5a0

Browse files
committed
Add findLines() function based on HoughLinesP.
Add Line class as result for findLines() including angle calculation in double precision. Add example of usage of findLines()
1 parent fb90dd3 commit 1b6a5a0

6 files changed

Lines changed: 99 additions & 68 deletions

File tree

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,38 @@
11
import gab.opencv.*;
2-
import org.opencv.imgproc.Imgproc;
3-
import org.opencv.core.Mat;
4-
import java.awt.geom.Line2D;
52

63
OpenCV opencv;
7-
8-
ArrayList<Line2D> lines;
4+
ArrayList<Line> lines;
95

106
void setup() {
117
PImage src = loadImage("film_scan.jpg");
128
src.resize(0, 800);
139
size(src.width, src.height, P2D);
1410

1511
opencv = new OpenCV(this, src);
16-
opencv.findSobelEdges(1, 0);
17-
opencv.threshold(20);
18-
19-
lines = findLines(opencv.getGray(), 200, 120, 5);
20-
21-
for(Line2D l : lines){
22-
println(Math.toDegrees(angleOf(l)));
23-
}
24-
25-
noLoop();
26-
}
27-
28-
double angleOf(Line2D l){
29-
Line2D vertical = new Line2D.Double(0,0, 0,1);
30-
31-
return angleBetween(l, vertical);
32-
}
12+
opencv.findCannyEdges(20, 75);
3313

34-
double angleBetween(Line2D line1, Line2D line2) {
35-
double angle1 = Math.atan2(line1.getY1() - line1.getY2(),
36-
line1.getX1() - line1.getX2());
37-
double angle2 = Math.atan2(line2.getY1() - line2.getY2(),
38-
line2.getX1() - line2.getX2());
39-
return angle1-angle2;
40-
}
41-
42-
ArrayList<Line2D> findLines(Mat m, int threshold, double minLength, double maxGap) {
43-
ArrayList<Line2D> result = new ArrayList<Line2D>();
44-
45-
Mat lineMat = new Mat();
46-
Imgproc.HoughLinesP(m, lineMat, 1, PI/180.0, threshold, minLength, maxGap);
47-
for (int i = 0; i < lineMat.width(); i++) {
48-
double[] coords = lineMat.get(0, i);
49-
result.add(new Line2D.Double(coords[0], coords[1], coords[2], coords[3]));
50-
}
51-
52-
return result;
14+
// Find lines with Hough line detection
15+
// Arguments are: threshold, minLengthLength, maxLineGap
16+
lines = opencv.findLines(100, 30, 20);
5317
}
5418

5519
void draw() {
5620
image(opencv.getOutput(), 0, 0);
57-
stroke(0, 255, 0);
58-
strokeWeight(2);
59-
for (Line2D line : lines) {
60-
line((float)line.getX1(), (float)line.getY1(), (float)line.getX2(), (float)line.getY2());
21+
strokeWeight(3);
22+
23+
for (Line line : lines) {
24+
// lines include angle in radians, measured in double precision
25+
// so we can select out vertical and horizontal lines
26+
// They also include "start" and "end" PVectors with the position
27+
if (line.angle >= radians(0) && line.angle < radians(1)) {
28+
stroke(0, 255, 0);
29+
line(line.start.x, line.start.y, line.end.x, line.end.y);
30+
}
31+
32+
if (line.angle > radians(89) && line.angle < radians(91)) {
33+
stroke(255, 0, 0);
34+
line(line.start.x, line.start.y, line.end.x, line.end.y);
35+
}
6136
}
6237
}
6338

readme.md

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ OpenCV for Processing is in the early stages of development. Currently it only s
1616

1717
_NB: When running on the Mac, make sure you have Processing set to 64-bit mode in the Preferences_
1818

19-
See [here](https://github.com/atduskgreg/OpenCVPro/releases) for the latest release.
19+
See [here](https://github.com/atduskgreg/opencv-processing/releases) for the latest release.
2020

2121
### Examples
2222

2323
#### LiveCamTest
2424

2525
Access a live camera and do image processing on the result, specifically face detection.
2626

27-
Code: [LiveCamTest.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/LiveCamTest/LiveCamTest.pde)
27+
Code: [LiveCamTest.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/LiveCamTest/LiveCamTest.pde)
2828

2929
_Note: There's a bug that prevents live camera access in current versions of Processing 2.0 on machines with a Retina display._
3030

@@ -34,79 +34,87 @@ Detect faces in images.
3434

3535
<a href="http://www.flickr.com/photos/unavoidablegrain/8634017624/" title="Screen Shot 2013-04-08 at 1.22.18 PM by atduskgreg, on Flickr"><img src="http://farm9.staticflickr.com/8543/8634017624_35f7ef05ce.jpg" width="500" height="358" alt="Screen Shot 2013-04-08 at 1.22.18 PM"></a>
3636

37-
Code: [FaceDetection.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/FaceDetection/FaceDetection.pde)
37+
Code: [FaceDetection.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/FaceDetection/FaceDetection.pde)
3838

3939
#### BrightnessContrast
4040

4141
Adjust the brightness and contrast of color and gray images.
4242

4343
<a href="http://www.flickr.com/photos/unavoidablegrain/9155239258/" title="brightness and contrast by atduskgreg, on Flickr"><img src="http://farm3.staticflickr.com/2841/9155239258_41a7df36c6.jpg" width="500" height="358" alt="brightness and contrast"></a>
4444

45-
Code: [BrightnessContrast.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/BrightnessContrast/BrightnessContrast.pde)
45+
Code: [BrightnessContrast.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/BrightnessContrast/BrightnessContrast.pde)
4646

4747
#### FilterImages
4848

4949
Basic filtering operations on images: threshold, blur, and adaptive thresholds.
5050

5151
<a href="http://www.flickr.com/photos/unavoidablegrain/8643666252/" title="Screen Shot 2013-04-12 at 1.42.30 PM by atduskgreg, on Flickr"><img src="http://farm9.staticflickr.com/8240/8643666252_be0da1c751.jpg" width="500" height="358" alt="Screen Shot 2013-04-12 at 1.42.30 PM"></a>
5252

53-
Code: [FilterImages.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/FilterImages/FilterImages.pde)
53+
Code: [FilterImages.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/FilterImages/FilterImages.pde)
5454

5555
#### FindContours
5656

5757
Find contours in images and calculate polygon approximations of the contours (i.e., the closest straight line that fits the contour).
5858

5959
<a href="http://www.flickr.com/photos/unavoidablegrain/9024663015/" title="contours with polygon approximations by atduskgreg, on Flickr"><img src="http://farm4.staticflickr.com/3719/9024663015_f419b117b1.jpg" width="500" height="208" alt="contours with polygon approximations"></a>
6060

61-
Code: [FindContours.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/FindContours/FindContours.pde)
61+
Code: [FindContours.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/FindContours/FindContours.pde)
6262

6363
#### FindEdges
6464

6565
Three different edge-detection techniques: Canny, Scharr, and Sobel.
6666

6767
<a href="http://www.flickr.com/photos/unavoidablegrain/8635989723/" title="Screen Shot 2013-04-10 at 2.03.59 AM by atduskgreg, on Flickr"><img src="http://farm9.staticflickr.com/8109/8635989723_170b69dca0.jpg" width="500" height="358" alt="Screen Shot 2013-04-10 at 2.03.59 AM"></a>
6868

69-
Code: [FindEdges.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/FindEdges/FindEdges.pde)
69+
Code: [FindEdges.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/FindEdges/FindEdges.pde)
70+
71+
#### FindLines
72+
73+
Find straight lines in the image using Hough line detection.
74+
75+
<a href="http://www.flickr.com/photos/unavoidablegrain/9263329608/" title="Hough line detection by atduskgreg, on Flickr"><img src="http://farm4.staticflickr.com/3781/9263329608_735ce228bb.jpg" width="486" height="500" alt="Hough line detection"></a>
76+
77+
Code: [HoughLineDetection.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/HoughLineDetection/HoughLineDetection.pde)
7078

7179
#### BrightestPoint
7280

7381
Find the brightest point in an image.
7482

7583
<a href="http://www.flickr.com/photos/unavoidablegrain/9199572469/" title="finding the brightest point by atduskgreg, on Flickr"><img src="http://farm8.staticflickr.com/7407/9199572469_4a25c83062.jpg" width="500" height="366" alt="finding the brightest point"></a>
7684

77-
Code: [BrightestPoint.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/BrightestPoint/BrightestPoint.pde)
85+
Code: [BrightestPoint.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/BrightestPoint/BrightestPoint.pde)
7886

7987
#### RegionOfInterest
8088

8189
Assign a sub-section (or Region of Interest) of the image to be processed. Video of this example in action here: [Region of Interest demo on Vimeo](https://vimeo.com/69009345).
8290

8391
<a href="http://www.flickr.com/photos/unavoidablegrain/9077805277/" title="region of interest by atduskgreg, on Flickr"><img src="http://farm4.staticflickr.com/3795/9077805277_084d87a3a5.jpg" width="500" height="358" alt="region of interest"></a>
8492

85-
Code: [RegionOfInterest.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/RegionOfInterest/RegionOfInterest.pde)
93+
Code: [RegionOfInterest.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/RegionOfInterest/RegionOfInterest.pde)
8694

8795
#### ImageDiff
8896

8997
Find the difference between two images in order to subtract the background or detect a new object in a scene.
9098

9199
<a href="http://www.flickr.com/photos/unavoidablegrain/8640005799/" title="Screen Shot 2013-04-11 at 2.10.35 PM by atduskgreg, on Flickr"><img src="http://farm9.staticflickr.com/8114/8640005799_44b48e01ae.jpg" width="500" height="409" alt="Screen Shot 2013-04-11 at 2.10.35 PM"></a>
92100

93-
Code: [ImageDiff.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/ImageDiff/ImageDiff.pde)
101+
Code: [ImageDiff.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/ImageDiff/ImageDiff.pde)
94102

95103
#### DilationAndErosion
96104

97105
Thin (erode) and expand (dilate) an image in order to close holes. These are known as "morphological" operations.
98106

99107
<a href="http://www.flickr.com/photos/unavoidablegrain/9075875005/" title="dilation and erosion by atduskgreg, on Flickr"><img src="http://farm3.staticflickr.com/2818/9075875005_8f7cde3ed7.jpg" width="496" height="500" alt="dilation and erosion"></a>
100108

101-
Code: [DilationAndErosion.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/DilationAndErosion/DilationAndErosion.pde)
109+
Code: [DilationAndErosion.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/DilationAndErosion/DilationAndErosion.pde)
102110

103111
#### BackgroundSubtraction
104112

105113
Detect moving objects in a scene. Use background subtraction to distinguish background from foreground and contour tracking to track the foreground objects.
106114

107115
<a href="http://www.flickr.com/photos/unavoidablegrain/9220336868/" title="Background Subtraction by atduskgreg, on Flickr"><img src="http://farm8.staticflickr.com/7292/9220336868_bed3498528.jpg" width="500" height="369" alt="Background Subtraction"></a>
108116

109-
Code: [BackgroundSubtraction.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/BackgroundSubtraction/BackgroundSubtraction.pde)
117+
Code: [BackgroundSubtraction.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/BackgroundSubtraction/BackgroundSubtraction.pde)
110118

111119

112120
#### WorkingWithColorImages
@@ -115,62 +123,62 @@ Demonstration of what you can do color images in OpenCV (threshold, blur, etc) a
115123

116124
<a href="http://www.flickr.com/photos/unavoidablegrain/9136033334/" title="color operations: threshold and blur by atduskgreg, on Flickr"><img src="http://farm6.staticflickr.com/5451/9136033334_3345dfa057.jpg" width="500" height="358" alt="color operations: threshold and blur"></a>
117125

118-
Code: [WorkingWithColorImages.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/WorkingWithColorImages/WorkingWithColorImages.pde)
126+
Code: [WorkingWithColorImages.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/WorkingWithColorImages/WorkingWithColorImages.pde)
119127

120128
#### ColorChannels ####
121129

122130
Separate a color image into red, green, blue or hue, saturation, and value channels in order to work with the channels individually.
123131

124132
<a href="http://www.flickr.com/photos/unavoidablegrain/9246157901/" title="ColorChannels by atduskgreg, on Flickr"><img src="http://farm3.staticflickr.com/2847/9246157901_08ccf19e7d.jpg" width="488" height="500" alt="ColorChannels"></a>
125133

126-
Code: [ColorChannels](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/ColorChannels/ColorChannels.pde)
134+
Code: [ColorChannels](https://github.com/atduskgreg/opencv-processing/blob/master/examples/ColorChannels/ColorChannels.pde)
127135

128136
#### FindHistogram
129137

130138
Demonstrates use of the findHistogram() function and the Histogram class to get and draw histograms for grayscale and individual color channels.
131139

132140
<a href="http://www.flickr.com/photos/unavoidablegrain/9174190443/" title="gray, red, green, blue histograms by atduskgreg, on Flickr"><img src="http://farm8.staticflickr.com/7287/9174190443_224a740ce8.jpg" width="500" height="355" alt="gray, red, green, blue histograms"></a>
133141

134-
Code: [FindHistogram.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/FindHistogram/FindHistogram.pde)
142+
Code: [FindHistogram.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/FindHistogram/FindHistogram.pde)
135143

136144
#### HueRangeSelection
137145

138146
Detect objects based on their color. Demonstrates the use of HSV color space as well as range-based image filtering.
139147

140148
<a href="http://www.flickr.com/photos/unavoidablegrain/9193745547/" title="Hue-based color detection by atduskgreg, on Flickr"><img src="http://farm4.staticflickr.com/3799/9193745547_8f09e55a39.jpg" width="500" height="397" alt="Hue-based color detection"></a>
141149

142-
Code: [HueRangeSelection.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/HueRangeSelection/HueRangeSelection.pde)
150+
Code: [HueRangeSelection.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/HueRangeSelection/HueRangeSelection.pde)
143151

144152
#### CalibrationDemo (in progress)
145153

146154
An example of the process involved in calibrating a camera. Currently only detects the corners in a chessboard pattern.
147155

148156
<a href="http://www.flickr.com/photos/unavoidablegrain/8706849024/" title="Screen Shot 2013-05-04 at 2.03.23 AM by atduskgreg, on Flickr"><img src="http://farm9.staticflickr.com/8267/8706849024_f2d938ec51.jpg" width="500" height="382" alt="Screen Shot 2013-05-04 at 2.03.23 AM"></a>
149157

150-
Code: [CalibrationDemo.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/CalibrationDemo/CalibrationDemo.pde)
158+
Code: [CalibrationDemo.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/CalibrationDemo/CalibrationDemo.pde)
151159

152160
#### HistogramSkinDetection
153161

154162
A more advanced example. Detecting skin in an image based on colors in a region of color space. Warning: uses un-wrapped OpenCV objects and functions.
155163

156164
<a href="http://www.flickr.com/photos/unavoidablegrain/8707167599/" title="Screen Shot 2013-05-04 at 2.25.18 PM by atduskgreg, on Flickr"><img src="http://farm9.staticflickr.com/8135/8707167599_d38fbdfe30.jpg" width="500" height="171" alt="Screen Shot 2013-05-04 at 2.25.18 PM"></a>
157165

158-
Code: [HistogramSkinDetection.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/HistogramSkinDetection/HistogramSkinDetection.pde)
166+
Code: [HistogramSkinDetection.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/HistogramSkinDetection/HistogramSkinDetection.pde)
159167

160168
#### DepthFromStereo
161169

162170
An advanced example. Calculates depth information from a pair of stereo images. Warning: uses un-wrapped OpenCV objects and functions.
163171

164172
<a href="http://www.flickr.com/photos/unavoidablegrain/8642493130/" title="Screen Shot 2013-04-12 at 2.27.30 AM by atduskgreg, on Flickr"><img src="http://farm9.staticflickr.com/8260/8642493130_f99dd76f3d.jpg" width="500" height="404" alt="Screen Shot 2013-04-12 at 2.27.30 AM"></a>
165173

166-
Code: [DepthFromStereo.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/DepthFromStereo/DepthFromStereo.pde)
174+
Code: [DepthFromStereo.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/DepthFromStereo/DepthFromStereo.pde)
167175

168176
#### MarkerDetection
169177

170178
An in-depth advanced example. Detect a CV marker in an image, warp perspective, and detect the number stored in the marker. Many steps in the code. Uses many un-wrapped OpenCV objects and functions.
171179

172180
<a href="http://www.flickr.com/photos/unavoidablegrain/8642309968/" title="Screen Shot 2013-04-12 at 12.20.17 AM by atduskgreg, on Flickr"><img src="http://farm9.staticflickr.com/8522/8642309968_257e397db2.jpg" width="500" height="225" alt="Screen Shot 2013-04-12 at 12.20.17 AM"></a>
173181

174-
Code: [MarkerDetection.pde](https://github.com/atduskgreg/OpenCVPro/blob/master/examples/MarkerDetection/MarkerDetection.pde)
182+
Code: [MarkerDetection.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/MarkerDetection/MarkerDetection.pde)
175183

176184

resources/build.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ source.repository=https://github.com/atduskgreg/OpenCVPro
149149
# This is used to compare different versions of the same library, and check if
150150
# an update is available.
151151

152-
library.version=5
152+
library.version=6
153153

154154

155155
# The version as the user will see it.
156156

157-
library.prettyVersion=0.4
157+
library.prettyVersion=0.4.1
158158

159159

160160
library.copyright=(c) 2013

src/gab/opencv/Histogram.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Histogram {
77
private Mat mat;
88
private PApplet parent;
99

10-
Histogram(PApplet parent, Mat mat){
10+
public Histogram(PApplet parent, Mat mat){
1111
this.mat = mat;
1212
this.parent = parent;
1313
}

src/gab/opencv/Line.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package gab.opencv;
2+
3+
import processing.core.*;
4+
5+
public class Line {
6+
public PVector start, end;
7+
public double angle;
8+
public double x1, y1, x2, y2;
9+
10+
public Line(double x1, double y1, double x2, double y2){
11+
this.x1 = x1;
12+
this.y1 = y1;
13+
this.x2 = x2;
14+
this.y2 = y2;
15+
16+
start = new PVector((float)x1, (float)y1);
17+
end = new PVector((float)x2, (float)y2);
18+
19+
// measure the angle between this line
20+
// and a vertical line oriented up
21+
angle = angleBetween(x1, y1, x2, y2, 0, 0, 0, -1);
22+
}
23+
24+
public double angleFrom(Line other){
25+
return angleBetween(x1, y1, x2, y2, other.x1, other.y1, other.x2, other.y2);
26+
}
27+
28+
29+
public static double angleBetween(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
30+
double angle1 = Math.atan2(y1 - y2, x1 - x2);
31+
double angle2 = Math.atan2(y3 - y4, x3 - x4);
32+
return angle1-angle2;
33+
}
34+
}

src/gab/opencv/OpenCV.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import gab.opencv.Contour;
3333
import gab.opencv.ContourComparator;
3434
import gab.opencv.Histogram;
35+
import gab.opencv.Line;
3536

3637
import java.awt.Rectangle;
3738

@@ -520,6 +521,19 @@ public ArrayList<Contour> findContours(){
520521
return findContours(true, false);
521522
}
522523

524+
public ArrayList<Line> findLines(int threshold, double minLineLength, double maxLineGap){
525+
ArrayList<Line> result = new ArrayList<Line>();
526+
527+
Mat lineMat = new Mat();
528+
Imgproc.HoughLinesP(getCurrentMat(), lineMat, 1, PConstants.PI/180.0, threshold, minLineLength, maxLineGap);
529+
for (int i = 0; i < lineMat.width(); i++) {
530+
double[] coords = lineMat.get(0, i);
531+
result.add(new Line(coords[0], coords[1], coords[2], coords[3]));
532+
}
533+
534+
return result;
535+
}
536+
523537
public ArrayList<Contour> findContours(boolean findHoles, boolean sort){
524538
ArrayList<Contour> result = new ArrayList<Contour>();
525539

0 commit comments

Comments
 (0)