diff --git a/README.md b/README.md index 9230e8b..02c39dc 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/detect-shapes.js b/examples/detect-shapes.js new file mode 100755 index 0000000..1fa49c2 --- /dev/null +++ b/examples/detect-shapes.js @@ -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'); +}); diff --git a/examples/quad-crosses.js b/examples/quad-crosses.js new file mode 100755 index 0000000..3632f9f --- /dev/null +++ b/examples/quad-crosses.js @@ -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'); +}); diff --git a/examples/quads.jpg b/examples/quads.jpg new file mode 100644 index 0000000..f19ae3a Binary files /dev/null and b/examples/quads.jpg differ diff --git a/examples/shapes.jpg b/examples/shapes.jpg new file mode 100644 index 0000000..7271ed8 Binary files /dev/null and b/examples/shapes.jpg differ