This repository is currently being migrated. It's locked while the migration is in progress.
forked from CihanTopal/ED_Lib
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEDPF.cpp
272 lines (230 loc) · 7.64 KB
/
EDPF.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
#include "EDPF.h"
using namespace cv;
using namespace std;
EDPF::EDPF(const int _width, const int _height) : ED(_width, _height) { prealloc(); }
/*
EDPF::EDPF(Mat srcImage) : ED(srcImage, PREWITT_OPERATOR, 11, 3)
{
// Validate Edge Segments
const auto start_tick = getTickCount();
sigma /= 2.5;
GaussianBlur(srcImage, smoothImage, Size(), sigma); // calculate kernel from sigma
const auto gaussian_blur_tick = getTickCount();
lastEDPFProfile.gaussian_blur = (gaussian_blur_tick - start_tick) / getTickFrequency();
validateEdgeSegments();
const auto validate_edge_segments_tick = getTickCount();
lastEDPFProfile.validate_edge_segments = (validate_edge_segments_tick - gaussian_blur_tick) /
getTickFrequency();
}
*/
EDPF::EDPF(Mat srcImage) : ED(srcImage.cols, srcImage.rows)
{
prealloc();
process(srcImage);
}
EDPF::EDPF(ED obj) : ED(obj)
{
// Validate Edge Segments
sigma /= 2.5;
GaussianBlur(srcImage, smoothImage, Size(), sigma); // calculate kernel from sigma
validateEdgeSegments();
}
EDPF::EDPF(EDColor obj) : ED(obj) {}
void EDPF::prealloc() { H.reserve(MAX_GRAD_VALUE); }
void EDPF::process(cv::Mat _srcImage)
{
ED::process(_srcImage, PREWITT_OPERATOR, 11, 3);
// Validate Edge Segments
const auto start_tick = getTickCount();
sigma /= 2.5;
GaussianBlur(srcImage, smoothImage, Size(), sigma); // calculate kernel from sigma
const auto gaussian_blur_tick = getTickCount();
lastEDPFProfile.gaussian_blur = (gaussian_blur_tick - start_tick) / getTickFrequency();
validateEdgeSegments();
const auto validate_edge_segments_tick = getTickCount();
lastEDPFProfile.validate_edge_segments =
(validate_edge_segments_tick - gaussian_blur_tick) / getTickFrequency();
}
void EDPF::validateEdgeSegments()
{
divForTestSegment = 2.25; // Some magic number :-)
memset(edgeImg, 0, width * height); // clear edge image
ComputePrewitt3x3();
// Compute np: # of segment pieces
#if 1
// Does this underestimate the number of pieces of edge segments?
// What's the correct value?
np = 0;
for (int i = 0; i < getSegmentNo(); i++)
{
int len = segmentPoints[i].size();
np += (len * (len - 1)) / 2;
} // end-for
// np *= 32;
#elif 0
// This definitely overestimates the number of pieces of edge segments
int np = 0;
for (int i = 0; i < getSegmentNo(); i++)
{
np += segmentPoints[i].size();
} // end-for
np = (np * (np - 1)) / 2;
#endif
// Validate segments
for (int i = 0; i < getSegmentNo(); i++)
{
TestSegment(i, 0, segmentPoints[i].size() - 1);
} // end-for
ExtractNewSegments();
}
void EDPF::ComputePrewitt3x3()
{
const cv::Mat kernel = (cv::Mat_<int>(3, 3) << -1, -1, -1, 0, 0, 0, 1, 1, 1);
cv::Mat &gxImage = buffer0;
cv::Mat &gyImage = buffer1;
cv::filter2D(srcImage, gxImage, CV_16SC1, kernel.t());
cv::filter2D(srcImage, gyImage, CV_16SC1, kernel);
cv::absdiff(gxImage, cv::Scalar::all(0), gxImage); // gxImage = cv::abs(gxImage)
cv::absdiff(gyImage, cv::Scalar::all(0), gyImage);
cv::add(gxImage, gyImage, gradImage);
gradImage.col(0).setTo(0);
gradImage.col(gradImage.cols - 1).setTo(0);
gradImage.row(0).setTo(0);
gradImage.row(gradImage.rows - 1).setTo(0);
gradImg = (short *)gradImage.data;
double max_grad_value = static_cast<double>(MAX_GRAD_VALUE);
cv::minMaxLoc(gradImage, nullptr, &max_grad_value);
grads.clear();
grads.resize(static_cast<int>(max_grad_value) + 1);
std::fill(grads.begin(), grads.end(), 0);
for (int i = 0; i < gradImage.total(); ++i)
{
grads[gradImg[i]]++;
}
// Compute probability function H
const int size = (width - 2) * (height - 2);
for (int i = grads.size() - 1; i > 0; i--) grads[i - 1] += grads[i];
H.clear();
H.resize(grads.size(), 0);
for (int i = 0; i < grads.size(); i++) H[i] = (double)grads[i] / ((double)size);
}
//----------------------------------------------------------------------------------
// Resursive validation using half of the pixels as suggested by DMM algorithm
// We take pixels at Nyquist distance, i.e., 2 (as suggested by DMM)
//
void EDPF::TestSegment(int i, int index1, int index2)
{
int chainLen = index2 - index1 + 1;
if (chainLen < minPathLen) return;
// Test from index1 to index2. If OK, then we are done. Otherwise, split into two and
// recursively test the left & right halves
// First find the min. gradient along the segment
int minGrad = 1 << 30;
int minGradIndex;
for (int k = index1; k <= index2; k++)
{
int r = segmentPoints[i][k].y;
int c = segmentPoints[i][k].x;
if (gradImg[r * width + c] < minGrad)
{
minGrad = gradImg[r * width + c];
minGradIndex = k;
}
} // end-for
// Compute nfa
double nfa = NFA(H[minGrad], (int)(chainLen / divForTestSegment));
if (nfa <= EPSILON)
{
for (int k = index1; k <= index2; k++)
{
int r = segmentPoints[i][k].y;
int c = segmentPoints[i][k].x;
edgeImg[r * width + c] = 255;
} // end-for
return;
} // end-if
// Split into two halves. We divide at the point where the gradient is the minimum
int end = minGradIndex - 1;
while (end > index1)
{
int r = segmentPoints[i][end].y;
int c = segmentPoints[i][end].x;
if (gradImg[r * width + c] <= minGrad)
end--;
else
break;
} // end-while
int start = minGradIndex + 1;
while (start < index2)
{
int r = segmentPoints[i][start].y;
int c = segmentPoints[i][start].x;
if (gradImg[r * width + c] <= minGrad)
start++;
else
break;
} // end-while
TestSegment(i, index1, end);
TestSegment(i, start, index2);
}
//----------------------------------------------------------------------------------------------
// After the validation of the edge segments, extracts the valid ones
// In other words, updates the valid segments' pixel arrays and their lengths
//
void EDPF::ExtractNewSegments()
{
// vector<Point> *segments = &segmentPoints[getSegmentNo()];
vector<vector<Point> > validSegments;
int noSegments = 0;
for (int i = 0; i < getSegmentNo(); i++)
{
int start = 0;
while (start < segmentPoints[i].size())
{
while (start < segmentPoints[i].size())
{
int r = segmentPoints[i][start].y;
int c = segmentPoints[i][start].x;
if (edgeImg[r * width + c]) break;
start++;
} // end-while
int end = start + 1;
while (end < segmentPoints[i].size())
{
int r = segmentPoints[i][end].y;
int c = segmentPoints[i][end].x;
if (edgeImg[r * width + c] == 0) break;
end++;
} // end-while
int len = end - start;
if (len >= 10)
{
// A new segment. Accepted only only long enough (whatever that means)
// segments[noSegments].pixels = &map->segments[i].pixels[start];
// segments[noSegments].noPixels = len;
std::vector<cv::Point> subVec = takePointVectorFromPool();
subVec.insert(subVec.begin(), &segmentPoints[i][start], &segmentPoints[i][end - 1]);
validSegments.push_back(subVec);
noSegments++;
} // end-else
start = end + 1;
} // end-while
} // end-for
// Copy to ed
// before copying return vectors to the pool
for (int i = 0; i < static_cast<int>(segmentPoints.size()); ++i)
{
returnPointVectorToPool(std::move(segmentPoints[i]));
}
segmentPoints = validSegments;
}
//---------------------------------------------------------------------------
// Number of false alarms code as suggested by Desolneux, Moisan and Morel (DMM)
//
double EDPF::NFA(double prob, int len)
{
double nfa = np;
for (int i = 0; i < len && nfa > EPSILON; i++) nfa *= prob;
return nfa;
}
EDPF::Profile EDPF::getLastEDPFProfile() const { return lastEDPFProfile; }