|
| 1 | +#include "LaneDetection.h" |
| 2 | + |
| 3 | +cv::Mat LaneDetection::s_frame; |
| 4 | +int LaneDetection::s_frameCenter; |
| 5 | +int LaneDetection::s_maxLineHeight; |
| 6 | +cv::Mat LaneDetection::s_mask; |
| 7 | +std::vector<cv::Vec4i> LaneDetection::s_lines; |
| 8 | +std::vector<cv::Point> LaneDetection::s_rightLinePoints; |
| 9 | +std::vector<cv::Point> LaneDetection::s_leftLinePoints; |
| 10 | +std::array<cv::Point, 4> LaneDetection::s_boundaries; |
| 11 | + |
| 12 | +void LaneDetection::createMask(const cv::Size& frameSize, double frameFormat) { |
| 13 | + s_mask = cv::Mat::zeros(frameSize, frameFormat); |
| 14 | + |
| 15 | + const float hScale = 0.625; |
| 16 | + |
| 17 | + std::array<cv::Point, 4> points ({ |
| 18 | + cv::Point(0.08 * frameSize.width, frameSize.height), |
| 19 | + cv::Point(0.42 * frameSize.width, hScale * frameSize.height), |
| 20 | + cv::Point(0.56 * frameSize.width, hScale * frameSize.height), |
| 21 | + cv::Point( frameSize.width, frameSize.height) |
| 22 | + }); |
| 23 | + |
| 24 | + //Create trapezoid mask |
| 25 | + cv::fillConvexPoly(s_mask, points.data(), 4, cv::Scalar(255, 0, 0)); |
| 26 | +} |
| 27 | + |
| 28 | +inline void LaneDetection::applyMask() { |
| 29 | + cv::bitwise_and(s_frame, s_mask, s_frame); |
| 30 | +} |
| 31 | + |
| 32 | +inline void LaneDetection::blur() { |
| 33 | + cv::GaussianBlur(s_frame, s_frame, cv::Size(3, 3), 0, 0); //3x3px trial & error |
| 34 | +} |
| 35 | + |
| 36 | +inline void LaneDetection::edgeDetection() { |
| 37 | + |
| 38 | + cv::cvtColor(s_frame, s_frame, cv::COLOR_RGB2GRAY); |
| 39 | + |
| 40 | + //binarize gray image |
| 41 | + cv::threshold(s_frame, s_frame, 140, 255, cv::THRESH_BINARY); //threshold trial & error |
| 42 | + |
| 43 | + /* |
| 44 | +
|
| 45 | + Create the kernel [-1 0 1] |
| 46 | + This kernel is based on the one found in the |
| 47 | + Lane Departure Warning System by Mathworks |
| 48 | +
|
| 49 | + */ |
| 50 | + cv::Point anchor = cv::Point(-1, -1); |
| 51 | + cv::Mat kernel = cv::Mat(1, 3, CV_32F); |
| 52 | + kernel.at<float>(0, 0) = -1; |
| 53 | + kernel.at<float>(0, 1) = 0; |
| 54 | + kernel.at<float>(0, 2) = 1; |
| 55 | + |
| 56 | + //filter the binary image to obtain the edges //compare results to CannyEdge?? |
| 57 | + cv::filter2D(s_frame, s_frame, -1, kernel, anchor, 0, cv::BORDER_DEFAULT); |
| 58 | +} |
| 59 | + |
| 60 | +inline void LaneDetection::houghLines() { |
| 61 | + s_lines.clear(); |
| 62 | + |
| 63 | + //calibrate once every x seconds? |
| 64 | + HoughLinesP(s_frame, s_lines, 1, CV_PI / 180, 20, 20, 30); //rho & theta by trial & error |
| 65 | +} |
| 66 | + |
| 67 | +void LaneDetection::classifyLines() { |
| 68 | + s_rightLinePoints.clear(); |
| 69 | + s_leftLinePoints.clear(); |
| 70 | + |
| 71 | + const float minSlope = 0.3f; |
| 72 | + const float maxSlope = 1.5f; |
| 73 | + |
| 74 | + for (const auto& line : s_lines) { |
| 75 | + |
| 76 | + //slope = (y1 - y0) / (x1 - x0) |
| 77 | + float slope = static_cast<float>(line[3] - line[1]); |
| 78 | + slope /= ( static_cast<float>(line[2] - line[0]) + 0.00001f ); |
| 79 | + |
| 80 | + //filter too horizontal slopes |
| 81 | + float absSlope = std::fabs(slope); |
| 82 | + if (absSlope < minSlope || absSlope > maxSlope) continue; |
| 83 | + |
| 84 | + if (slope > 0 && line[2] > s_frameCenter && line[0] > s_frameCenter) { |
| 85 | + s_rightLinePoints.push_back(cv::Point(line[0], line[1])); |
| 86 | + s_rightLinePoints.push_back(cv::Point(line[2], line[3])); |
| 87 | + } else if (slope < 0 && line[2] < s_frameCenter && line[0] < s_frameCenter) { |
| 88 | + s_leftLinePoints.push_back(cv::Point(line[0], line[1])); |
| 89 | + s_leftLinePoints.push_back(cv::Point(line[2], line[3])); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void LaneDetection::leastSquaresRegression() { |
| 95 | + |
| 96 | + /* |
| 97 | + if left/right lane is not detected previous value is reused |
| 98 | + issue: if at least 1 lane is not detected in 1st frame => garbage output |
| 99 | +
|
| 100 | + TODO: averaging over few frames |
| 101 | + */ |
| 102 | + |
| 103 | + //fit right lane |
| 104 | + if (!s_rightLinePoints.empty()) { |
| 105 | + cv::Vec4d right_line; |
| 106 | + |
| 107 | + cv::fitLine(s_rightLinePoints, right_line, cv::DIST_L2, 0, 0.01, 0.01); |
| 108 | + float right_m = right_line[1] / right_line[0]; |
| 109 | + cv::Point right_b = cv::Point(right_line[2], right_line[3]); // y = m*x + b |
| 110 | + |
| 111 | + float right_ini_x = (static_cast<float>(s_frame.rows - right_b.y) / right_m) + right_b.x; |
| 112 | + float right_fin_x = (static_cast<float>(s_maxLineHeight - right_b.y) / right_m) + right_b.x; |
| 113 | + s_boundaries[0] = cv::Point(right_ini_x, s_frame.rows); |
| 114 | + s_boundaries[1] = cv::Point(right_fin_x, s_maxLineHeight); |
| 115 | + } |
| 116 | + |
| 117 | + //fit left lane |
| 118 | + if (!s_leftLinePoints.empty()) { |
| 119 | + cv::Vec4d left_line; |
| 120 | + |
| 121 | + cv::fitLine(s_leftLinePoints , left_line, cv::DIST_L2, 0, 0.01, 0.01); |
| 122 | + float left_m = left_line[1] / left_line[0]; |
| 123 | + cv::Point left_b = cv::Point(left_line[2], left_line[3]); |
| 124 | + |
| 125 | + float left_ini_x = (static_cast<float>(s_frame.rows - left_b.y) / left_m) + left_b.x; |
| 126 | + float left_fin_x = (static_cast<float>(s_maxLineHeight - left_b.y) / left_m) + left_b.x; |
| 127 | + s_boundaries[2] = cv::Point(left_ini_x, s_frame.rows); |
| 128 | + s_boundaries[3] = cv::Point(left_fin_x, s_maxLineHeight); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +void LaneDetection::prepare(const cv::Size& frameSize, double frameFormat) { |
| 133 | + createMask(frameSize, frameFormat); |
| 134 | + s_frameCenter = frameSize.width / 2; |
| 135 | + s_maxLineHeight = static_cast<int>(0.66f * frameSize.height); |
| 136 | +} |
| 137 | + |
| 138 | +void LaneDetection::setFrame(const cv::Mat& frame) { |
| 139 | + s_frame = frame; |
| 140 | +} |
| 141 | + |
| 142 | +void LaneDetection::process(cv::Mat& frame) { |
| 143 | + setFrame(frame); |
| 144 | + |
| 145 | + blur(); //remove noise by blurring image |
| 146 | + edgeDetection(); //detect edges |
| 147 | + |
| 148 | + applyMask(); //crop ROI |
| 149 | + houghLines(); // use HoughLinesP |
| 150 | + |
| 151 | + if (!s_lines.empty()) { |
| 152 | + classifyLines(); //Classify which lines are for left or right lane |
| 153 | + leastSquaresRegression(); //calculate lane regression |
| 154 | + |
| 155 | + display(frame); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +void LaneDetection::display(cv::Mat& frame) { |
| 160 | + |
| 161 | + cv::Mat output; |
| 162 | + frame.copyTo(output); |
| 163 | + |
| 164 | + //create semi-transparent trapezoid |
| 165 | + std::array<cv::Point, 4> poly_points; |
| 166 | + poly_points[0] = s_boundaries[2]; |
| 167 | + poly_points[1] = s_boundaries[0]; |
| 168 | + poly_points[2] = s_boundaries[1]; |
| 169 | + poly_points[3] = s_boundaries[3]; |
| 170 | + |
| 171 | + cv::fillConvexPoly(output, poly_points.data(), 4, cv::Scalar(255, 255, 255), cv::LINE_AA, 0); |
| 172 | + cv::addWeighted(output, 0.4, frame, 0.6, 0, frame); |
| 173 | + |
| 174 | + //draw left & right lane |
| 175 | + cv::line(frame, s_boundaries[0], s_boundaries[1], cv::Scalar(255, 255, 255), 7, cv::LINE_AA); |
| 176 | + cv::line(frame, s_boundaries[2], s_boundaries[3], cv::Scalar(255, 255, 255), 7, cv::LINE_AA); |
| 177 | + |
| 178 | + //display processed frame |
| 179 | + cv::imshow("Lane detection", frame); |
| 180 | + cv::waitKey(1); |
| 181 | +} |
| 182 | + |
0 commit comments