FAST ROAD/PATH DETECTION USING OPENCV

AUTHORS:Abhishek Kumar Annamraju,Akashdeep Singh,Adhesh Shrivastava

Hello Friends

Today I would like to discuss with you how we are trying to implement road detection for our robotics project of developing a hexapod.

Map building,object detection,obstacle detection etc,all emerge out clearly when we successfully implement background detection.Robotics enthusiasts like to call this background detection as “basic path detection”.

OpenCV has always been our favourite tool for image processing,so again we are using the same for road detection.

Inspired by the great working of watershed algorithm for image segmentation(RESEARCH PAPER:http://www.ias-iss.org/ojs/IAS/article/viewFile/852/755),we wanted to implement it for detection of path.The main aim of this code is to segment out road as FAST as possible,but we also need to take care of the accuracy.

The code goes here.
You can download the code from https://github.com/abhi-kumar/Hexapod/blob/master/road_detection.cpp

Click once somewhere on the code and press ctrl+A to select whole code.You may not see the whole code so its better to copy the code and paste it in your favourite text editor and then go through it.

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

class findroad{                              //class that separates out roads from images
private:
Mat path;
public:
void setpath(Mat& image)
{
image.convertTo(path, CV_32S);
}

Mat getroad(Mat &image)                  //major working function attribute of the class
{
watershed(image, path);                                     //using watershed segmenter
path.convertTo(path,CV_8U);
return path;
}
};

int main( int argc, const char** argv )
{
Mat image1 = imread(argv[1],1);
Mat image;
resize(image1,image,Size(500,500),0,0,INTER_LINEAR);
Mat gray;
cvtColor(image, gray, CV_BGR2GRAY);
threshold(gray, gray, 100, 255, THRESH_BINARY);          //threasholding the grayscale image
double t = 0;
t = (double)cvGetTickCount();                                //setting up timer
Mat Erode;
erode(gray,Erode,Mat(),Point(2,2),7); //eroding image 7 times.
Mat Dilate;
dilate(gray,Dilate,Mat(),Point(2,2),7);    //dilating the image
threshold(Dilate,Dilate,1, 50,THRESH_BINARY_INV);
Mat path_trace(gray.size(),CV_8U,Scalar(0));
path_trace= Erode+Dilate;                                      //morphological addition
findroad road;                                             //creating an object for our class
road.setpath(path_trace);                                      //preparing the input
namedWindow("founded road");
namedWindow("input image");
Mat road_found = road.getroad(image);
road_found.convertTo(road_found,CV_8U);
imshow("founded road", road_found);
imshow("input image",image);
t = (double)cvGetTickCount() - t;                              //time of detection
printf( "road got detected in = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
cout << endl << "cheers" << endl;
imwrite("ROAD1.jpg",image);
imwrite( "ROAD1_DETECTED.jpg",road_found);
waitKey(0);
return 0;
}

********************************************************************************

We are still developing the code to get better and better results.
The folowing were our best outcomes

This result was obtained in 109.48 ms.

ROAD1

ROAD1_DETECTED

***************************************************************************************
This result was obtained in 114.475 ms

ROAD1

ROAD1_DETECTED
****************************************************************************************

This result was obtained in 98.91 ms

ROAD3

ROAD3_DETECTED

****************************************************************************************

This result was obtained in 110.336 ms

ROAD4

ROAD4_DETECTED

***************************************************************************************

But still as the image gets more complex,more crowded with objects the result fades away,for example the two following road detections:

******************************************************************************
This result was obtained in 107.042 ms

ROAD5

ROAD5_DETECTED

**********************************************************************************

This result was obtained in 114.656 ms

ROAD6

ROAD6_DETECTED

***********************************************************************************

Well with vehicles and other objects the code becomes more vulnerable.We are trying our level best to come up with an optimim solution.

Note:I have made the code open source so that everyone could benefit from it,also it can be modified by others to function in a better way.You can download the code from here https://github.com/abhi-kumar/Hexapod/blob/master/road_detection.cpp

Any modified code could be mailed to this address:
abhishek4273@gmail.com

Your contribution is highly appriciated.

Thank You 🙂

See also :- http://blindperception.wordpress.com/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s