-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
answer_42.cpp
318 lines (262 loc) · 7.35 KB
/
answer_42.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
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>
// RGB to Gray scale
cv::Mat BGR2GRAY(cv::Mat img){
// get height and width
int height = img.rows;
int width = img.cols;
int channel = img.channels();
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
// BGR -> Gray
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
out.at<uchar>(y, x) = (int)((float)img.at<cv::Vec3b>(y, x)[0] * 0.0722 + \
(float)img.at<cv::Vec3b>(y, x)[1] * 0.7152 + \
(float)img.at<cv::Vec3b>(y, x)[2] * 0.2126);
}
}
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;
}
// Canny step 2
int Canny_step2(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);
//cv::imwrite("out.jpg", out);
cv::imshow("answer(edge)", edge);
cv::imshow("answer(angle)", angle);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
int main(int argc, const char* argv[]){
// read image
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);
// Canny step 2
Canny_step2(img);
return 0;
}