Skip to content

Commit ae9372b

Browse files
committed
convex hull implementation for Contour
1 parent 5dafc9c commit ae9372b

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

src/gab/opencv/Contour.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import org.opencv.core.MatOfPoint2f;
88
import org.opencv.imgproc.Imgproc;
99
import org.opencv.core.Rect;
10-
10+
import org.opencv.core.Mat;
11+
import org.opencv.core.MatOfInt;
1112
import org.opencv.core.Point;
1213

1314
import java.awt.Rectangle;
@@ -17,29 +18,29 @@ public class Contour {
1718
private ArrayList<PVector> points;
1819
private Point[] inputPoints;
1920
private double polygonApproximationFactor;
20-
PApplet parent;
21-
Rectangle boundingBox;
21+
private PApplet parent;
22+
private Rectangle boundingBox;
23+
public MatOfPoint pointMat;
2224

2325
public Contour(PApplet parent, MatOfPoint mat){
2426
polygonApproximationFactor = mat.size().height * 0.01;
2527
this.parent = parent;
28+
this.pointMat = mat;
2629

2730
Rect r = Imgproc.boundingRect(mat);
2831
boundingBox = new Rectangle(r.x, r.y, r.width, r.height);
29-
3032
loadPoints(mat.toArray());
3133
}
3234

3335
public Contour(PApplet parent, MatOfPoint2f mat){
3436
polygonApproximationFactor = mat.size().height * 0.01;
3537
this.parent = parent;
38+
this.pointMat = new MatOfPoint(mat.toArray());
3639

3740
Rect r = Imgproc.minAreaRect(mat).boundingRect();
3841
boundingBox = new Rectangle(r.x, r.y, r.width, r.height);
39-
4042
loadPoints(mat.toArray());
4143
}
42-
4344

4445
public void loadPoints(Point[] pts){
4546
points = new ArrayList<PVector>();
@@ -68,6 +69,25 @@ public Contour getPolygonApproximation(){
6869
return new Contour(parent, approx);
6970
}
7071

72+
public Contour getConvexHull(){
73+
74+
MatOfInt hull = new MatOfInt();
75+
MatOfPoint points = new MatOfPoint(pointMat);
76+
77+
78+
Imgproc.convexHull(points, hull);
79+
Point[] hp = new Point[hull.height()];
80+
81+
for(int i = 0; i < hull.height(); i++){
82+
int index = (int)hull.get(i,0)[0];
83+
hp[i] = new Point(pointMat.get(index,0));
84+
}
85+
MatOfPoint hullPoints = new MatOfPoint();
86+
hullPoints.fromArray(hp);
87+
88+
return new Contour(parent, hullPoints);
89+
}
90+
7191
public void draw(){
7292
parent.beginShape();
7393
for (PVector p : points) {

src/gab/opencv/OpenCV.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public class OpenCV {
109109

110110
private boolean nativeLoaded;
111111

112-
CascadeClassifier classifier;
112+
public CascadeClassifier classifier;
113113
BackgroundSubtractorMOG backgroundSubtractor;
114114

115115
public final static String VERSION = "##library.prettyVersion##";
@@ -477,7 +477,7 @@ public PVector min(){
477477
return OpenCV.pointToPVector(r.minLoc);
478478
}
479479

480-
private static PVector pointToPVector(Point p){
480+
public static PVector pointToPVector(Point p){
481481
return new PVector((float)p.x, (float)p.y);
482482
}
483483

0 commit comments

Comments
 (0)