/
StairDetection.cpp
372 lines (304 loc) · 10.6 KB
/
StairDetection.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
#include "StairDetection.h"
StairDetection::StairDetection()
{
}
StairDetection::~StairDetection()
{
}
void StairDetection::Run(cv::InputArray colorImg, cv::InputArray depthImg, std::vector<cv::Point> &stairsConvexHull)
{
cv::Mat detected_edges, detected_edges_inv;
std::vector<cv::Point> stairsConvexHull_normal, stairsConvexHull_inverse;
cv::Mat scaledColor, scaledDepth;
std::vector<cv::Vec4i> allLines;
int stairsAngle = -999;
cv::Point stairMidPoint;
std::vector<std::vector<int>> angles = std::vector<std::vector<int>>(180, std::vector<int>());
std::vector<cv::Point> stairPoints, stairsHull;
std::vector<cv::Point> stairMidLine, stairsMidPoints;
std::string timestamp = std::to_string(cv::getTickCount());
cv::resize(colorImg, scaledColor, cv::Size(320, 240));
cv::resize(depthImg, scaledDepth, cv::Size(320, 240));
//cv::imshow("Color Stairs", scaledColor);
//cv::imshow("DEPTH Stairs", scaledDepth);
CannyThreshold(scaledColor, detected_edges);
ApplyFilter(detected_edges, scaledDepth, 254, 255, CV_THRESH_BINARY);
Probabilistic_Hough(detected_edges, allLines);
SortLinesByAngle(allLines, angles);
DetermineStairAngle(angles, stairsAngle);
// stairs angle not found.
if (stairsAngle == -999)
return;
GetStairMidLine(allLines, angles[stairsAngle], stairsAngle, stairMidLine);
GetStairPoints(allLines, stairMidLine, stairsAngle, stairPoints, stairsMidPoints);
if (!DetermineStairs(scaledDepth, stairMidLine, stairsMidPoints))
return;
ExtractStairsHull(stairPoints, stairsConvexHull);
return;
}
bool StairDetection::DetermineStairs(cv::InputArray depthImg, std::vector<cv::Point> &stairMidLine, std::vector<cv::Point> &stairMidPoints)
{
// stairs should have at least 3 edges.
if (stairMidPoints.size() < 3)
return false;
// current holds the current found depth.
int current = -1, above = -1, below = -1;
int previous = -1, zeroCount = 0;
const int ZeroConsequtiveLimit = 10;
const int PreviousDeltaAllowance = 5;
cv::LineIterator it(depthImg.getMat(), stairMidLine[0], stairMidLine[1], 8, false);
std::vector<cv::Point>::iterator midpts = stairMidPoints.begin();
// for each row in half of the image frame
for (int i = 3; i < it.count / 2.0; i++, ++it)
{
// skip if this row is not the stair edge.
// the stair edges are stored in midpts.
if (i != midpts->y)
continue;
/// Let current = depth at stairEdge at y;
/// below = depth at stairEdge at y - 3;
/// above = depth at stairEdge at y - 3;
cv::Point curPt(it.pos());
cv::Point abvPt(it.pos().x, it.pos().y + 3);
cv::Point blwPt(it.pos().x, it.pos().y - 3);
current = (int)depthImg.getMat().at<uchar>(curPt);
above = (int)depthImg.getMat().at<uchar>(abvPt);
below = (int)depthImg.getMat().at<uchar>(blwPt);
// if difference between above and current is < 3 depth units &&
// difference between current's depth must more than 9 depth units.
if (((above - current) < 3) && (current - below) > 9)
// then it could be stairs, update to next stair edge.
++midpts;
else
// else it's not stairs at all.
return false;
}
cv::LineIterator ascendingIt(depthImg.getMat(), stairMidLine[0], stairMidLine[1], 8, false);
// until first half of the image.
for (int i = 0; i < ascendingIt.count / 2.0; i++, ++ascendingIt)
{
current = (int)depthImg.getMat().at<uchar>(ascendingIt.pos());
if (current == 0) {
/// if too many consecutive empty depth,
/// then this image is too corrupted / does not have stairs
if (++zeroCount > ZeroConsequtiveLimit)
return false;
continue;
}
zeroCount = 0;
/// Stairs should have ascending depth value;
/// However, on angled view stairs, the depth value can occasional drop a bit.
/// Else it's not stairs at all.
if (current > previous)
previous = current;
else if (current > (previous - PreviousDeltaAllowance))
continue;
else
return false;
}
return true;
}
void StairDetection::GetIntersectHull(std::vector<cv::Point> &stairsConvexHull_normal, std::vector<cv::Point> &stairsConvexHull_inverse, std::vector<cv::Point> &intersectConvexHull)
{
if (stairsConvexHull_normal.empty() || stairsConvexHull_inverse.empty())
return;
for (cv::Point p : stairsConvexHull_normal) {
if (pointPolygonTest(stairsConvexHull_inverse, p, false) >= 0) {
intersectConvexHull.push_back(p);
}
}
for (cv::Point p : stairsConvexHull_inverse) {
if (pointPolygonTest(stairsConvexHull_normal, p, false) >= 0) {
intersectConvexHull.push_back(p);
}
}
}
int StairDetection::AngleBetween(const int x1, const int y1, const int x2, const int y2) {
double xDiff = x2 - x1;
double yDiff = y2 - y1;
int angle = floor(atan2(yDiff, xDiff) * 180 / CV_PI);
if (angle < 0) {
angle += 180;
}
return angle;
}
void StairDetection::CannyThreshold(cv::InputArray image, cv::Mat &edges) {
if (image.channels() > 1) {
/// Convert the image to grayscale
cvtColor(image, edges, CV_RGB2GRAY);
/// Reduce noise with a kernel 3x3
blur(edges, edges, cv::Size(3, 3));
}
else {
/// src is already a grayscale image.
edges = image.getMat().clone();
}
/// Canny detector
Canny(edges, edges, cannyLowThreshold, cannyLowThreshold*cannyRatio, cannyKernelSize);
}
void StairDetection::ApplyFilter(cv::Mat &src, cv::InputArray &filter, double thresh, double maxval, int type) {
cv::Mat threshold, temp;
cv::blur(filter, temp, cv::Size(3, 3));
cv::threshold(temp, threshold, 0, 255, CV_THRESH_BINARY);
int erosion_size = 1;
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT,
cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1),
cv::Point(erosion_size, erosion_size));
/// Apply the erosion operation
dilate(threshold, temp, element);
cv::bitwise_and(src.clone(), temp, src);
}
void StairDetection::Probabilistic_Hough(cv::InputArray src, cv::OutputArray output) {
HoughLinesP(src, output, 2, CV_PI / 720.0 * 1.0, min_Houghthreshold + houghThreshold, min_HoughLinelength, min_HoughLinegap);
}
/// groups the angles into one similar angle.
int StairDetection::GroupAngles(int angle)
{
/// ignore angles between this range.
/// because stairs shouldn't be perpendicular to the user.
if (angle > 33 & angle < 147)
return -1;
/// shift the angle into the middle angle.
int binSize = 10;
if (angle % binSize <= binSize / 2.0)
angle -= angle % binSize;
else
angle += (-angle % binSize);
if (angle > 180)
angle = 0;
return angle;
}
/// Determine the angles in the list of lines in allLines
/// and sorts the lines of the same angle into the same vector
void StairDetection::SortLinesByAngle(std::vector<cv::Vec4i> &allLines, std::vector<std::vector<int>> &angles)
{
for (size_t i = 0; i < allLines.size(); i++) {
cv::Vec4i l = allLines.at(i);
int angle = AngleBetween(l[0], l[1], l[2], l[3]);
angle = GroupAngles(angle);
if (angle == -1)
continue;
angles[angle].push_back(i);
}
}
/// Find the angle with the most lines to be the angle of the stairs.
void StairDetection::DetermineStairAngle(std::vector<std::vector<int>> &angles, int &stairsAngle)
{
int max = 0;
for (int angle = 0; angle < 180; ++angle) {
std::vector<int> &vec = angles.at(angle);
if (!vec.empty() && vec.size() > 3 && vec.size() > max) {
stairsAngle = angle;
max = vec.size();
}
}
}
// Finds the intersection of two lines, or returns false.
// The lines are defined by (o1, p1) and (o2, p2).
bool StairDetection::intersection(cv::Point2f o1, cv::Point2f p1, cv::Point2f o2, cv::Point2f p2, cv::Point &r)
{
cv::Point2f x = o2 - o1;
cv::Point2f d1 = p1 - o1;
cv::Point2f d2 = p2 - o2;
float cross = d1.x*d2.y - d1.y*d2.x;
if (abs(cross) < /*EPS*/1e-8)
return false;
double t1 = (x.x * d2.y - x.y * d2.x) / cross;
r = o1 + d1 * t1;
if (r.x < 0 || r.x > 320)
return false;
if (r.y < 0 || r.y > 240)
return false;
return true;
}
/// From confident stair points,
/// find the best fit line that represents stairs.
void StairDetection::GetStairMidLine(std::vector<cv::Vec4i> &allLines, std::vector<int> &stairIndexes, int &stairsAngle, std::vector<cv::Point> &stairMidLine)
{
std::vector<cv::Point2f> points;
cv::Vec4f temp;
for (int index : stairIndexes) {
cv::Vec4i &l = allLines[index];
cv::Point p1(l[0], l[1]);
cv::Point p2(l[2], l[3]);
points.push_back(p1);
points.push_back(p2);
}
if (!points.empty())
cv::fitLine(points, temp, CV_DIST_L2, 0, 0.01, 0.01);
cv::Point pt1, pt2;
double theta = stairsAngle * CV_PI / 180;
double a = cos(theta), b = sin(theta);
pt1.x = cvRound(temp[2] + 320 * -b);
pt1.y = cvRound(temp[3] + 240 * a);
pt2.x = cvRound(temp[2] - 320 * -b);
pt2.y = cvRound(temp[3] - 240 * a);
if (pt1.x < 0)
pt1.x = 0;
if (pt2.x < 0)
pt2.x = 0;
if (pt1.y < 0)
pt1.y = 0;
if (pt2.y < 0)
pt2.y = 0;
if (pt1.x >= 320)
pt1.x = 319;
if (pt2.x >= 320)
pt2.x = 319;
if (pt1.y >= 240)
pt1.y = 239;
if (pt2.y >= 240)
pt2.y = 239;
/// Ensure the lower point is first.
if (pt1.y > pt2.y) {
stairMidLine.push_back(pt1);
stairMidLine.push_back(pt2);
}
else {
stairMidLine.push_back(pt2);
stairMidLine.push_back(pt1);
}
}
/// Using the found best fit line that represents the stairs,
/// find all lines that intersect with the fit line
/// all lines that intersect belong to the stairs
void StairDetection::GetStairPoints(std::vector<cv::Vec4i> &allLines, std::vector<cv::Point> &stairMidLine, int &stairsAngle, std::vector<cv::Point> &stairPoints, std::vector<cv::Point> &stairMidPoints)
{
cv::Point pt1 = stairMidLine[0];
cv::Point pt2 = stairMidLine[1];
for (cv::Vec4i vec : allLines) {
cv::Point l1(vec[0], vec[1]);
cv::Point l2(vec[2], vec[3]);
cv::Point r;
int theta = AngleBetween(l1.x, l1.y, l2.x, l2.y);
// if the line is within +- 10 degs, and it intersects the midline,
// then it belongs to the set of lines that represents the stairs.
if (abs(stairsAngle - theta) <= 10) {
if (intersection(l1, l2, pt1, pt2, r)) {
stairPoints.push_back(l1);
stairPoints.push_back(l2);
stairMidPoints.push_back(r);
}
}
}
}
void StairDetection::ExtractStairsHull(std::vector<cv::Point> &stairPoints, std::vector<cv::Point> &stairsHull)
{
if (!stairPoints.empty())
cv::convexHull(cv::Mat(stairPoints), stairsHull);
}
void StairDetection::drawStairs(std::string windowName, cv::InputArray colorImg, std::vector<cv::Point> &stairConvexHull)
{
if (!stairConvexHull.empty()) {
cv::Mat temp;
cv::resize(colorImg, temp, cv::Size(320, 240));
std::vector<std::vector<cv::Point> > hull(1);
cv::Scalar color = cv::Scalar(cv::theRNG().uniform(0, 255), cv::theRNG().uniform(0, 255), cv::theRNG().uniform(0, 255));
hull.push_back(stairConvexHull);
for (int i = 0; i<hull.size(); ++i) {
drawContours(temp, hull, i, color, 3, 8, std::vector<cv::Vec4i>(), 0, cv::Point());
}
cv::imshow(windowName, temp);
//cv::imwrite(windowName + ".png", temp);
}
}