|
| 1 | + |
| 2 | +#ifndef __LANE_DETECTION__ |
| 3 | +#define __LANE_DETECTION__ |
| 4 | + |
| 5 | +#define _USE_MATH_DEFINES |
| 6 | +#include <cmath> |
| 7 | +#include "regression.h" |
| 8 | +#include "log_manager.h" |
| 9 | +#include <opencv2/highgui.hpp> |
| 10 | +#include <stdexcept> |
| 11 | + |
| 12 | +#define CAR_IN_IMAGE 80 |
| 13 | + |
| 14 | +struct LaneDrawingInfo { |
| 15 | + int rightX1; //x bottom right |
| 16 | + int rightX2; //x top right |
| 17 | + int leftX1; //x bottom left |
| 18 | + int leftX2; //x top left |
| 19 | + int y1; //y bottom right & left |
| 20 | + int y2; //y top right & left |
| 21 | +}; |
| 22 | + |
| 23 | +class LaneDetector { |
| 24 | + public: |
| 25 | + void init(); |
| 26 | + void manageLaneDetector(std::shared_ptr<cv::Mat> frame); |
| 27 | + void drawLanesOnImage(std::shared_ptr<cv::Mat> img); |
| 28 | + |
| 29 | + private: |
| 30 | + std::shared_ptr<cv::Mat> image; |
| 31 | + bool first; |
| 32 | + bool withoutCar; |
| 33 | + int imgCols; |
| 34 | + int imgRows; |
| 35 | + LaneDrawingInfo drawingInfo; |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns true when the image is classified as daytime. |
| 39 | + * Note: this is based on the mean pixel value of an image and might not |
| 40 | + * always lead to accurate results. |
| 41 | + */ |
| 42 | + bool isDayTime(); |
| 43 | + |
| 44 | + /** |
| 45 | + * Filter source image so that only the white and yellow pixels remain. |
| 46 | + * A gray filter will also be added if the source image is classified as taken during the night. |
| 47 | + * One assumption for lane detection here is that lanes are either white or yellow. |
| 48 | + * @param isDayTime true if image is taken during the day, false if at night |
| 49 | + * @return Mat filtered image |
| 50 | + */ |
| 51 | + cv::Mat filterColors(bool isDayTime); |
| 52 | + |
| 53 | + /** |
| 54 | + * Apply grayscale transform on image. |
| 55 | + * @return grayscale image |
| 56 | + */ |
| 57 | + cv::Mat applyGrayscale(cv::Mat source); |
| 58 | + |
| 59 | + /** |
| 60 | + * Apply Gaussian blur to image. |
| 61 | + * @return blurred image |
| 62 | + */ |
| 63 | + cv::Mat applyGaussianBlur(cv::Mat source); |
| 64 | + |
| 65 | + /** |
| 66 | + * Detect edges of image by applying canny edge detection. |
| 67 | + * @return image with detected edges |
| 68 | + */ |
| 69 | + cv::Mat applyCanny(cv::Mat source); |
| 70 | + |
| 71 | + /** |
| 72 | + * Apply an image mask. |
| 73 | + * Only keep the part of the image defined by the |
| 74 | + * polygon formed from four points. The rest of the image is set to black. |
| 75 | + * @return Mat image with mask |
| 76 | + */ |
| 77 | + cv::Mat RegionOfInterest(cv::Mat source); |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns a vector with the detected hough lines. |
| 81 | + * @param canny image resulted from a canny transform |
| 82 | + * @param source image on which hough lines are drawn |
| 83 | + * @param drawHough draw detected lines on source image if true. |
| 84 | + * It will also show the image with de lines drawn on it, which is why |
| 85 | + * it is not recommended to pass in true when working with a video. |
| 86 | + * @return vector<Vec4i> contains hough lines. |
| 87 | + */ |
| 88 | + std::vector<cv::Vec4i> houghLines(cv::Mat canny, cv::Mat source, |
| 89 | + bool drawHough); |
| 90 | + |
| 91 | + /** |
| 92 | + * Creates mask and blends it with source image so that the lanes |
| 93 | + * are drawn on the source image. |
| 94 | + * @param lines vector < vec4i > holding the lines |
| 95 | + * @return Mat image with lines drawn on it |
| 96 | + */ |
| 97 | + bool drawLanes(std::vector<cv::Vec4i> lines); |
| 98 | + |
| 99 | + /** |
| 100 | + * Drawing the lane on the road only |
| 101 | + */ |
| 102 | + void cutCar(); |
| 103 | +}; |
| 104 | + |
| 105 | +#endif /*__LANE_DETECTION__*/ |
0 commit comments