-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcannyEdgeDetector.cpp
287 lines (254 loc) · 10.3 KB
/
cannyEdgeDetector.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
#include "cannyEdgeDetector.h"
using namespace cv;
using namespace std;
Mat CannyEdgeDetector::convolve2D(Mat img, vector<vector<double>>& kernel) {
int krows = kernel.size(), kcols = kernel[0].size();
int irows = img.rows, icols = img.cols;
//Mat img_pad(img.rows + krows, img.cols + kcols, 0.0f);
vector<vector<double>> img_pad(irows + krows, vector<double>(icols + kcols, 0));
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
img_pad[i + krows / 2][j + kcols / 2] = img.at<double>(i, j);
}
}
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
double val = 0;
for (int kx = 0; kx < krows; kx++) {
for (int ky = 0; ky < kcols; ky++) {
val += img_pad[i + kx][j + ky] * kernel[kx][ky];
}
}
img.at<double>(i, j) = val;
}
}
return img;
}
void CannyEdgeDetector::fast_sobel(Mat img, Mat& G, Mat& grad) {
//int krows = 1, kcols = kernelX[0].size();
int irows = img.rows, icols = img.cols;
/*Mat G(irows + 3, icols + 3, 0.0f);
Mat grad(irows + 3, icols + 3, 0.0f);*/
//vector<vector<double>> img_pad(irows + 3, vector<double>(icols + 3, 0));
double maxMagnitude = 0;
for (int i = 1; i < irows - 1; i++) {
for (int j = 1; j < icols - 1; j++) {
//if (i < 1 || j < 1 || (i + 1) == irows || (j + 1) == icols) continue;
double gx = -img.at<double>(i - 1, j - 1) +
img.at<double>(i + 1, j - 1) +
img.at<double>(i - 1, j) * (-2.0f) +
img.at<double>(i + 1, j) * (2.0f) -
img.at<double>(i - 1, j + 1) +
img.at<double>(i + 1, j + 1);
double gy = -img.at<double>(i - 1, j - 1) +
img.at<double>(i, j - 1) * (-2.0f) -
img.at<double>(i + 1, j - 1) +
img.at<double>(i - 1, j + 1) +
img.at<double>(i, j + 1) * (2.0f) +
img.at<double>(i + 1, j + 1);
G.at<double>(i, j) = min(255.0f, max(0.0f, sqrtf(gx * gx + gy * gy)));
grad.at<double>(i, j) = atan2(gy, gx);
maxMagnitude = max(maxMagnitude, G.at<double>(i, j));
}
}
for (int i = 0; i < irows; i++) {
for (int j = 0; j < icols; j++) {
G.at<double>(i, j) *= 255 / maxMagnitude;
}
}
}
vector<vector<double>> CannyEdgeDetector::convolve2D(vector<vector<double>> img, vector<vector<double>>& kernel) {
int krows = kernel.size(), kcols = kernel[0].size();
int irows = img.size(), icols = img[0].size();
vector<vector<double>> img_pad(irows + krows, vector<double>(icols + kcols, 0));
for (int i = 0; i < irows; i++) {
for (int j = 0; j < icols; j++) {
img_pad[i + krows / 2][j + kcols / 2] = img[i][j];
}
}
for (int i = 0; i < irows; i++) {
for (int j = 0; j < icols; j++) {
double val = 0;
for (int kx = 0; kx < krows; kx++) {
for (int ky = 0; ky < kcols; ky++) {
val += img_pad[i + kx][j + ky] * kernel[kx][ky];
}
}
img[i][j] = val;
}
}
return img;
}
vector<vector<double>> CannyEdgeDetector::GaussianKernel(double sigma, int size) {
vector<vector<double>> kernel(size, vector<double>(size, 0));
size = size / 2;
for (int x = -size; x <= size; x++) {
for (int y = -size; y <= size; y++) {
double val = exp(-(x * x + y * y) / (2 * sigma * sigma)) / double(2 * M_PI * sigma * sigma);
kernel[x + size][y + size] = val;
}
}
return kernel;
}
vector<int> CannyEdgeDetector::boxesForGauss(float sigma, int n) // standard deviation, number of boxes
{
auto wIdeal = sqrt((12 * sigma * sigma / n) + 1); // Ideal averaging filter width
int wl = floor(wIdeal);
if (wl % 2 == 0)
wl--;
int wu = wl + 2;
auto mIdeal = (12 * sigma * sigma - n * wl * wl - 4 * n * wl - 3 * n) / (-4 * wl - 4);
int m = round(mIdeal);
// var sigmaActual = Math.sqrt( (m*wl*wl + (n-m)*wu*wu - n)/12 );
vector<int> sizes(n);
for (auto i = 0; i < n; i++)
sizes[i] = i < m ? wl : wu;
return sizes;
}
void CannyEdgeDetector::boxBlurH_4(Mat& scl, Mat& tcl, int w, int h, int r) {
float iarr = 1.f / (r + r + 1);
for (auto i = 0; i < h; i++) {
auto ti = i * w;
auto li = ti, ri = ti + r;
//auto fv = scl[ti], lv = scl[ti + w - 1];
auto fv = scl.at<double>(ti / w, ti % w);
auto lv = scl.at<double>(ti / w, w - 1);
auto val = (r + 1) * fv;
for (auto j = 0; j < r; j++) val += scl.at<double>((ti + j) / w, (ti + j) % w);
for (auto j = 0; j <= r; j++) { val += scl.at<double>(ri / w, ri % w) - fv; tcl.at<double>(ti / w, ti % w) = round(val * iarr); ri++, ti++; }
for (auto j = r + 1; j < w - r; j++) { val += scl.at<double>(ri / w, ri % w) - scl.at<double>(li / w, li % w); tcl.at<double>(ti / w, ti % w) = round(val * iarr); ri++, li++, ti++; }
for (auto j = w - r; j < w; j++) { val += lv - scl.at<double>(li / w, li % w); tcl.at<double>(ti / w, ti % w) = round(val * iarr); li++, ti++; }
}
}
void CannyEdgeDetector::boxBlurT_4(Mat& scl, Mat& tcl, int w, int h, int r) {
float iarr = 1.f / (r + r + 1);
for (auto i = 0; i < w; i++) {
auto ti = i, li = ti, ri = ti + r * w;
auto fv = scl.at<double>(ti / w, ti % w), lv = scl.at<double>((ti + w * (h - 1)) / w, (ti + w * (h - 1)) % w);
auto val = (r + 1) * fv;
for (auto j = 0; j < r; j++) val += scl.at<double>((ti + j * w) / w, (ti + j * w) % w);
for (auto j = 0; j <= r; j++) { val += scl.at<double>(ri / w, ri % w) - fv; tcl.at<double>(ti / w, ti % w) = round(val * iarr); ri += w; ti += w; }
for (auto j = r + 1; j < h - r; j++) { val += scl.at<double>(ri / w, ri % w) - scl.at<double>(li / w, li % w); tcl.at<double>(ti / w, ti % w) = round(val * iarr); li += w; ri += w; ti += w; }
for (auto j = h - r; j < h; j++) { val += lv - scl.at<double>(li / w, li % w); tcl.at<double>(ti / w, ti % w) = round(val * iarr); li += w; ti += w; }
}
}
void CannyEdgeDetector::boxBlur_4(Mat& scl, Mat& tcl, int w, int h, int r) {
for (auto i = 0; i < scl.rows; i++)
for (auto j = 0; j < scl.cols; j++)
tcl.at<double>(i, j) = scl.at<double>(i, j);
boxBlurH_4(tcl, scl, w, h, r);
boxBlurT_4(scl, tcl, w, h, r);
}
void CannyEdgeDetector::gaussBlur_4(Mat& scl, Mat& tcl, int w, int h, int r) {
auto bxs = boxesForGauss(r, 3);
boxBlur_4(scl, tcl, w, h, (bxs[0] - 1) / 2);
boxBlur_4(tcl, scl, w, h, (bxs[1] - 1) / 2);
boxBlur_4(scl, tcl, w, h, (bxs[2] - 1) / 2);
}
Mat CannyEdgeDetector::LowPass(Mat source, int w, int h, int radius)
{
Mat lowpass = source.clone(); // copy constructor
Mat target(source.rows, source.cols, CV_64FC1, 0.0f);
gaussBlur_4(lowpass, target, w, h, radius);
return target;
}
void CannyEdgeDetector::sobel(Mat& img, Mat& magnitude, Mat& gradient) {
vector<vector<double>> Kx = { {-1, 0, 1},{-2, 0, 2}, {-1, 0, 1} };
vector<vector<double>> Ky = { {-1, -2, -1}, {0, 0, 0}, {1, 2, 1 } };
Mat Ix = convolve2D(img.clone(), Kx);
Mat Iy = convolve2D(img.clone(), Ky);
double maxMagnitude = 0;
for (int i = 0; i < Ix.rows; i++) {
for (int j = 0; j < Ix.cols; j++) {
magnitude.at<double>(i, j) = sqrt(Ix.at<double>(i, j) * Ix.at<double>(i, j) + Iy.at<double>(i, j) * Iy.at<double>(i, j));
gradient.at<double>(i, j) = atan2(Iy.at<double>(i, j), Ix.at<double>(i, j)) * 180 / M_PI;
}
}
for (int i = 0; i < Ix.rows; i++) {
for (int j = 0; j < Ix.cols; j++) {
maxMagnitude = max(maxMagnitude, magnitude.at<double>(i, j));
}
}
for (int i = 0; i < Ix.rows; i++) {
for (int j = 0; j < Ix.cols; j++) {
magnitude.at<double>(i, j) *= 255 / maxMagnitude;
}
}
}
Mat CannyEdgeDetector::nms(Mat magnitude, Mat gradient) {
Mat mag = magnitude.clone(), grad = gradient.clone();
Mat Z(mag.rows, mag.cols, CV_64FC1);
for (int i = 2; i < mag.rows - 2; i++) {
for (int j = 2; j < mag.cols - 2; j++) {
double q = 0, r = 0;
if (grad.at<double>(i, j) < 0) grad.at<double>(i, j) += 180;
double thisAngle = grad.at<double>(i, j);
try {
if (((thisAngle < 22.5) && (thisAngle > -22.5)) || (thisAngle > 157.5) && (thisAngle < 180)) {
q = mag.at<double>(i + 1, j - 1);
r = mag.at<double>(i - 1, j + 1);
}
if ((thisAngle >= 22.5) && (thisAngle < 67.5)) {
q = mag.at<double>(i + 1, j);
r = mag.at<double>(i - 1, j);
}
if (((thisAngle >= 112.5) && (thisAngle < 157.5))) {
q = mag.at<double>(i - 1, j - 1);
r = mag.at<double>(i + 1, j + 1);
}
if ((mag.at<double>(i, j) >= q) && (mag.at<double>(i, j) >= r)) Z.at<double>(i, j) = mag.at<double>(i, j);
else Z.at<double>(i, j) = 0;
}
catch (...) {
}
}
}
return Z;
}
Mat CannyEdgeDetector::hysteresis(Mat image, double lowRatio, double highRatio) {
assert(highRatio != 0);
Mat img = image.clone();
int M = img.rows, N = img.cols;
double highThreshold = 255 * highRatio;
double lowThreshold = highThreshold * lowRatio;
double weak = 75;
double strong = 255;
Mat res(M, N, CV_64FC1, 0.0f);
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (img.at<double>(i, j) >= highThreshold) { res.at<double>(i, j) = 255; }
else if (img.at<double>(i, j) < lowThreshold) { res.at<double>(i, j) = 0; }
else { res.at<double>(i, j) = weak; }
}
}
for (int i = 3; i < M - 3; i++) {
for (int j = 3; j < N - 3; j++) {
try {
if ((res.at<double>(i + 1, j - 1) == strong) || (res.at<double>(i + 1, j) == strong) || (res.at<double>(i + 1, j + 1) == strong)
|| (res.at<double>(i, j - 1) == strong) || (res.at<double>(i, j + 1) == strong) || (res.at<double>(i - 1, j - 1) == strong)
|| (res.at<double>(i - 1, j) == strong) || (res.at<double>(i - 1, j + 1) == strong))
{
img.at<double>(i, j) = strong;
}
else img.at<double>(i, j) = 0;
}
catch (...) {}
}
}
return img;
}
Mat CannyEdgeDetector::detect(Mat image, int kernel, float lowRatio, float highRatio) {
assert(kernel != 0); //check kernel size is not 0
assert(highRatio != 0); //check highratio is not 0
Mat img = image.clone(); //clone image to new variable to avoid changing original input
if (image.channels() > 1) cvtColor(image, image, COLOR_BGR2GRAY); //convert image to grayscale
image.convertTo(image, CV_64FC1); //change format from unsigned int to double for future calculations
Mat smooth_image = LowPass(image, image.cols, image.rows, kernel); //apply approximate gaussian blur
/*Mat smooth_image = convolve2D(image.clone(), kernel);*/ //apply true gaussian blur
Mat magnitude(image.rows + 3, image.cols + 3, CV_64FC1, 0.0f);
Mat gradient(image.rows + 3, image.cols + 3, CV_64FC1, 0.0f);
fast_sobel(smooth_image, magnitude, gradient); //apply fast sobel filter on blurred image and return magnitude and gradient
Mat Z = nms(magnitude, gradient); // apply non-max suppression to reduce thickness of edges
Mat res = hysteresis(Z, lowRatio, highRatio); //make edges coontinuous
return res;
}