Skip to content

Commit 79d124d

Browse files
committed
Contour class, findContour function and contour finding example
1 parent 269ba2a commit 79d124d

30 files changed

Lines changed: 579 additions & 53 deletions

.DS_Store

0 Bytes
Binary file not shown.
250 KB
Binary file not shown.
250 KB
Binary file not shown.

distribution/OpenCVPro-1/examples/BackgroundSubtraction/BackgroundSubtraction.pde

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,29 @@ PImage before, after, colorDiff, grayDiff;
77
void setup() {
88
before = loadImage("before.jpg");
99
after = loadImage("after.jpg");
10-
size(before.width, before.height);
10+
size(before.width, before.height, P2D);
1111

1212
beforeCv = new OpenCVPro(this, before);
1313
afterCv = new OpenCVPro(this, after);
14-
14+
1515
// NOTE: important to not do the color diff
1616
// before calling these, as this makes
1717
// the gray buffer from the current color buffer.
1818
beforeCv.gray();
1919
afterCv.gray();
20+
2021

2122
OpenCVPro.diff(beforeCv.getBufferColor(), afterCv.getBufferColor());
2223
OpenCVPro.diff(beforeCv.getBufferGray(), afterCv.getBufferGray());
2324

25+
colorDiff = beforeCv.getColorImage();
2426

2527
// FIXME: same mysterious problem with getOutputImage()
26-
colorDiff = createImage(before.width, before.height, RGB);
27-
beforeCv.toPImage(beforeCv.getBufferColor(), colorDiff);
28-
29-
grayDiff = createImage(before.width, before.height, RGB);
30-
beforeCv.toPImage(beforeCv.getBufferGray(), grayDiff);
28+
// colorDiff = createImage(before.width, before.height, RGB);
29+
// beforeCv.toPImage(beforeCv.getBufferColor(), colorDiff);
30+
// colorDiff = beforeCv.getColorImage();
31+
32+
grayDiff = beforeCv.getGrayImage();
3133
}
3234

