Skip to content

Commit

Permalink
Final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ozcanovunc committed May 14, 2016
1 parent 7246aa0 commit c044c9b
Show file tree
Hide file tree
Showing 62 changed files with 199 additions and 203 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
114 changes: 67 additions & 47 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,107 +1,127 @@
#include <opencv2/opencv.hpp>
#include <iostream>

#include "modules/speed-estimator/speed_estimator.h"
#include "modules/lane-detector/lane_detector.h"
#include "modules/pedestrian-detector/pedestrian_detector.h"
#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<Vec4i> lanes;
vector<Rect> 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<Rect> 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;
}
}
46 changes: 17 additions & 29 deletions modules/lane-detector/lane_detector.cpp
Original file line number Diff line number Diff line change
@@ -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<Vec3b>(pi, pj)[0] = 0;
temp.at<Vec3b>(pi, pj)[1] = 0;
temp.at<Vec3b>(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<Vec3b>(pi, pj)[0] = 255;
Expand All @@ -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<Mat> 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;
}
Expand All @@ -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);
Expand All @@ -75,7 +62,7 @@ Mat LaneDetector::GetWhiteMask(Mat image, bool apply_mask) {

vector<Vec4i> LaneDetector::GetLanes(Mat image) {

Mat white_mask = GetWhiteMask(image, true);
Mat white_mask = this->GetWhiteMask(image, true);
vector<Vec4i> lanes;

erode(white_mask, white_mask, Mat(), Point(-1, -1), 1, 1, 1);
Expand All @@ -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);
}
}
}
Expand Down
63 changes: 43 additions & 20 deletions modules/lane-detector/lane_detector.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
#pragma once

#include <opencv2/opencv.hpp>
#include <iostream>

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<Vec4i> GetLanes(Mat image);
static void DrawLanes(Mat image, vector<Vec4i> lanes, Scalar color, int thickness);

/*
* Returns true if the vehicle is out of lane
*/
bool IsOutOfLane(Mat image);

vector<Vec4i> GetLanes(Mat image);
void DrawLanes(Mat image, vector<Vec4i> 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;
};
10 changes: 6 additions & 4 deletions modules/pedestrian-detector/pedestrian_detector.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#pragma once

#include <opencv2/opencv.hpp>
#include <vector>

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:
Expand Down
Loading

0 comments on commit c044c9b

Please sign in to comment.