-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathED.cpp
More file actions
316 lines (271 loc) · 8.89 KB
/
ED.cpp
File metadata and controls
316 lines (271 loc) · 8.89 KB
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
/**
* @brief: core of ED
* @author: hongxinliu <github.com/hongxinliu> <hongxinliu.com>
* @date: Mar. 05, 2018
*/
#include "ED.h"
#define GAUSS_SIZE (5)
#define GAUSS_SIGMA (1.0)
#define SOBEL_ORDER (1)
#define SOBEL_SIZE (3)
int ED::detectEdges(const cv::Mat &image,
std::vector<std::list<cv::Point>> &edges,
const int proposal_thresh,
const int anchor_interval,
const int anchor_thresh)
{
// 0.preparation
cv::Mat gray;
if(image.empty())
{
std::cout<<"Empty image input!"<<std::endl;
return -1;
}
if(image.type() == CV_8UC1)
gray = image.clone();
else if(image.type() == CV_8UC3)
cv::cvtColor(image, gray, CV_BGR2GRAY);
else
{
std::cout<<"Unknow image type!"<<std::endl;
return -2;
}
// 1.Gauss blur
cv::GaussianBlur(gray, gray, cv::Size(GAUSS_SIZE, GAUSS_SIZE), GAUSS_SIGMA, GAUSS_SIGMA);
// 2.get gradient magnitude and orientation
cv::Mat M, O;
getGradient(gray, M, O);
// 3.get anchors
std::vector<cv::Point> anchors;
getAnchors(M, O, proposal_thresh, anchor_interval, anchor_thresh, anchors);
// 4.trace edges from anchors
cv::Mat status(gray.rows, gray.cols, CV_8UC1, cv::Scalar(STATUS_UNKNOWN)); //Init all status to STATUS_UNKNOWN
edges.clear();
for(const auto &anchor : anchors)
traceFromAnchor(M, O, proposal_thresh, anchor, status, edges);
return int(edges.size());
}
void ED::getGradient(const cv::Mat &gray,
cv::Mat &M,
cv::Mat &O)
{
cv::Mat Gx, Gy;
cv::Sobel(gray, Gx, CV_16SC1, SOBEL_ORDER, 0, SOBEL_SIZE);
cv::Sobel(gray, Gy, CV_16SC1, 0, SOBEL_ORDER, SOBEL_SIZE);
M.create(gray.rows, gray.cols, CV_16SC1);
O.create(gray.rows, gray.cols, CV_8UC1);
for(int r=0; r<gray.rows; ++r)
{
for(int c=0; c<gray.cols; ++c)
{
short dx = abs(Gx.at<short>(r, c));
short dy = abs(Gy.at<short>(r, c));
M.at<short>(r, c) = dx + dy;
O.at<uchar>(r, c) = (dx > dy ? EDGE_VER : EDGE_HOR);
}
}
}
void ED::getAnchors(const cv::Mat &M,
const cv::Mat &O,
const int proposal_thresh,
const int anchor_interval,
const int anchor_thresh,
std::vector<cv::Point> &anchors)
{
anchors.clear();
for(int r = 1; r < M.rows - 1; r += anchor_interval)
{
for(int c = 1; c < M.cols - 1; c += anchor_interval)
{
// ignore non-proposal pixels
if(M.at<short>(r, c) < proposal_thresh)
continue;
// horizontal edge
if(O.at<uchar>(r, c) == EDGE_HOR)
{
if(M.at<short>(r, c) - M.at<short>(r-1, c) >= anchor_thresh &&
M.at<short>(r, c) - M.at<short>(r+1, c) >= anchor_thresh)
anchors.emplace_back(c, r);
}
// vertical edge
else
{
if(M.at<short>(r, c) - M.at<short>(r, c-1) >= anchor_thresh &&
M.at<short>(r, c) - M.at<short>(r, c+1) >= anchor_thresh)
anchors.emplace_back(c, r);
}
}
}
}
void ED::traceFromAnchor(const cv::Mat &M,
const cv::Mat &O,
const int proposal_thresh,
const cv::Point &anchor,
cv::Mat &status,
std::vector<std::list<cv::Point>> &edges)
{
// if this anchor point has already been visited
if(status.at<uchar>(anchor.y, anchor.x) != STATUS_UNKNOWN)
return;
std::list<cv::Point> edge;
cv::Point pt_last;
TRACE_DIR dir_last;
// if horizontal edge, go left and right
if(O.at<uchar>(anchor.y, anchor.x) == EDGE_HOR)
{
// go left first
// sssume the last visited point is the right hand side point and TRACE_LEFT to current point, the same below
pt_last = cv::Point(anchor.x + 1, anchor.y);
dir_last = TRACE_LEFT;
trace(M, O, proposal_thresh, pt_last, anchor, dir_last, false, status, edge);
// reset anchor point
// it has already been set in the previous traceEdge(), reset it to satisfy the initial while condition, the same below */
status.at<uchar>(anchor.y, anchor.x) = STATUS_UNKNOWN;
// go right then
pt_last = cv::Point(anchor.x - 1, anchor.y);
dir_last = TRACE_RIGHT;
trace(M, O, proposal_thresh, pt_last, anchor, dir_last, true, status, edge);
}
// vertical edge, go up and down
else
{
// go up first
pt_last = cv::Point(anchor.x, anchor.y + 1);
dir_last = TRACE_UP;
trace(M, O, proposal_thresh, pt_last, anchor, dir_last, false, status, edge);
// reset anchor point
status.at<uchar>(anchor.y, anchor.x) = STATUS_UNKNOWN;
// go down then
pt_last = cv::Point(anchor.x, anchor.y - 1);
dir_last = TRACE_DOWN;
trace(M, O, proposal_thresh, pt_last, anchor, dir_last, true, status, edge);
}
edges.push_back(edge);
}
void ED::trace(const cv::Mat &M,
const cv::Mat &O,
const int proposal_thresh,
cv::Point pt_last,
cv::Point pt_cur,
TRACE_DIR dir_last,
bool push_back,
cv::Mat &status,
std::list<cv::Point> &edge)
{
// current direction
TRACE_DIR dir_cur;
// repeat until reaches the visited pixel or non-proposal
while (true)
{
// terminate trace if that point has already been visited
if(status.at<uchar>(pt_cur.y, pt_cur.x) != STATUS_UNKNOWN)
break;
// set it to background and terminate trace if that point is not a proposal edge
if(M.at<short>(pt_cur.y, pt_cur.x) < proposal_thresh)
{
status.at<uchar>(pt_cur.y, pt_cur.x) = STATUS_BACKGROUND;
break;
}
// set point pt_cur as edge
status.at<uchar>(pt_cur.y, pt_cur.x) = STATUS_EDGE;
if (push_back)
edge.push_back(pt_cur);
else
edge.push_front(pt_cur);
// if its direction is EDGE_HOR, trace left or right
if (O.at<uchar>(pt_cur.y, pt_cur.x) == EDGE_HOR)
{
// calculate trace direction
if (dir_last == TRACE_UP || dir_last == TRACE_DOWN)
{
if (pt_cur.x < pt_last.x)
dir_cur = TRACE_LEFT;
else
dir_cur = TRACE_RIGHT;
}
else
dir_cur = dir_last;
// update last state
pt_last = pt_cur;
dir_last = dir_cur;
// go left
if (dir_cur == TRACE_LEFT)
{
auto leftTop = M.at<short>(pt_cur.y - 1, pt_cur.x - 1);
auto left = M.at<short>(pt_cur.y, pt_cur.x - 1);
auto leftBottom = M.at<short>(pt_cur.y + 1, pt_cur.x - 1);
if (leftTop >= left && leftTop >= leftBottom)
pt_cur = cv::Point(pt_cur.x - 1, pt_cur.y - 1);
else if (leftBottom >= left && leftBottom >= leftTop)
pt_cur = cv::Point(pt_cur.x - 1, pt_cur.y + 1);
else
pt_cur.x -= 1;
// break if reaches the border of image, the same below
if (pt_cur.x == 0 || pt_cur.y == 0 || pt_cur.y == M.rows - 1)
break;
}
// go right
else
{
auto rightTop = M.at<short>(pt_cur.y - 1, pt_cur.x + 1);
auto right = M.at<short>(pt_cur.y, pt_cur.x + 1);
auto rightBottom = M.at<short>(pt_cur.y + 1, pt_cur.x + 1);
if (rightTop >= right && rightTop >= rightBottom)
pt_cur = cv::Point(pt_cur.x + 1, pt_cur.y - 1);
else if (rightBottom >= right && rightBottom >= rightTop)
pt_cur = cv::Point(pt_cur.x + 1, pt_cur.y + 1);
else
pt_cur.x += 1;
if (pt_cur.x == M.cols - 1 || pt_cur.y == 0 || pt_cur.y == M.rows - 1)
break;
}
}
// its direction is EDGE_VER, trace up or down
else
{
// calculate trace direction
if (dir_last == TRACE_LEFT || dir_last == TRACE_RIGHT)
{
if (pt_cur.y < pt_last.y)
dir_cur = TRACE_UP;
else
dir_cur = TRACE_DOWN;
}
else
dir_cur = dir_last;
// update last state
pt_last = pt_cur;
dir_last = dir_cur;
// go up
if (dir_cur == TRACE_UP)
{
auto leftTop = M.at<short>(pt_cur.y - 1, pt_cur.x - 1);
auto top = M.at<short>(pt_cur.y - 1, pt_cur.x);
auto rightTop = M.at<short>(pt_cur.y - 1, pt_cur.x + 1);
if (leftTop >= top && leftTop >= rightTop)
pt_cur = cv::Point(pt_cur.x - 1, pt_cur.y - 1);
else if (rightTop >= top && rightTop >= leftTop)
pt_cur = cv::Point(pt_cur.x + 1, pt_cur.y - 1);
else
pt_cur.y -= 1;
if (pt_cur.y == 0 || pt_cur.x == 0 || pt_cur.x == M.cols - 1)
break;
}
// go down
else
{
auto leftBottom = M.at<short>(pt_cur.y + 1, pt_cur.x - 1);
auto bottom = M.at<short>(pt_cur.y + 1, pt_cur.x);
auto rightBottom = M.at<short>(pt_cur.y + 1, pt_cur.x + 1);
if (leftBottom >= bottom && leftBottom >= rightBottom)
pt_cur = cv::Point(pt_cur.x - 1, pt_cur.y + 1);
else if (rightBottom >= bottom && rightBottom >= leftBottom)
pt_cur = cv::Point(pt_cur.x + 1, pt_cur.y + 1);
else
pt_cur.y += 1;
if (pt_cur.y == M.rows - 1 || pt_cur.x == 0 || pt_cur.x == M.cols - 1)
break;
}
}
}
}