diff --git a/Makefile b/Makefile index e91f495..41ed156 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,15 @@ modules/traffic-sign-detector/traffic_sign_detector.cpp EXE = main.exe -LIB = -L/usr/local/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn -lopencv_dpm -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core -lwiringpi +LIB = -L/usr/local/lib -lopencv_stitching -lopencv_superres -lopencv_videostab \ +-lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn \ +-lopencv_dpm -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_plot \ +-lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_rgbd \ +-lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face \ +-lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d \ +-lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml \ +-lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo \ +-lopencv_imgproc -lopencv_core -lwiringpi $(EXE) : $(SRC) - $(CC) -o $(EXE) $(CFLAGS) $(SRC) $(LIB) + $(CC) -o $(EXE) $(CFLAGS) $(SRC) $(LIB) \ No newline at end of file diff --git a/main.cpp b/main.cpp index 7d1ca34..8fbca45 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,4 @@ #include -#include #include "modules/speed-estimator/speed_estimator.h" #include "modules/lane-detector/lane_detector.h" @@ -7,101 +6,122 @@ #include "modules/stopping-distance-calculator/stopping_distance_calculator.h" #include "modules/traffic-sign-detector/traffic_sign_detector.h" +//#define VIBRATION_MOTOR +//#define DETECT_PEDESTRIANS + +#ifdef VIBRATION_MOTOR #include "rpi3.h" +#endif #define RED Scalar(0, 0, 255) #define GREEN Scalar(0, 255, 0) #define BLUE Scalar(255, 0, 0) -#define VIDEO_RATIO 0.4 - using namespace std; using namespace cv; int main(int argc, const char** argv) { - Mat in, out; - Speed speed, currSpeed; - vector lanes; - vector pedestrians; - LaneDetector* lane_detector; - PedestrianDetector* pedestrian_detector; - TrafficSignDetector* sign_detector; - int curr_frame = 0; - bool is_out_of_lane = false, - is_distance_safe = true, - contains_pedestrian = false; - - // Vibration motor + Mat in, out; + Speed speed, currSpeed; + vector pedestrians; + LaneDetector* lane_detector; + PedestrianDetector* pedestrian_detector; + TrafficSignDetector* sign_detector; + SpeedEstimator* speed_estimator; + StoppingDistanceCalculator* distance_calculator; + int curr_frame = 0, + running_mode = atoi(argv[1]); + bool is_distance_safe = true, + contains_pedestrian = false; + +#ifdef VIBRATION_MOTOR + // Vibration motor initialization hardwareSetup(); - +#endif + VideoCapture cap(0); cap >> in; - resize(in, in, Size(), VIDEO_RATIO, VIDEO_RATIO, INTER_AREA); - - lane_detector = new LaneDetector(in, atoi(argv[1])); + resize(in, in, Size(480, 360)); + + lane_detector = new LaneDetector(in, running_mode); sign_detector = new TrafficSignDetector(in); pedestrian_detector = new PedestrianDetector(); - namedWindow("PROJECT", WINDOW_KEEPRATIO); + speed_estimator = new SpeedEstimator(); + distance_calculator = new StoppingDistanceCalculator(); while (true) { cap >> in; - resize(in, in, Size(), VIDEO_RATIO, VIDEO_RATIO, INTER_AREA); + resize(in, in, Size(480, 360)); in.copyTo(out); // Speed Estimator - currSpeed = SpeedEstimator::GetSpeed(in, curr_frame++, atoi(argv[1])); + currSpeed = speed_estimator->GetSpeed(in, curr_frame++, running_mode); if (currSpeed != SPD_CURR) { speed = currSpeed; } switch (speed) { - case SPD_SLOW: - putText(out, "SPD: SLOW", Point(40, in.rows - 50), 1, 2, RED, 2); - break; - case SPD_NORMAL: - putText(out, "SPD: NORMAL", Point(40, in.rows - 50), 1, 2, RED, 2); - break; - case SPD_FAST: - putText(out, "SPD: FAST", Point(40, in.rows - 50), 1, 2, RED, 2); - vibrationStateChange(1); - break; + case SPD_SLOW: + putText(out, "SPD: SLOW", + Point(40, in.rows - 50), 1, 2, RED, 2); + break; + case SPD_NORMAL: + putText(out, "SPD: NORMAL", + Point(40, in.rows - 50), 1, 2, RED, 2); + break; + case SPD_FAST: + putText(out, "SPD: FAST", + Point(40, in.rows - 50), 1, 2, RED, 2); + +#ifdef VIBRATION_MOTOR + vibrationStateChange(1); +#endif + break; } -#if 0 + +#ifdef DETECT_PEDESTRIANS // Pedestrian Detector if (currSpeed == SPD_FAST) { - contains_pedestrian = pedestrian_detector->DetectPedestrians(in, out, BLUE, 2); + contains_pedestrian = + pedestrian_detector->DetectPedestrians(in, out, BLUE, 2); if (contains_pedestrian) { + +#ifdef VIBRATION_MOTOR vibrationStateChange(1); +#endif } } #endif + // Lane Detector - lanes = lane_detector->GetLanes(in); - is_out_of_lane = lane_detector->IsOutOfLane(in); - lane_detector->DrawLanes(out, lanes, GREEN, 7); - if (is_out_of_lane) { - vibrationStateChange(1); - } + lane_detector->DrawLanes(out, lane_detector->GetLanes(in), GREEN, 5); // Stopping Distance Calculator if (curr_frame % 20 == 0) { - is_distance_safe = StoppingDistanceCalculator::IsSafe(in); + is_distance_safe = distance_calculator->IsSafe(in); } if (!is_distance_safe) { putText(out, "DIST: NOT SAFE", Point(in.cols / 2, in.rows - 50), 1, 2, RED, 2); + +#ifdef VIBRATION_MOTOR vibrationStateChange(1); +#endif } // Traffic Sign Detector - sign_detector->DetectTrafficSigns(in, out, CLR_BLUE, BLUE, 2); + sign_detector->DetectTrafficSigns(in, out, BLUE, 2); - imshow("PROJECT", out); + imshow("DRIVING ASSISTANT", out); waitKey(1); - reset(); - } + +#ifdef VIBRATION_MOTOR + //reset(); +#endif + + } // Infinite loop return 0; -} +} \ No newline at end of file diff --git a/modules/lane-detector/lane_detector.cpp b/modules/lane-detector/lane_detector.cpp index e4e26a9..0463a6c 100644 --- a/modules/lane-detector/lane_detector.cpp +++ b/modules/lane-detector/lane_detector.cpp @@ -1,27 +1,14 @@ #include "lane_detector.h" -Mat LaneDetector::mask_for_elim; -int LaneDetector::kMode; - const double LaneDetector::kLaneTresh = 0.05; const double LaneDetector::kAngleTresh = 20.0; LaneDetector::LaneDetector(Mat image, int mode) { - Mat temp; - image.copyTo(temp); - - for (int pi = 0; pi < temp.rows; ++pi) { - for (int pj = 0; pj < temp.cols; ++pj) { - - temp.at(pi, pj)[0] = 0; - temp.at(pi, pj)[1] = 0; - temp.at(pi, pj)[2] = 0; - } - } + Mat temp = Mat::zeros(image.rows, image.cols, image.type()); - // Eliminate the further traffic - for (int pi = temp.rows * 3 / 5; pi < temp.rows * 4 / 5; ++pi) { + // Search for lanes in rows between %40 and %80 + for (int pi = temp.rows * 2 / 5; pi < temp.rows * 4 / 5; ++pi) { for (int pj = 0; pj < temp.cols; ++pj) { temp.at(pi, pj)[0] = 255; @@ -30,24 +17,24 @@ LaneDetector::LaneDetector(Mat image, int mode) { } } - LaneDetector::mask_for_elim = temp; - LaneDetector::kMode = mode; + this->mask_for_elim_ = temp; + this->mode_ = mode; } Mat LaneDetector::GetWhiteMask(Mat image, bool apply_mask) { - Mat tresh_saturation, - tresh_value, - hsv; - int sensitivity_sat, - sensitivity_val; + Mat tresh_saturation, + tresh_value, + hsv; vector channels; + int sensitivity_sat, + sensitivity_val; - if (LaneDetector::kMode == 0) { + if (this->mode_ == 0) { sensitivity_sat = 70; sensitivity_val = 130; } - else if (LaneDetector::kMode == 1){ + else if (this->mode_ == 1) { sensitivity_sat = 70; sensitivity_val = 115; } @@ -58,7 +45,7 @@ Mat LaneDetector::GetWhiteMask(Mat image, bool apply_mask) { // Convert image to HSV, split it to channels if (apply_mask) { - cvtColor(LaneDetector::mask_for_elim & image, hsv, CV_BGR2HSV); + cvtColor(this->mask_for_elim_ & image, hsv, CV_BGR2HSV); } else { cvtColor(image, hsv, CV_BGR2HSV); @@ -75,7 +62,7 @@ Mat LaneDetector::GetWhiteMask(Mat image, bool apply_mask) { vector LaneDetector::GetLanes(Mat image) { - Mat white_mask = GetWhiteMask(image, true); + Mat white_mask = this->GetWhiteMask(image, true); vector lanes; erode(white_mask, white_mask, Mat(), Point(-1, -1), 1, 1, 1); @@ -90,14 +77,15 @@ void LaneDetector::DrawLanes( double angle; + // Eliminate some of the lanes which are not vertical for (unsigned int li = 0; li < lanes.size(); ++li) { angle = atan2(lanes[li][3] - lanes[li][1], lanes[li][2] - lanes[li][0]) * 180.0 / CV_PI; if (angle > kAngleTresh || angle < -kAngleTresh) { - line(image, cv::Point(lanes[li][0], lanes[li][1]), - cv::Point(lanes[li][2], lanes[li][3]), color, thickness); + line(image, Point(lanes[li][0], lanes[li][1]), + Point(lanes[li][2], lanes[li][3]), color, thickness); } } } diff --git a/modules/lane-detector/lane_detector.h b/modules/lane-detector/lane_detector.h index cd867d4..9ea4c05 100644 --- a/modules/lane-detector/lane_detector.h +++ b/modules/lane-detector/lane_detector.h @@ -1,36 +1,59 @@ #pragma once #include -#include using namespace std; using namespace cv; -class LaneDetector -{ +class LaneDetector { + public: - // Initialize with a sample image - // Mode: 0 -> If the video quality is low - // Mode: 1 -> If the video quality is middle - // Mode: 2 -> If the video quality is high + /* + * Initialize with a sample image + * + * Mode: 0 -> If the video quality is low + * Mode: 1 -> If the video quality is middle + * Mode: 2 -> If the video quality is high + */ LaneDetector(Mat image, int mode); - static bool IsOutOfLane(Mat image); - static vector GetLanes(Mat image); - static void DrawLanes(Mat image, vector lanes, Scalar color, int thickness); + + /* + * Returns true if the vehicle is out of lane + */ + bool IsOutOfLane(Mat image); + + vector GetLanes(Mat image); + void DrawLanes(Mat image, vector lanes, Scalar color, int thickness); private: - // Returns a binary image in which - // 1 corresponds to white area, 0 corresponds to other colors - // apply_mask flag myst be true if we are detecting lanes, false o/w - static Mat GetWhiteMask(Mat image, bool apply_mask); - static Mat mask_for_elim; + /* + * Returns a binary image in which 1 corresponds to white area, 0 corresponds + * to other colors apply_mask flag must be true if we are detecting lanes, + * false o/w + */ + Mat GetWhiteMask(Mat image, bool apply_mask); - // Treshold value for checking if the vehice is in the middle of lanes + /* + * Running mode + * + * Mode: 0 -> If the video quality is low + * Mode: 1 -> If the video quality is middle + * Mode: 2 -> If the video quality is high + */ + int mode_; + + Mat mask_for_elim_; + + /* + * Treshold value for checking if the vehice is in the middle of lanes + * (Same as checking if the vehicle is out of lane) + */ const static double kLaneTresh; - // If the angle of a line is greater than kAngleTresh or less than - // -kAngleTresh, then the line is considered to be vertical + /* + * If the angle of a line is greater than kAngleTresh or less than + * -kAngleTresh, then the line is considered to be vertical (they are + * more likely to denote a lane) + */ const static double kAngleTresh; - - static int kMode; }; \ No newline at end of file diff --git a/modules/pedestrian-detector/pedestrian_detector.h b/modules/pedestrian-detector/pedestrian_detector.h index 70a3c9b..6c4de23 100644 --- a/modules/pedestrian-detector/pedestrian_detector.h +++ b/modules/pedestrian-detector/pedestrian_detector.h @@ -1,16 +1,18 @@ #pragma once #include -#include using namespace std; using namespace cv; -class PedestrianDetector -{ +class PedestrianDetector { + public: PedestrianDetector(); - // Returns true if any pedestrian has been detected and draws them into "out" + + /* + * Returns true if any pedestrian has been detected and draws them into "out" + */ bool DetectPedestrians(Mat in, Mat out, Scalar color, int thickness); private: diff --git a/modules/speed-estimator/speed_estimator.cpp b/modules/speed-estimator/speed_estimator.cpp index bbf6d14..568620f 100644 --- a/modules/speed-estimator/speed_estimator.cpp +++ b/modules/speed-estimator/speed_estimator.cpp @@ -6,11 +6,14 @@ const int SpeedEstimator::kGapTreshFast = 4; Speed SpeedEstimator::GetSpeed(Mat image, int curr_frame, int mode) { Mat temp, left_lane, right_lane; + static int right_lane_changes = 0; static int left_lane_changes = 0; + // Variable for checking if last frame contains lane static bool right_lane_exists = true; static bool left_lane_exists = true; + int non_zero_pixels, avg_lane_changes; @@ -52,10 +55,6 @@ Speed SpeedEstimator::GetSpeed(Mat image, int curr_frame, int mode) { avg_lane_changes = right_lane_changes > left_lane_changes ? right_lane_changes : left_lane_changes; -#if DEBUG - cout << avg_lane_changes << endl; -#endif - if (avg_lane_changes == 0) { right_lane_changes = 0; left_lane_changes = 0; diff --git a/modules/speed-estimator/speed_estimator.h b/modules/speed-estimator/speed_estimator.h index 2d00407..da09a35 100644 --- a/modules/speed-estimator/speed_estimator.h +++ b/modules/speed-estimator/speed_estimator.h @@ -1,34 +1,35 @@ #pragma once #include -#include using namespace std; using namespace cv; enum Speed { SPD_SLOW, SPD_NORMAL, SPD_FAST, SPD_CURR }; -class SpeedEstimator -{ +class SpeedEstimator { + public: - // Mode: 0 -> If the video quality is low - // Mode: 1 -> If the video quality is middle - // Mode: 2 -> If the video quality is high - static Speed GetSpeed(Mat image, int curr_frame, int mode); + /* + * Mode: 0 -> If the video quality is low + * Mode: 1 -> If the video quality is middle + * Mode: 2 -> If the video quality is high + */ + Speed GetSpeed(Mat image, int curr_frame, int mode); private: - static Mat GetWhiteMask(Mat image, int mode); + Mat GetWhiteMask(Mat image, int mode); /* - Number of frames needed in order to estimate the speed of the vehicle - */ + * Number of frames needed in order to estimate the speed of the vehicle + */ const static int kFrameTresh; /* - Maximum number of gaps detected between lanes per kFrameTresh - in order to decide if the vehicle is going too fast or not - If the number of gaps below this treshold, vehicle is considered - to have normal speed - */ + * Maximum number of gaps detected between lanes per kFrameTresh + * in order to decide if the vehicle is going too fast or not + * If the number of gaps below this treshold, vehicle is considered + * to have normal speed + */ const static int kGapTreshFast; }; \ No newline at end of file diff --git a/modules/stopping-distance-calculator/stopping_distance_calculator.cpp b/modules/stopping-distance-calculator/stopping_distance_calculator.cpp index c30256c..38198f0 100644 --- a/modules/stopping-distance-calculator/stopping_distance_calculator.cpp +++ b/modules/stopping-distance-calculator/stopping_distance_calculator.cpp @@ -7,11 +7,8 @@ bool StoppingDistanceCalculator::IsSafe(Mat image) { Mat temp = GetRedMask(image); double safety_ratio; - temp = temp.rowRange(temp.rows / 2, temp.rows * 4 / 5); - temp = temp.colRange(temp.cols / 3, temp.cols * 2 / 3); - - image = image.rowRange(image.rows / 2, image.rows * 4 / 5); - image = image.colRange(image.cols / 3, image.cols * 2 / 3); + temp = temp.rowRange(temp.rows / 2, temp.rows * 3 / 4); + temp = temp.colRange(temp.cols * 2 / 5, temp.cols * 3 / 5); // Number of non zero pixels / number of all pixels, percentage of red color safety_ratio = (double)(countNonZero(temp)) diff --git a/modules/stopping-distance-calculator/stopping_distance_calculator.h b/modules/stopping-distance-calculator/stopping_distance_calculator.h index 03f6f71..c7d2fe9 100644 --- a/modules/stopping-distance-calculator/stopping_distance_calculator.h +++ b/modules/stopping-distance-calculator/stopping_distance_calculator.h @@ -9,11 +9,13 @@ using namespace cv; class StoppingDistanceCalculator { public: - static bool IsSafe(Mat image); + bool IsSafe(Mat image); private: - static Mat GetRedMask(Mat image); + Mat GetRedMask(Mat image); - // Safety treshold for stopping distance + /* + * Safety treshold for stopping distance + */ const static double kSafetyTresh; }; \ No newline at end of file diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (66).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (1).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (66).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (1).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (75).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (10).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (75).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (10).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (76).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (11).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (76).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (11).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (77).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (12).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (77).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (12).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (78).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (13).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (78).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (13).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (79).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (14).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (79).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (14).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (80).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (15).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (80).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (15).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (81).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (16).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (81).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (16).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (82).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (17).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (82).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (17).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (83).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (18).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (83).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (18).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (84).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (19).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (84).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (19).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (67).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (2).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (67).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (2).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (85).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (20).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (85).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (20).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (86).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (21).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (86).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (21).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (68).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (3).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (68).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (3).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (69).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (4).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (69).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (4).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (70).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (5).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (70).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (5).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (71).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (6).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (71).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (6).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (72).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (7).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (72).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (7).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (73).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (8).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (73).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (8).png diff --git a/modules/traffic-sign-detector-2/tests/Screenshot (74).png b/modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (9).png similarity index 100% rename from modules/traffic-sign-detector-2/tests/Screenshot (74).png rename to modules/traffic-sign-detector-2/tests/traffic-sign-detector-2 (9).png diff --git a/modules/traffic-sign-detector-2/traffic_sign_detector.cpp b/modules/traffic-sign-detector-2/traffic_sign_detector.cpp index 75e7c6c..5c865b4 100644 --- a/modules/traffic-sign-detector-2/traffic_sign_detector.cpp +++ b/modules/traffic-sign-detector-2/traffic_sign_detector.cpp @@ -77,10 +77,6 @@ vector TrafficSignDetector::GetRedTrafficSigns(Mat image) { return detected_signs; } -vector TrafficSignDetector::GetBlueTrafficSigns(Mat image) { - -} - Mat TrafficSignDetector::GetRedMask(Mat image) { // Hue and sturation tresholds @@ -101,8 +97,4 @@ Mat TrafficSignDetector::GetRedMask(Mat image) { tresh_saturation = channels[1] > 50; return (tresh_hue_fst | tresh_hue_snd) & tresh_saturation; -} - -Mat TrafficSignDetector::GetBlueMask(Mat image) { - } \ No newline at end of file diff --git a/modules/traffic-sign-detector-2/traffic_sign_detector.h b/modules/traffic-sign-detector-2/traffic_sign_detector.h index 3148cdc..d5bb285 100644 --- a/modules/traffic-sign-detector-2/traffic_sign_detector.h +++ b/modules/traffic-sign-detector-2/traffic_sign_detector.h @@ -1,23 +1,26 @@ #pragma once +#include + #ifdef DEBUG #include #endif -#include using namespace std; using namespace cv; -class TrafficSignDetector -{ +class TrafficSignDetector { + public: - // Returns the bounding rectangles for detected red traffic signs + /* + * Returns the bounding rectangles for detected red traffic signs + */ static vector GetRedTrafficSigns(Mat image); - static vector GetBlueTrafficSigns(Mat image); private: - // Returns a binary image in which - // 1 corresponds to red area, 0 corresponds to other colors + /* + * Returns a binary image in which 1 corresponds to red area, + * 0 corresponds to other colors + */ static Mat GetRedMask(Mat image); - static Mat GetBlueMask(Mat image); }; \ No newline at end of file diff --git a/modules/traffic-sign-detector/traffic_sign_detector.cpp b/modules/traffic-sign-detector/traffic_sign_detector.cpp index c09e601..05fecba 100644 --- a/modules/traffic-sign-detector/traffic_sign_detector.cpp +++ b/modules/traffic-sign-detector/traffic_sign_detector.cpp @@ -2,17 +2,7 @@ TrafficSignDetector::TrafficSignDetector(Mat image) { - Mat temp; - image.copyTo(temp); - - for (int pi = 0; pi < temp.rows; ++pi) { - for (int pj = 0; pj < temp.cols; ++pj) { - - temp.at(pi, pj)[0] = 0; - temp.at(pi, pj)[1] = 0; - temp.at(pi, pj)[2] = 0; - } - } + Mat temp = Mat::zeros(image.rows, image.cols, image.type()); for (int pi = temp.rows / 8; pi < temp.rows / 4; ++pi) { for (int pj = temp.cols / 3; pj < temp.cols * 2 / 3; ++pj) { @@ -41,23 +31,16 @@ TrafficSignDetector::TrafficSignDetector(Mat image) { } } - TrafficSignDetector::mask_for_elim = temp; + this->mask_for_elim_ = temp; } bool TrafficSignDetector::DetectTrafficSigns( - Mat in, Mat out, SignColor color, Scalar rect_color, int thickness) { + Mat in, Mat out, Scalar rect_color, int thickness) { - Mat mask; + Mat mask = GetBlueMask(in & this->mask_for_elim_); vector > contours; bool is_sign_detected = false; - if (color == CLR_BLUE) { - mask = GetBlueMask(in & mask_for_elim); - } - else { - mask = GetRedMask(in & mask_for_elim); - } - erode(mask, mask, Mat(), Point(-1, -1), 1, 1, 1); dilate(mask, mask, Mat(), Point(-1, -1), 10, 1, 1); @@ -72,28 +55,6 @@ bool TrafficSignDetector::DetectTrafficSigns( return is_sign_detected; } -Mat TrafficSignDetector::GetRedMask(Mat image) { - - // Hue and sturation tresholds - Mat tresh_hue_fst, - tresh_hue_snd, - tresh_saturation, - hsv; - - vector channels; - - // Convert image to HSV, split it to channels - cvtColor(image, hsv, CV_BGR2HSV); - split(hsv, channels); - - // Hue values between 0° to 25° and 335° to 360° - tresh_hue_fst = channels[0] < 25 / 2.0f; - tresh_hue_snd = channels[0] > 335 / 2.0f; - tresh_saturation = channels[1] > 50; - - return (tresh_hue_fst | tresh_hue_snd) & tresh_saturation; -} - Mat TrafficSignDetector::GetBlueMask(Mat image) { // Hue and sturation tresholds diff --git a/modules/traffic-sign-detector/traffic_sign_detector.h b/modules/traffic-sign-detector/traffic_sign_detector.h index 4f89d79..6fa81b9 100644 --- a/modules/traffic-sign-detector/traffic_sign_detector.h +++ b/modules/traffic-sign-detector/traffic_sign_detector.h @@ -1,30 +1,30 @@ #pragma once #include -#include using namespace std; using namespace cv; -// Color of the traffic sign -enum SignColor { CLR_BLUE, CLR_RED }; +class TrafficSignDetector { -class TrafficSignDetector -{ public: - // Initialize with a sample image + /* + * Initialize with a sample image + */ TrafficSignDetector(Mat image); - // Returns true if it detects any traffic signs by color - // and draws the bounding rectangles to matrix out - bool DetectTrafficSigns( - Mat in, Mat out, SignColor color, Scalar rect_color, int thickness); + /* + * Returns true if it detects blue traffic signs and draws the bounding + * rectangles to matrix "out" + */ + bool DetectTrafficSigns(Mat in, Mat out, Scalar rect_color, int thickness); private: - // Returns a binary image in which 1 corresponds to red (or blue) - // area, 0 corresponds to other colors - Mat GetRedMask(Mat image); + /* + * Returns a binary image in which 1 corresponds to blue area, + * 0 corresponds to other colors + */ Mat GetBlueMask(Mat image); - Mat mask_for_elim; + Mat mask_for_elim_; }; \ No newline at end of file diff --git a/rpi3.c b/rpi3.c index 764b208..0800eee 100644 --- a/rpi3.c +++ b/rpi3.c @@ -18,7 +18,7 @@ void hardwareSetup(){ } // Drive motor via gpio output signal -int vibrMtrController(int motor_state){ +int vibrationStateChange(int motor_state){ if(motor_state == 0){ digitalWrite(VIBR_MTR_01,0); digitalWrite(VIBR_MTR_02,0); diff --git a/sample-images/lane-detector-1.png b/sample-images/lane-detector-1.png new file mode 100644 index 0000000..16569a3 Binary files /dev/null and b/sample-images/lane-detector-1.png differ diff --git a/sample-images/lane-detector-2.png b/sample-images/lane-detector-2.png new file mode 100644 index 0000000..cbf38e3 Binary files /dev/null and b/sample-images/lane-detector-2.png differ diff --git a/sample-images/lane-detector-3.png b/sample-images/lane-detector-3.png new file mode 100644 index 0000000..d179a9d Binary files /dev/null and b/sample-images/lane-detector-3.png differ diff --git a/sample-images/lane-detector-4.png b/sample-images/lane-detector-4.png new file mode 100644 index 0000000..0aa4432 Binary files /dev/null and b/sample-images/lane-detector-4.png differ diff --git a/sample-images/lane-detector-5.png b/sample-images/lane-detector-5.png new file mode 100644 index 0000000..5b1f41e Binary files /dev/null and b/sample-images/lane-detector-5.png differ diff --git a/tests/Screenshot (126).png b/sample-images/stopping-distance-calculator-1.png similarity index 100% rename from tests/Screenshot (126).png rename to sample-images/stopping-distance-calculator-1.png diff --git a/tests/Screenshot (127).png b/sample-images/stopping-distance-calculator-2.png similarity index 100% rename from tests/Screenshot (127).png rename to sample-images/stopping-distance-calculator-2.png diff --git a/tests/Screenshot (128).png b/sample-images/stopping-distance-calculator-3.png similarity index 100% rename from tests/Screenshot (128).png rename to sample-images/stopping-distance-calculator-3.png diff --git a/tests/Screenshot (160).png b/sample-images/traffic-sign-detector-1.png similarity index 100% rename from tests/Screenshot (160).png rename to sample-images/traffic-sign-detector-1.png diff --git a/tests/Screenshot (161).png b/sample-images/traffic-sign-detector-2.png similarity index 100% rename from tests/Screenshot (161).png rename to sample-images/traffic-sign-detector-2.png diff --git a/tests/Screenshot (163).png b/sample-images/traffic-sign-detector-3.png similarity index 100% rename from tests/Screenshot (163).png rename to sample-images/traffic-sign-detector-3.png diff --git a/tests/Screenshot (164).png b/sample-images/traffic-sign-detector-4.png similarity index 100% rename from tests/Screenshot (164).png rename to sample-images/traffic-sign-detector-4.png diff --git a/tests/Screenshot (165).png b/sample-images/traffic-sign-detector-5.png similarity index 100% rename from tests/Screenshot (165).png rename to sample-images/traffic-sign-detector-5.png diff --git a/tests/Screenshot (129).png b/tests/Screenshot (129).png deleted file mode 100644 index 1dd763b..0000000 Binary files a/tests/Screenshot (129).png and /dev/null differ diff --git a/tests/Screenshot (130).png b/tests/Screenshot (130).png deleted file mode 100644 index 18690c2..0000000 Binary files a/tests/Screenshot (130).png and /dev/null differ diff --git a/tests/Screenshot (131).png b/tests/Screenshot (131).png deleted file mode 100644 index 79953b7..0000000 Binary files a/tests/Screenshot (131).png and /dev/null differ diff --git a/tests/Screenshot (132).png b/tests/Screenshot (132).png deleted file mode 100644 index c512733..0000000 Binary files a/tests/Screenshot (132).png and /dev/null differ diff --git a/tests/Screenshot (133).png b/tests/Screenshot (133).png deleted file mode 100644 index f346982..0000000 Binary files a/tests/Screenshot (133).png and /dev/null differ diff --git a/tests/Screenshot (134).png b/tests/Screenshot (134).png deleted file mode 100644 index 142916e..0000000 Binary files a/tests/Screenshot (134).png and /dev/null differ diff --git a/tests/Screenshot (135).png b/tests/Screenshot (135).png deleted file mode 100644 index e18969d..0000000 Binary files a/tests/Screenshot (135).png and /dev/null differ diff --git a/tests/Screenshot (136).png b/tests/Screenshot (136).png deleted file mode 100644 index da7d20a..0000000 Binary files a/tests/Screenshot (136).png and /dev/null differ diff --git a/tests/Screenshot (137).png b/tests/Screenshot (137).png deleted file mode 100644 index 263e268..0000000 Binary files a/tests/Screenshot (137).png and /dev/null differ diff --git a/tests/Screenshot (138).png b/tests/Screenshot (138).png deleted file mode 100644 index deb0bf1..0000000 Binary files a/tests/Screenshot (138).png and /dev/null differ diff --git a/tests/Screenshot (139).png b/tests/Screenshot (139).png deleted file mode 100644 index cbddeb7..0000000 Binary files a/tests/Screenshot (139).png and /dev/null differ diff --git a/tests/Screenshot (162).png b/tests/Screenshot (162).png deleted file mode 100644 index 445068b..0000000 Binary files a/tests/Screenshot (162).png and /dev/null differ diff --git a/tests/Screenshot (166).png b/tests/Screenshot (166).png deleted file mode 100644 index d0364f8..0000000 Binary files a/tests/Screenshot (166).png and /dev/null differ diff --git a/tests/Screenshot (167).png b/tests/Screenshot (167).png deleted file mode 100644 index ba8c05e..0000000 Binary files a/tests/Screenshot (167).png and /dev/null differ