-
Notifications
You must be signed in to change notification settings - Fork 0
/
contourDetector.js
60 lines (40 loc) · 1.22 KB
/
contourDetector.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
53
54
55
56
57
58
59
60
var cv = require('opencv');
function detect(image, name) {
var lowThresh = 50;
var highThresh = 125;
var maxArcLength = 100;
var GREEN = [0, 255, 0]; // B, G, R
var WHITE = [255, 255, 255]; // B, G, R
var RED = [0, 0, 255]; // B, G, R
var dotCount = 0;
return new Promise(function (resolve, reject){
cv.readImage(image, function(err, im) {
if (err) reject(err);
var width = im.width();
var height = im.height();
if (width < 1 || height < 1) throw 'Image has no size';
nw = 1000;
nh = (height / width) * nw;
im.resize(nw,nh);
counted = im.copy();
// im.convertGrayscale();
im.cvtColor('CV_BGR2GRAY');
im_canny = im.copy();
im_canny.gaussianBlur([11,11]);
im_canny.canny(lowThresh, highThresh);
contours = im_canny.findContours();
dotCount = 0;
// only draw and count found contours within range of deinfed length
for(i = 0; i < contours.size(); i++) {
if(contours.arcLength(i, true) < maxArcLength) {
counted.drawContour(contours, i, GREEN, 3);
dotCount += 1;
}
}
counted.save('./uploads/modified/contour_' + name);
console.log('Image saved');
resolve(dotCount);
});
});
}
module.exports = { detect };