forked from peterbraden/node-opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect-shapes.js
executable file
·52 lines (37 loc) · 1008 Bytes
/
detect-shapes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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');
});