Skip to content

Commit 13767c6

Browse files
committed
Working on undistortion. Currently at the experimental example phase.
1 parent 1b6a5a0 commit 13767c6

5 files changed

Lines changed: 105 additions & 15 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import gab.opencv.*;
2+
import org.opencv.imgproc.Imgproc;
3+
import org.opencv.core.MatOfPoint2f;
4+
import org.opencv.core.Point;
5+
import org.opencv.core.Size;
6+
7+
import org.opencv.core.Mat;
8+
import org.opencv.core.CvType;
9+
10+
11+
OpenCV opencv;
12+
PImage src;
13+
PImage card;
14+
int cardWidth = 250;
15+
int cardHeight = 350;
16+
17+
Contour contour;
18+
19+
void setup() {
20+
src = loadImage("cards.png");
21+
size(src.width + cardWidth, src.height);
22+
opencv = new OpenCV(this, src);
23+
24+
opencv.blur(1);
25+
opencv.threshold(120);
26+
27+
contour = opencv.findContours(false, true).get(0).getPolygonApproximation();
28+
29+
card = createImage(cardWidth, cardHeight, ARGB);
30+
opencv.toPImage(warpPerspective(contour.getPoints(), cardWidth, cardHeight), card);
31+
}
32+
33+
Mat getPerspectiveTransformation(ArrayList<PVector> inputPoints, int w, int h) {
34+
Point[] canonicalPoints = new Point[4];
35+
canonicalPoints[0] = new Point(w, 0);
36+
canonicalPoints[1] = new Point(0, 0);
37+
canonicalPoints[2] = new Point(0, h);
38+
canonicalPoints[3] = new Point(w, h);
39+
40+
MatOfPoint2f canonicalMarker = new MatOfPoint2f();
41+
canonicalMarker.fromArray(canonicalPoints);
42+
43+
Point[] points = new Point[4];
44+
for (int i = 0; i < 4; i++) {
45+
points[i] = new Point(inputPoints.get(i).x, inputPoints.get(i).y);
46+
}
47+
MatOfPoint2f marker = new MatOfPoint2f(points);
48+
return Imgproc.getPerspectiveTransform(marker, canonicalMarker);
49+
}
50+
51+
Mat warpPerspective(ArrayList<PVector> inputPoints, int w, int h) {
52+
Mat transform = getPerspectiveTransformation(inputPoints, w, h);
53+
Mat unWarpedMarker = new Mat(w, h, CvType.CV_8UC1);
54+
Imgproc.warpPerspective(opencv.getColor(), unWarpedMarker, transform, new Size(w, h));
55+
return unWarpedMarker;
56+
}
57+
58+
59+
void draw() {
60+
image(src, 0, 0);
61+
noFill();
62+
stroke(0, 255, 0);
63+
strokeWeight(4);
64+
contour.draw();
65+
fill(255, 0);
66+
ArrayList<PVector> points = contour.getPoints();
67+
for (int i = 0; i < points.size(); i++) {
68+
text(i, points.get(i).x, points.get(i).y);
69+
}
70+
71+
pushMatrix();
72+
translate(src.width, 0);
73+
image(card, 0, 0);
74+
popMatrix();
75+
}
76+

examples/WarpPerspective/cards.png

845 KB
Loading

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ An advanced example. Calculates depth information from a pair of stereo images.
173173

174174
Code: [DepthFromStereo.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/DepthFromStereo/DepthFromStereo.pde)
175175

176+
#### WarpPerspective (in progress)
177+
178+
Un-distort an object that's in perspective. Coming to the real API soon.
179+
180+
<a href="http://www.flickr.com/photos/unavoidablegrain/9279197332/" title="Warp Perspective by atduskgreg, on Flickr"><img src="http://farm3.staticflickr.com/2861/9279197332_ca6beb3760.jpg" width="500" height="416" alt="Warp Perspective"></a>
181+
182+
Code: [WarpPerspective.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/WarpPerspective/WarpPerspective.pde)
183+
176184
#### MarkerDetection
177185

178186
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.

src/gab/opencv/Contour.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void draw(){
7373
for (PVector p : points) {
7474
parent.vertex(p.x, p.y);
7575
}
76-
parent.endShape();
76+
parent.endShape(PConstants.CLOSE);
7777

7878
}
7979

src/gab/opencv/OpenCV.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,17 @@ public void loadCascade(String cascadeFileName){
326326
// localize path to cascade file to point at the library's data folder
327327
String relativePath = "data/" + cascadeFileName;
328328
String jarPath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
329-
329+
330330
String[] parts = jarPath.split("/");
331331

332332
String cascadePath = "";
333333
for(int i = 0; i < parts.length-2; i++){
334334
cascadePath += parts[i] + "/";
335335
}
336336
cascadePath += relativePath;
337-
337+
338+
PApplet.println("Load cascade from: " + cascadePath);
339+
338340
classifier = new CascadeClassifier(cascadePath);
339341

340342
if(classifier.empty()){
@@ -521,18 +523,7 @@ public ArrayList<Contour> findContours(){
521523
return findContours(true, false);
522524
}
523525

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-
}
526+
536527

537528
public ArrayList<Contour> findContours(boolean findHoles, boolean sort){
538529
ArrayList<Contour> result = new ArrayList<Contour>();
@@ -556,6 +547,21 @@ public ArrayList<Contour> findContours(boolean findHoles, boolean sort){
556547
return result;
557548
}
558549

550+
public ArrayList<Line> findLines(int threshold, double minLineLength, double maxLineGap){
551+
ArrayList<Line> result = new ArrayList<Line>();
552+
553+
Mat lineMat = new Mat();
554+
Imgproc.HoughLinesP(getCurrentMat(), lineMat, 1, PConstants.PI/180.0, threshold, minLineLength, maxLineGap);
555+
for (int i = 0; i < lineMat.width(); i++) {
556+
double[] coords = lineMat.get(0, i);
557+
result.add(new Line(coords[0], coords[1], coords[2], coords[3]));
558+
}
559+
560+
return result;
561+
}
562+
563+
564+
559565
public ArrayList<PVector> findChessboardCorners(int patternWidth, int patternHeight){
560566
MatOfPoint2f corners = new MatOfPoint2f();
561567
Calib3d.findChessboardCorners(getCurrentMat(), new Size(patternWidth,patternHeight), corners);

0 commit comments

Comments
 (0)