3335
void draw() {
@@ -38,11 +40,11 @@ void draw() {
3840
image(colorDiff, 0, before.height);
3941
image(grayDiff, before.width, before.height);
4042
popMatrix();
41-
43+
4244
fill(255);
4345
text("before", 10, 20);
4446
text("after", before.width/2 +10, 20);
4547
text("color diff", 10, before.height/2+ 20);
4648
text("gray diff", before.width/2 + 10, before.height/2+ 20);
47-
4849
}
50+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import gab.opencvpro.*;
2+
3+
import org.opencv.imgproc.Imgproc;
4+
import org.opencv.core.Core;
5+
import org.opencv.core.Mat;
6+
7+
import org.opencv.core.MatOfPoint;
8+
import org.opencv.core.Point;
9+
10+
PImage src, dst;
11+
OpenCVPro opencv;
12+
13+
ArrayList<Contour> contours;
14+
15+
void setup() {
16+
src = loadImage("test.jpg");
17+
size(src.width, src.height/2, P2D);
18+
opencv = new OpenCVPro(this, src);
19+
20+
opencv.gray();
21+
opencv.threshold(50);
22+
23+
contours = findContours(opencv.getBufferGray());
24+
dst = opencv.getGrayImage();
25+
26+
println("numContours: " + contours.size());
27+
}
28+
29+
void draw() {
30+
scale(0.5);
31+
image(src, 0, 0);
32+
image(dst, src.width, 0);
33+
34+
noFill();
35+
stroke(255);
36+
for(Contour contour : contours){
37+
if(contour.points.size() > 20 && contour.points.size() < 1000){
38+
contour.draw();
39+
}
40+
}
41+
}
42+
43+
void drawContour(Contour c){
44+
void draw(){
45+
beginShape();
46+
for(PVector point : c.points){
47+
vertex(point.x, point.y);
48+
}
49+
endShape();
50+
}
51+
}
52+
53+
// find contours works on a binary image,
54+
// so you have to threshold it (or otherwise make it binary) first
55+
// in order to get a good result
56+
ArrayList<Contour> findContours(Mat buffer){
57+
ArrayList<MatOfPoint> cs = new ArrayList<MatOfPoint>();
58+
Imgproc.findContours(buffer, cs, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);
59+
60+
ArrayList<Contour> result = new ArrayList<Contour>();
61+
for(MatOfPoint c : cs){
62+
result.add(new Contour(c));
63+
}
64+
65+
66+
return result;
67+
}
99.8 KB
Loading

distribution/OpenCVPro-1/examples/FindEdges/FindEdges.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PImage src, canny, scharr, sobel;
55

66
void setup() {
77
src = loadImage("test.jpg");
8-
size(src.width, src.height);
8+
size(src.width, src.height, P2D);
99

1010
cannyFilter = new OpenCVPro(this, src);
1111
scharrFilter = new OpenCVPro(this, src);
@@ -14,7 +14,7 @@ void setup() {
1414
sobelFilter.gray();
1515

1616
cannyFilter.findCannyEdges(20, 75);
17-
scharrFilter.findScharrY();
17+
scharrFilter.findScharrX(); // also try: scharrFilter.findScharrY()
1818
sobelFilter.findSobelEdges(1,0);
1919

2020
canny = cannyFilter.getColorImage();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import gab.opencvpro.*;
2+
import org.opencv.core.Core;
3+
4+
import org.opencv.core.Mat;
5+
import org.opencv.core.Size;
6+
import org.opencv.core.Point;
7+
import org.opencv.core.Scalar;
8+
import org.opencv.core.CvType;
9+
10+
import org.opencv.imgproc.Imgproc;
11+
12+
13+
OpenCVPro opencv;
14+
PImage src,dst, hist, histMask;
15+
16+
Mat skinHistogram;
17+
18+
void setup(){
19+
src = loadImage("test.jpg");
20+
src.resize(src.width/2, 0);
21+
size(src.width*2 + 256, src.height, P2D);
22+
opencv = new OpenCVPro(this, src);
23+
24+
skinHistogram = Mat.zeros(256, 256, CvType.CV_8UC1);
25+
Core.ellipse(skinHistogram, new Point(113.0, 155.6), new Size(40.0, 25.2), 43.0, 0.0, 360.0, new Scalar(255, 255, 255), Core.FILLED);
26+
27+
histMask = createImage(256,256, ARGB);
28+
opencv.toPImage(skinHistogram, histMask);
29+
hist = loadImage("cb-cr.png");
30+
hist.blend(histMask, 0,0,256,256,0,0,256,256, ADD);
31+
32+
dst = opencv.getColorImage();
33+
dst.loadPixels();
34+
35+
for(int i = 0; i < dst.pixels.length; i++){
36+
37+
Mat input = new Mat(new Size(1, 1), CvType.CV_8UC3);
38+
input.setTo(colorToScalar(dst.pixels[i]));
39+
Mat output = opencv.imitate(input);
40+
Imgproc.cvtColor(input, output, Imgproc.COLOR_BGR2YCrCb );
41+
double[] inputComponents = output.get(0,0);
42+
if(skinHistogram.get((int)inputComponents[1], (int)inputComponents[2])[0] > 0){
43+
dst.pixels[i] = color(255);
44+
} else {
45+
dst.pixels[i] = color(0);
46+
}
47+
}
48+
49+
dst.updatePixels();
50+
}
51+
52+
// in BGR
53+
Scalar colorToScalar(color c){
54+
return new Scalar(blue(c), green(c), red(c));
55+
}
56+
57+
58+
void draw(){
59+
image(src,0,0);
60+
image(dst, src.width, 0);
61+
image(hist, src.width*2, 0);
62+
}
46.4 KB
Loading
99.8 KB
Loading

0 commit comments

Comments
 (0)