-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
216 lines (172 loc) · 8.77 KB
/
index.html
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<html>
<head>
<script type="text/javascript" src="opencv.js" async onload='onOpenCvReady();'></script>
</head>
<body>
<img id='src-image' style="max-width:800px" src='IMG_6254.JPG' onload="imgload();" alt=''>
<canvas id="canvas-id"></canvas>
<script>
var opencvloaded = false;
window.onOpenCvReady = function () {
function openCvReady() {
cv['onRuntimeInitialized'] = () => {
console.log("opencv loaded");
opencvloaded = true;
};
}
openCvReady();
}
var largest = -1;
var largest_contour = -1;
var largest_index = -1;
var largest_poly = -1;
function detect(c, i) {
var shape = "unidentified";
var peri = cv.arcLength(c, true);
let poly = new cv.Mat();
cv.approxPolyDP(c, poly, 0.01 * peri, true);
let rect = cv.boundingRect(c);
// if (true) {
if (poly.rows == 4) {
shape = "square";
//console.log("square!!", rect.width)
if (rect.width < 10 || rect.width > 600) {
console.log("false with width !!", rect.width)
return false
}
console.log("width: " + rect.width + " height: " + rect.height + " rows: " + poly.rows)
if (rect.width > 100 && rect.height > 100) {
if (rect.width > largest) {
largest = rect.width
largest_contour = c
largest_poly = poly
largest_index = i
console.log("largest****** ", largest)
return true
}
}
}
}
function imgload() {
if (opencvloaded == false) {
console.log("set timeout");
setTimeout(imgload, 100);
return;
}
console.log("process");
var image = cv.imread('src-image');
var dst = new cv.Mat();
var gray = new cv.Mat();
var thresh = new cv.Mat();
var blurred = new cv.Mat();
cv.cvtColor(image, gray, cv.COLOR_BGR2GRAY, 0);
let dsize = new cv.Size(5, 5);
cv.GaussianBlur(gray, blurred, dsize, 0, 0);
cv.threshold(blurred, thresh, 190, 255, cv.THRESH_BINARY)
cv.Canny(thresh, dst, 50, 100, 3, false);
let contours = new cv.MatVector();
let hierarchy = new cv.Mat();
cv.findContours(dst, contours, hierarchy, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);
console.log("hierarchy: ", hierarchy);
//var cnts = imutils.grab_contours(contours);
console.log(contours.size());
let color = new cv.Scalar(0, 255, 0);
//cv.drawContours(gray, contours, -1, color, 1, cv.LINE_8, hierarchy, 100);
for (let i = 0; i < contours.size(); ++i) {
let contour = contours.get(i);
let ret = detect(contour, i)
//console.log(ret)
if (ret) {
console.log("draw width !!!",)
let color = new cv.Scalar(0, 255, 0);
//cv.drawContours(gray, contours, i, color, 1, cv.LINE_8, hierarchy, 100);
}
}
cv.drawContours(thresh, contours, largest_index, color, 1, cv.LINE_8, hierarchy, 100);
//cv.drawContours(gray, contours, -1, color, 1, cv.LINE_8, hierarchy, 100);
//Find the corners
//foundCountour has 2 channels (seemingly x/y), has a depth of 4, and a type of 12. Seems to show it's a CV_32S "type", so the valid data is in data32S??
let foundContour = largest_poly
let corner1 = new cv.Point(foundContour.data32S[0], foundContour.data32S[1]);
let corner2 = new cv.Point(foundContour.data32S[2], foundContour.data32S[3]);
let corner3 = new cv.Point(foundContour.data32S[4], foundContour.data32S[5]);
let corner4 = new cv.Point(foundContour.data32S[6], foundContour.data32S[7]);
console.log(corner1)
console.log(corner2)
console.log(corner3)
console.log(corner4)
console.log(largest_poly)
//Order the corners
let cornerArray = [{ corner: corner1 }, { corner: corner2 }, { corner: corner3 }, { corner: corner4 }];
//Sort by Y position (to get top-down)
cornerArray.sort((item1, item2) => { return (item1.corner.y < item2.corner.y) ? -1 : (item1.corner.y > item2.corner.y) ? 1 : 0; }).slice(0, 5);
console.log(cornerArray)
//Determine left/right based on x position of top and bottom 2
let tl = cornerArray[0].corner.x < cornerArray[1].corner.x ? cornerArray[0] : cornerArray[1];
let tr = cornerArray[0].corner.x > cornerArray[1].corner.x ? cornerArray[0] : cornerArray[1];
let bl = cornerArray[2].corner.x < cornerArray[3].corner.x ? cornerArray[2] : cornerArray[3];
let br = cornerArray[2].corner.x > cornerArray[3].corner.x ? cornerArray[2] : cornerArray[3];
//Calculate the max width/height
let widthBottom = Math.hypot(br.corner.x - bl.corner.x, br.corner.y - bl.corner.y);
let widthTop = Math.hypot(tr.corner.x - tl.corner.x, tr.corner.y - tl.corner.y);
let theWidth = (widthBottom > widthTop) ? widthBottom : widthTop;
let heightRight = Math.hypot(tr.corner.x - br.corner.x, tr.corner.y - br.corner.y);
let heightLeft = Math.hypot(tl.corner.x - bl.corner.x, tr.corner.y - bl.corner.y);
let theHeight = (heightRight > heightLeft) ? heightRight : heightLeft;
//Transform!
var finalDest = new cv.Mat();
let finalDestCoords = cv.matFromArray(4, 1, cv.CV_32FC2, [0, 0, theWidth - 1, 0, theWidth - 1, theHeight - 1, 0, theHeight - 1]); //
let srcCoords = cv.matFromArray(4, 1, cv.CV_32FC2, [tl.corner.x, tl.corner.y, tr.corner.x, tr.corner.y, br.corner.x, br.corner.y, bl.corner.x, bl.corner.y]);
dsize = new cv.Size(theWidth, theHeight);
let M = cv.getPerspectiveTransform(srcCoords, finalDestCoords)
cv.warpPerspective(thresh, finalDest, M, dsize, cv.INTER_LINEAR, cv.BORDER_CONSTANT, new cv.Scalar());
dsize = new cv.Size(200, 200);
// You can try more different parameters
cv.resize(finalDest, finalDest, dsize, 0, 0, cv.INTER_AREA);
let rect = new cv.Rect(20, 20, 140, 140);
finalDest = finalDest.roi(rect);
dsize = new cv.Size(300, 300);
// You can try more different parameters
cv.resize(finalDest, finalDest, dsize, 0, 0, cv.INTER_AREA);
const imgDiff = new cv.Mat();
cv.absdiff(finalDest, finalDest, imgDiff);
console.log(imgDiff)
dsize = new cv.Size(5, 5);
color = new cv.Scalar(255, 126, 126);
cv.GaussianBlur(finalDest, blurred, dsize, 0, 0);
cv.threshold(blurred, thresh, 190, 255, cv.THRESH_BINARY)
cv.Canny(thresh, dst, 50, 100, 3, false);
//cv.imshow('canvas-id', dst);
cv.findContours(dst, contours, hierarchy, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE);
console.log(contours.size())
cv.drawContours(imgDiff, contours, -1, color, 1, cv.LINE_8, hierarchy, 100);
let cnt = contours.get(0);
rect = cv.boundingRect(cnt);
console.log(rect)
finalDest = finalDest.roi(rect);
let point1 = new cv.Point(rect.x, rect.y);
let point2 = new cv.Point(rect.x + rect.width, rect.y + rect.height);
cv.rectangle(imgDiff, point1, point2, color, 2, cv.LINE_AA, 0);
cv.imshow('canvas-id', finalDest);
}
function color1(image, contours, i) {
console.log(image.cols);
let mask = cv.Mat.zeros(image.rows, image.cols, cv.CV_8U);
let mean = new cv.Mat;
let std = new cv.Mat;
//let t1 = new cv.Mat();
//cv.meanStdDev(image, mean, std, mask);
//console.log("mask std: ", mean);
let c = new cv.Scalar(255, 255, 255);
cv.drawContours(mask, contours, i, c, -1);
let M = cv.Mat.ones(5, 5, cv.CV_8U);
let anchor = new cv.Point(-1, -1);
cv.erode(mask, mask, M, anchor, 2);
let result = cv.mean(image, mask);
//console.log("result: ", result);
//console.log("mean: ", mean.data);
cv.imshow('canvas-id', mask);
}
</script>
</body>
</html>