Include more info about working with contours

Some readme examples for using functions on a Contours object
Examples detecting and playing with simple shapes and contours
This commit is contained in:
Ryan Souza 2013-02-25 22:53:12 -08:00
parent 454fd1188a
commit 521139a76e
5 changed files with 140 additions and 0 deletions

View File

@ -129,6 +129,39 @@ Also:
mat.drawContour
mat.drawAllContours
### Using Contours
`findContours` returns a `Contours` collection object, not a native array. This object provides
functions for accessing, computing with, and altering the contours contained in it.
See [relevant source code](src/Contours.cc) and [examples](examples/)
var contours = im.findContours;
# Count of contours in the Contours object
contours.size();
# Count of corners(verticies) of contour `index`
contours.cornerCount(index);
# Access vertex data of contours
for(var c = 0; c < contours.size(); ++c) {
console.log("Contour " + c);
for(var i = 0; i < contours.cornerCount(c); ++i) {
var point = contours.point(c, i);
console.log("(" + point.x + "," + point.y + ")");"
}
}
# Computations of contour `index`
contours.area(index);
contours.arcLength(index, isClosed);
contours.boundingRect(index);
contours.minAreaRect(index);
contours.isConvex(index);
# Destructively alter contour `index`
contours.approxPolyDP(index, epsilon, isClosed);
contours.convexHull(index, clockwise);
## MIT License
The library is distributed under the MIT License - if for some reason that

52
examples/detect-shapes.js Executable file
View File

@ -0,0 +1,52 @@
#!/usr/bin/env node
//
// Detects triangles and quadrilaterals
//
var cv = require('../lib/opencv');
var lowThresh = 0;
var highThresh = 100;
var nIters = 2;
var minArea = 2000;
var BLUE = [0, 255, 0]; //B, G, R
var RED = [0, 0, 255]; //B, G, R
var GREEN = [0, 255, 0]; //B, G, R
var WHITE = [255, 255, 255]; //B, G, R
cv.readImage('./shapes.jpg', function(err, im) {
var out = new cv.Matrix(im.height(), im.width());
im.convertGrayscale();
im_canny = im.copy();
im_canny.canny(lowThresh, highThresh);
im_canny.dilate(nIters);
contours = im_canny.findContours();
for(i = 0; i < contours.size(); i++) {
if(contours.area(i) < minArea) continue;
var arcLength = contours.arcLength(i, true);
contours.approxPolyDP(i, 0.01 * arcLength, true);
switch(contours.cornerCount(i)) {
case 3:
out.drawContour(contours, i, GREEN);
break;
case 4:
out.drawContour(contours, i, RED);
break;
default:
out.drawContour(contours, i, WHITE);
}
}
out.save('./out.png');
});

55
examples/quad-crosses.js Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env node
//
// Finds quadrilaterals and fills them with an X
//
var cv = require('../lib/opencv');
var lowThresh = 0;
var highThresh = 100;
var nIters = 2;
var minArea = 2000;
var maxArea = 100000;
var BLUE = [0, 255, 0]; //B, G, R
var RED = [0, 0, 255]; //B, G, R
var GREEN = [0, 255, 0]; //B, G, R
var WHITE = [255, 255, 255]; //B, G, R
cv.readImage('./quads.jpg', function(err, im) {
var out = im.copy();
im.convertGrayscale();
im_canny = im.copy();
im_canny.canny(lowThresh, highThresh);
im_canny.dilate(nIters);
contours = im_canny.findContours();
for(i = 0; i < contours.size(); i++) {
var area = contours.area(i);
if(area < minArea || area > maxArea) continue;
var arcLength = contours.arcLength(i, true);
contours.approxPolyDP(i, 0.01 * arcLength, true);
if(contours.cornerCount(i) != 4) continue;
var points = [
contours.point(i, 0),
contours.point(i, 1),
contours.point(i, 2),
contours.point(i, 3)
]
out.line([points[0].x,points[0].y], [points[2].x, points[2].y], RED);
out.line([points[1].x,points[1].y], [points[3].x, points[3].y], RED);
}
out.save('./out.png');
});

BIN
examples/quads.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

BIN
examples/shapes.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB