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:,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

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
Mat path;
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
return path;

int main( int argc, const char** argv )
Mat image1 = imread(argv[1],1);
Mat image;
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);
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_DETECTED.jpg",road_found);
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.



This result was obtained in 114.475 ms



This result was obtained in 98.91 ms




This result was obtained in 110.336 ms




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




This result was obtained in 114.656 ms




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

Any modified code could be mailed to this address:

Your contribution is highly appriciated.

Thank You 🙂

See also :-


Leave a Reply

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

You are commenting using your 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