7#include <unordered_map>
11#include <opencv2/core.hpp>
12#include <opencv2/core/mat.hpp>
13#include <opencv2/core/matx.hpp>
14#include <opencv2/core/types.hpp>
15#include <opencv2/imgproc.hpp>
17#include "helpers/Globals.hpp"
18#include "helpers/Information.hpp"
20#include "detectors/LaneDetector.hpp"
46 std::vector<cv::Vec4i> houghLines;
87 std::unordered_map<T, uint32_t> elementCount;
90 elementCount[element]++;
93 T mostFrequentElement =
static_cast<T
>(0);
94 uint32_t mostFrequentElementCount = 0;
96 for (
const std::pair<T, uint32_t> element : elementCount)
98 if (element.second > mostFrequentElementCount)
100 mostFrequentElement = element.first;
101 mostFrequentElementCount = element.second;
105 return mostFrequentElement;
117 for (
const cv::Vec4i& houghLine : p_houghLines)
126 if (CHANGE_IN_X == 0)
135 const double GRADIENT = CHANGE_IN_Y / CHANGE_IN_X;
260 const int32_t LOWER_X = detectedObjectInformation.
m_boundingBox.x;
262 const int32_t LOWER_Y = detectedObjectInformation.
m_boundingBox.y;
292 std::string rightLaneLineState =
" (R = Solid )";
295 rightLaneLineState =
" (R = Empty )";
300 rightLaneLineState =
" (R = Dashed)";
327 if ((LEFT_LINES_PRESENT && MIDDLE_LINES_PRESENT && RIGHT_LINES_PRESNET) ||
328 (LEFT_LINES_PRESENT && !MIDDLE_LINES_PRESENT && RIGHT_LINES_PRESNET))
330 currentDrivingState = Globals::DrivingState::WITHIN_LANE;
333 else if ((!LEFT_LINES_PRESENT && MIDDLE_LINES_PRESENT && !RIGHT_LINES_PRESNET) ||
334 (LEFT_LINES_PRESENT && MIDDLE_LINES_PRESENT && !RIGHT_LINES_PRESNET) ||
335 (!LEFT_LINES_PRESENT && MIDDLE_LINES_PRESENT && RIGHT_LINES_PRESNET))
337 currentDrivingState = Globals::DrivingState::CHANGING_LANES;
340 else if (LEFT_LINES_PRESENT && !MIDDLE_LINES_PRESENT && !RIGHT_LINES_PRESNET)
342 currentDrivingState = Globals::DrivingState::ONLY_LEFT_LANE_MARKING_DETECTED;
345 else if (!LEFT_LINES_PRESENT && !MIDDLE_LINES_PRESENT && RIGHT_LINES_PRESNET)
347 currentDrivingState = Globals::DrivingState::ONLY_RIGHT_LANE_MARKING_DETECTED;
356 if (NEXT_DRIVING_STATE == Globals::DrivingState::CHANGING_LANES)
382 case Globals::DrivingState::WITHIN_LANE:
386 case Globals::DrivingState::CHANGING_LANES:
390 case Globals::DrivingState::ONLY_LEFT_LANE_MARKING_DETECTED:
391 case Globals::DrivingState::ONLY_RIGHT_LANE_MARKING_DETECTED:
392 case Globals::DrivingState::NO_LANE_MARKINGS_DETECTED:
408 double leftLaneLineC = 0;
409 double leftLaneLineM = 0;
410 double leftLaneLineMinimumY = std::numeric_limits<double>::max();
417 leftLaneLineMinimumY});
432 double rightLaneLineC = 0;
433 double rightLaneLineM = 0;
434 double rightLaneLineMinimumY = std::numeric_limits<double>::max();
441 rightLaneLineMinimumY});
457 const double LANE_LINE_MINIMUM_Y = std::min(leftLaneLineMinimumY, rightLaneLineMinimumY);
479 const int32_t INTERSECTION_Y0 =
static_cast<int32_t
>((rightLaneLineM * leftLaneLineC - leftLaneLineM * rightLaneLineC) / (rightLaneLineM - leftLaneLineM));
483 if (INTERSECTION_Y0 < LANE_LINE_MINIMUM_Y)
485 m_laneDetectionInformation.m_laneOverlayCorners[0] = cv::Point(
static_cast<int32_t
>((LANE_LINE_MINIMUM_Y - leftLaneLineC) / leftLaneLineM),
static_cast<int32_t
>(LANE_LINE_MINIMUM_Y));
486 m_laneDetectionInformation.m_laneOverlayCorners[1] = cv::Point(
static_cast<int32_t
>((LANE_LINE_MINIMUM_Y - rightLaneLineC) / rightLaneLineM),
static_cast<int32_t
>(LANE_LINE_MINIMUM_Y));
499 double averageDistanceFromLeft = 0;
500 double averageDistanceFromRight = 0;
523 const double CURRENT_DISTANCE_DIFFERENCE = averageDistanceFromLeft - averageDistanceFromRight;
538 if (CHANGE_IN_DISTANCE_DIFFERENCE < 0)
543 else if (CHANGE_IN_DISTANCE_DIFFERENCE > 0)
Used for functionality that has not been implemented.
RollingAverage()
Constructs a new RollingAverage object.
std::deque< T > m_rollingAverageArray
A double-ended queue representing the states stored in the rolling average.
T CalculateRollingAverage(const T &p_nextInput)
Adds p_nextInput to the rolling average buffer and returns the most frequent value within the rolling...
LaneDetectionInformation GetInformation()
Get the LaneDetectionInformation struct.
void CalculateChangingLanesTurningDirection()
Determines the direction (left, right or neither) that the vehicle is turning while changing lanes.
Globals::DrivingState m_currentDrivingState
The current driving state.
static bool IsLineWithinObjectBoundingBoxes(const cv::Vec4i &p_houghLine, const ObjectDetectionInformation &p_objectDetectionInformation)
Determines whether p_houghLine is within any one of the p_objectDetectionInformation object bounding ...
std::vector< cv::Vec4i > m_middleLaneLines
The left, middle and right lane lines that have been detected for the current frame.
double m_middleLineAverageLength
The average left, middle and right lane line lengths for the current frame.
void AnalyseHoughLines(const std::vector< cv::Vec4i > &p_houghLines, const ObjectDetectionInformation &p_objectDetectionInformation, const bool &p_debugMode)
Determines whether the detected hough lines are left, middle or right lane lines. The majority of the...
void ExecuteDrivingState()
Execute logic based on the current driving state.
uint32_t m_changingLanesFrameCount
The number of frames passed since a distance difference was calculated while the vehicle is changing ...
LaneDetector()
Construct a new LaneDetector object.
bool m_changingLanesFirstFrame
Whether the current frame is the frame in which the driving state changed to CHANGING_LANES.
RollingAverage< Globals::DrivingState > m_drivingStateRollingAverage
The rolling average to determine the driving state.
std::vector< cv::Vec4i > m_rightLaneLines
The left, middle and right lane lines that have been detected for the current frame.
double m_rightLineAverageLength
The average left, middle and right lane line lengths for the current frame.
LaneDetectionInformation m_laneDetectionInformation
The LaneDetectionInformation struct containing all lane detection-related information.
void UpdateDrivingState()
Determines the current driving state depending upon the absence/presence of left, right and middle la...
double m_changingLanesPreviousDistanceDifference
The distance difference that will be compared to in order to determine the direction the vehicle is m...
void UpdateLineTypes()
Determines whether the left, middle, and right lane lines detected are solid, dashed or empty.
void RunLaneDetector(const cv::Mat &p_frame, const ObjectDetectionInformation &p_objectDetectionInformation, const bool &p_debugMode)
Runs the lane detector against p_frame.
double m_leftLineAverageLength
The average left, middle and right lane line lengths for the current frame.
std::vector< cv::Vec4i > m_leftLaneLines
The left, middle and right lane lines that have been detected for the current frame.
void CalculateLanePosition()
Determines the lane overlay for the current lane.
Contains all global and constant objects.
static const uint32_t G_SOLID_LINE_LENGTH_THRESHOLD
Threshold length to decide whether a line is to be considered solid line road marking.
static const std::unordered_map< DrivingState, std::string > G_DRIVING_STATE_TITLES
The driving state titles to display.
static const uint32_t G_CHANGING_LANES_DISTANCE_DIFFERENCE_FRAME_COUNT_THRESHOLD
The number of frames to wait before calculating another distance difference while the vehicle is chan...
static const cv::Scalar G_COLOUR_LIGHT_BLUE
OpenCV Colours (in BGR format).
static const cv::Range G_ROI_BOUNDING_BOX_Y_RANGE
The bounding box of the region-of-interest which is used to crop debugging frames....
static const double G_RIGHT_LINE_THRESHOLD_M
Region-of-interest sub-division line equations (y = mx + c).
static const uint32_t G_HOUGH_MIN_LINE_LENGTH
Hough transform threshold and properties.
static const uint32_t G_VIDEO_INPUT_HEIGHT
Input video dimensions.
static const uint32_t G_VIDEO_INPUT_WIDTH
Input video dimensions.
static const double G_RIGHT_LINE_THRESHOLD_C
Region-of-interest sub-division line equations (y = mx + c).
static const cv::Scalar G_COLOUR_GREY
OpenCV Colours (in BGR format).
static const uint32_t G_VEC4_X1_INDEX
Hough line index mapping.
static const cv::Range G_ROI_BOUNDING_BOX_X_RANGE
The bounding box of the region-of-interest which is used to crop debugging frames....
static const int32_t G_HOUGH_LINE_THICKNESS
The thickness of the hough lines drawn in debug mode.
static const cv::Scalar G_COLOUR_GREEN
OpenCV Colours (in BGR format).
static const uint32_t G_VEC4_X2_INDEX
Hough line index mapping.
static const cv::Scalar G_COLOUR_WHITE
OpenCV Colours (in BGR format).
static const uint32_t G_VEC4_Y1_INDEX
Hough line index mapping.
static const double G_DEBUGGING_FRAME_SCALING_FACTOR
Scaling factor for debugging frames.
static const int32_t G_ROI_BOTTOM_HEIGHT
Region-of-interest dimensions.
static const uint32_t G_HOUGH_MAX_LINE_GAP
Hough transform threshold and properties.
static const uint32_t G_DEFAULT_ROLLING_AVERAGE_SIZE
Default rolling average size.
static const double G_RIGHT_EDGE_OF_MASK_M
Region-of-interest sub-division line equations (y = mx + c).
static const double G_LEFT_LINE_THRESHOLD_M
Region-of-interest sub-division line equations (y = mx + c).
static const double G_LEFT_EDGE_OF_MASK_M
Region-of-interest sub-division line equations (y = mx + c).
static const cv::Scalar G_COLOUR_YELLOW
OpenCV Colours (in BGR format).
static const cv::Scalar G_COLOUR_RED
OpenCV Colours (in BGR format).
static const double G_LEFT_EDGE_OF_MASK_C
Region-of-interest sub-division line equations (y = mx + c).
static const uint32_t G_VEC4_Y2_INDEX
Hough line index mapping.
static const std::array< cv::Point, G_NUMBER_OF_POINTS > G_ROI_MASK_POINTS
Region-of-interest points. Below is how the indicies of the array map to the points.
static const double G_HOUGH_RHO
Hough transform threshold and properties.
static const uint32_t G_HOUGH_THRESHOLD
Hough transform threshold and properties.
static const double G_HOUGH_LINE_HORIZONTAL_GRADIENT_THRESHOLD
Threshold gradient to decide whether a line is to be considered horizontal.
static const double G_RIGHT_EDGE_OF_MASK_C
Region-of-interest sub-division line equations (y = mx + c).
DrivingState
The different driving states supported by the lane detector.
static const double G_HOUGH_THETA
Hough transform threshold and properties.
static const uint32_t G_CANNY_ALGORITHM_UPPER_THRESHOLD
Canny algorithm thresholds.
static const cv::Scalar G_COLOUR_BLUE
OpenCV Colours (in BGR format).
static const uint32_t G_CANNY_ALGORITHM_LOWER_THRESHOLD
Canny algorithm thresholds.
static const cv::Scalar G_COLOUR_LIGHT_RED
OpenCV Colours (in BGR format).
static const double G_LEFT_LINE_THRESHOLD_C
Region-of-interest sub-division line equations (y = mx + c).
Contains all Lane-and-Object-Detection objects.