-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageFilter.js
114 lines (104 loc) Β· 3.11 KB
/
imageFilter.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class ImageFilter {
constructor(cv) {
this.cv = cv;
}
getOpenCV() {
return this.cv;
}
dilation(src, size) {
let dst = new cv.Mat();
let M = cv.Mat.ones(size, size, cv.CV_8U);
let anchor = new cv.Point(-1, -1);
this.cv.dilate(src, dst, M, anchor, 1, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
src.delete();
return dst;
}
medianBlur(src, size) {
let dst = new cv.Mat();
this.cv.medianBlur(src, dst, size);
src.delete();
return dst;
}
erosion(src, size) {
let dst = new this.cv.Mat();
let M = this.cv.Mat.ones(size, size, this.cv.CV_8U);
let anchor = new this.cv.Point(-1, -1);
this.cv.erode(src, dst, M, anchor, 1, this.cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
src.delete();
return dst;
}
gaussianBlur(src, size) {
let dst = new cv.Mat();
let ksize = new cv.Size(size, size);
this.cv.GaussianBlur(src, dst, ksize, 0, 0, cv.BORDER_DEFAULT);
src.delete();
return dst;
}
enclosingCircleMaxOne(src, size) {
let contours = new cv.MatVector();
let hierarchy = new cv.Mat();
//this.cv.findContours(src, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_TC89_L1);
this.cv.findContours(src, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_TC89_KCOS);
this.cv.cvtColor(src, src, cv.COLOR_GRAY2RGBA, 0);
var count = 0;
var maxCircle = null;
for (var i = 0; i < contours.size(); i++) {
let cnt = contours.get(i);
let circle = cv.minEnclosingCircle(cnt);
if (maxCircle == null) {
maxCircle = circle;
}
if (circle.radius >= size &&
circle.radius >= maxCircle.radius) {
maxCircle = circle;
count++;
}
cnt.delete();
}
contours.delete();
hierarchy.delete();
return count == 0 ? [] : [{
x: maxCircle.center.x,
y: maxCircle.center.y,
radius: maxCircle.radius
}];
}
enclosingCircle(src, size) {
let contours = new cv.MatVector();
let hierarchy = new cv.Mat();
//this.cv.findContours(src, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_TC89_L1);
this.cv.findContours(src, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_TC89_KCOS);
this.cv.cvtColor(src, src, cv.COLOR_GRAY2RGBA, 0);
var objList = [];
var count = 0;
for (var i = 0; i < contours.size(); i++) {
let cnt = contours.get(i);
let circle = cv.minEnclosingCircle(cnt);
if (size == 0 || circle.radius > size) {
count++;
objList.push({
x: circle.center.x,
y: circle.center.y,
radius: circle.radius
});
}
cnt.delete();
}
if (count > 0) {
//console.log("count:", count, contours.size());
}
contours.delete();
hierarchy.delete();
return objList;
}
findContours(src) {
let contours = new cv.MatVector();
let hierarchy = new cv.Mat();
this.cv.findContours(src, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_TC89_KCOS);
var size = contours.size();
contours.delete();
hierarchy.delete();
return size;
}
}
cv.imgFilter = new ImageFilter(cv);