-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
answer_50.cpp
464 lines (380 loc) · 10.5 KB
/
answer_50.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>
// BGR -> Gray
cv::Mat BGR2GRAY(cv::Mat img){
// get height and width
int width = img.cols;
int height = img.rows;
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
// each y, x
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// BGR -> Gray
out.at<uchar>(y, x) = 0.2126 * (float)img.at<cv::Vec3b>(y, x)[2] \
+ 0.7152 * (float)img.at<cv::Vec3b>(y, x)[1] \
+ 0.0722 * (float)img.at<cv::Vec3b>(y, x)[0];
}
}
return out;
}
float clip(float value, float min, float max){
return fmin(fmax(value, 0), 255);
}
// gaussian filter
cv::Mat gaussian_filter(cv::Mat img, double sigma, int kernel_size){
int height = img.rows;
int width = img.cols;
int channel = img.channels();
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);
if (channel == 1) {
out = cv::Mat::zeros(height, width, CV_8UC1);
}
// prepare kernel
int pad = floor(kernel_size / 2);
int _x = 0, _y = 0;
double kernel_sum = 0;
// get gaussian kernel
float kernel[kernel_size][kernel_size];
for (int y = 0; y < kernel_size; y++){
for (int x = 0; x < kernel_size; x++){
_y = y - pad;
_x = x - pad;
kernel[y][x] = 1 / (2 * M_PI * sigma * sigma) * exp( - (_x * _x + _y * _y) / (2 * sigma * sigma));
kernel_sum += kernel[y][x];
}
}
for (int y = 0; y < kernel_size; y++){
for (int x = 0; x < kernel_size; x++){
kernel[y][x] /= kernel_sum;
}
}
// filtering
double v = 0;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// for BGR
if (channel == 3){
for (int c = 0; c < channel; c++){
v = 0;
for (int dy = -pad; dy < pad + 1; dy++){
for (int dx = -pad; dx < pad + 1; dx++){
if (((x + dx) >= 0) && ((y + dy) >= 0) && ((x + dx) < width) && ((y + dy) < height)){
v += (double)img.at<cv::Vec3b>(y + dy, x + dx)[c] * kernel[dy + pad][dx + pad];
}
}
}
out.at<cv::Vec3b>(y, x)[c] = (uchar)clip(v, 0, 255);
}
} else {
// for Gray
v = 0;
for (int dy = -pad; dy < pad + 1; dy++){
for (int dx = -pad; dx < pad + 1; dx++){
if (((x + dx) >= 0) && ((y + dy) >= 0) && ((x + dx) < width) && ((y + dy) < height)){
v += (double)img.at<uchar>(y + dy, x + dx) * kernel[dy + pad][dx + pad];
}
}
}
out.at<uchar>(y, x) = (uchar)clip(v, 0, 255);
}
}
}
return out;
}
// Sobel filter
cv::Mat sobel_filter(cv::Mat img, int kernel_size, bool horizontal){
int height = img.rows;
int width = img.cols;
int channel = img.channels();
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
// prepare kernel
double kernel[kernel_size][kernel_size] = {{1, 2, 1}, {0, 0, 0}, {-1, -2, -1}};
if (horizontal){
kernel[0][1] = 0;
kernel[0][2] = -1;
kernel[1][0] = 2;
kernel[1][2] = -2;
kernel[2][0] = 1;
kernel[2][1] = 0;
}
int pad = floor(kernel_size / 2);
double v = 0;
// filtering
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
v = 0;
for (int dy = -pad; dy < pad + 1; dy++){
for (int dx = -pad; dx < pad + 1; dx++){
if (((y + dy) >= 0) && (( x + dx) >= 0) && ((y + dy) < height) && ((x + dx) < width)){
v += (double)img.at<uchar>(y + dy, x + dx) * kernel[dy + pad][dx + pad];
}
}
}
out.at<uchar>(y, x) = (uchar)clip(v, 0, 255);
}
}
return out;
}
// get edge
cv::Mat get_edge(cv::Mat fx, cv::Mat fy){
// get height and width
int height = fx.rows;
int width = fx.cols;
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
double _fx, _fy;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
_fx = (double)fx.at<uchar>(y, x);
_fy = (double)fy.at<uchar>(y, x);
out.at<uchar>(y, x) = (uchar)clip(sqrt(_fx * _fx + _fy * _fy), 0, 255);
}
}
return out;
}
// get angle
cv::Mat get_angle(cv::Mat fx, cv::Mat fy){
// get height and width
int height = fx.rows;
int width = fx.cols;
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
double _fx, _fy;
double angle;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
_fx = fmax((double)fx.at<uchar>(y, x), 0.000001);
_fy = (double)fy.at<uchar>(y, x);
angle = atan2(_fy, _fx);
angle = angle / M_PI * 180;
if(angle < -22.5){
angle = 180 + angle;
} else if (angle >= 157.5) {
angle = angle - 180;
}
//std::cout << angle << " " ;
// quantization
if (angle <= 22.5){
out.at<uchar>(y, x) = 0;
} else if (angle <= 67.5){
out.at<uchar>(y, x) = 45;
} else if (angle <= 112.5){
out.at<uchar>(y, x) = 90;
} else {
out.at<uchar>(y, x) = 135;
}
}
}
return out;
}
// non maximum suppression
cv::Mat non_maximum_suppression(cv::Mat angle, cv::Mat edge){
int height = angle.rows;
int width = angle.cols;
int channel = angle.channels();
int dx1, dx2, dy1, dy2;
int now_angle;
// prepare output
cv::Mat _edge = cv::Mat::zeros(height, width, CV_8UC1);
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
now_angle = angle.at<uchar>(y, x);
// angle condition
if (now_angle == 0){
dx1 = -1;
dy1 = 0;
dx2 = 1;
dy2 = 0;
} else if(now_angle == 45) {
dx1 = -1;
dy1 = 1;
dx2 = 1;
dy2 = -1;
} else if(now_angle == 90){
dx1 = 0;
dy1 = -1;
dx2 = 0;
dy2 = 1;
} else {
dx1 = -1;
dy1 = -1;
dx2 = 1;
dy2 = 1;
}
if (x == 0){
dx1 = fmax(dx1, 0);
dx2 = fmax(dx2, 0);
}
if (x == (width - 1)){
dx1 = fmin(dx1, 0);
dx2 = fmin(dx2, 0);
}
if (y == 0){
dy1 = fmax(dy1, 0);
dy2 = fmax(dy2, 0);
}
if (y == (height - 1)){
dy1 = fmin(dy1, 0);
dy2 = fmin(dy2, 0);
}
// if pixel is max among adjuscent pixels, pixel is kept
if (fmax(fmax(edge.at<uchar>(y, x), edge.at<uchar>(y + dy1, x + dx1)), edge.at<uchar>(y + dy2, x + dx2)) == edge.at<uchar>(y, x)) {
_edge.at<uchar>(y, x) = edge.at<uchar>(y, x);
}
}
}
return _edge;
}
// histerisis
cv::Mat histerisis(cv::Mat edge, int HT, int LT){
int height = edge.rows;
int width = edge.cols;
int channle = edge.channels();
// prepare output
cv::Mat _edge = cv::Mat::zeros(height, width, CV_8UC1);
int now_pixel;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// get pixel
now_pixel = edge.at<uchar>(y, x);
// if pixel >= HT
if (now_pixel >= HT){
_edge.at<uchar>(y, x) = 255;
}
// if LT < pixel < HT
else if (now_pixel > LT) {
for (int dy = -1; dy < 2; dy++){
for (int dx = -1; dx < 2; dx++){
// if 8 nearest neighbor pixel >= HT
if (edge.at<uchar>(fmin(fmax(y + dy, 0), 255), fmin(fmax(x + dx, 0), 255)) >= HT){
_edge.at<uchar>(y, x) = 255;
}
}
}
}
}
}
return _edge;
}
// Canny
cv::Mat Canny(cv::Mat img){
// BGR -> Gray
cv::Mat gray = BGR2GRAY(img);
// gaussian filter
cv::Mat gaussian = gaussian_filter(gray, 1.4, 5);
// sobel filter (vertical)
cv::Mat fy = sobel_filter(gaussian, 3, false);
// sobel filter (horizontal)
cv::Mat fx = sobel_filter(gaussian, 3, true);
// get edge
cv::Mat edge = get_edge(fx, fy);
// get angle
cv::Mat angle = get_angle(fx, fy);
// edge non-maximum suppression
edge = non_maximum_suppression(angle, edge);
// histerisis
edge = histerisis(edge, 50, 20);
return edge;
}
// Morphology Erode
cv::Mat Morphology_Erode(cv::Mat img, int Erode_time){
int height = img.cols;
int width = img.rows;
// output image
cv::Mat tmp_img;
cv::Mat out = img.clone();
// for erode time
for (int i = 0; i < Erode_time; i++){
tmp_img = out.clone();
// each pixel
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// check left pixel
if ((x > 0) && (tmp_img.at<uchar>(y, x - 1) == 255)){
out.at<uchar>(y, x) = 255;
continue;
}
// check up pixel
if ((y > 0) && (tmp_img.at<uchar>(y - 1, x) == 255)){
out.at<uchar>(y, x) = 255;
continue;
}
// check right pixel
if ((x < width - 1) && (tmp_img.at<uchar>(y, x + 1) == 255)){
out.at<uchar>(y, x) = 255;
continue;
}
// check left pixel
if ((y < height - 1) && (tmp_img.at<uchar>(y + 1, x) == 255)){
out.at<uchar>(y, x) = 255;
continue;
}
}
}
}
return out;
}
// Morphology Dilate
cv::Mat Morphology_Dilate(cv::Mat img, int Dilate_time){
int height = img.cols;
int width = img.rows;
// output image
cv::Mat tmp_img;
cv::Mat out = img.clone();
// for erode time
for (int i = 0; i < Dilate_time; i++){
tmp_img = out.clone();
// each pixel
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// check left pixel
if ((x > 0) && (tmp_img.at<uchar>(y, x - 1) == 0)){
out.at<uchar>(y, x) = 0;
continue;
}
// check up pixel
if ((y > 0) && (tmp_img.at<uchar>(y - 1, x) == 0)){
out.at<uchar>(y, x) = 0;
continue;
}
// check right pixel
if ((x < width - 1) && (tmp_img.at<uchar>(y, x + 1) == 0)){
out.at<uchar>(y, x) = 0;
continue;
}
// check left pixel
if ((y < height - 1) && (tmp_img.at<uchar>(y + 1, x) == 0)){
out.at<uchar>(y, x) = 0;
continue;
}
}
}
}
return out;
}
// Morphology closing
cv::Mat Morphology_Closing(cv::Mat img, int open_time){
// Morphology erode
img = Morphology_Erode(img, open_time);
// Morphology dilate
img = Morphology_Dilate(img, open_time);
return img;
}
int main(int argc, const char* argv[]){
// read image
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);
// Canny
cv::Mat edge = Canny(img);
// Morphology Opening
cv::Mat out = Morphology_Closing(edge, 1);
//cv::imwrite("out.jpg", out);
cv::imshow("sample", out);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}