project-street

Top_Secret :P
git clone git://archive.git.mtrnord.blog/MTRNord/project-street.git
Log | Files | Refs | README | LICENSE

road_detection.cpp (2313B)


      1 #include <opencv2/opencv.hpp>
      2 #include <opencv2/highgui/highgui.hpp>
      3 #include <opencv2/imgproc/imgproc.hpp>
      4 #include <opencv2/core/core.hpp>
      5 #include <iostream>
      6 #include <stdio.h>
      7 
      8 
      9 using namespace std;
     10 using namespace cv;
     11 
     12 class findroad{			//class that separates out roads from images
     13 private:
     14     Mat path;
     15 public:
     16     void setpath(Mat& image)
     17     {
     18         image.convertTo(path, CV_32S);
     19     }
     20 
     21     Mat getroad(Mat &image)                                         //major working function attribute of the class
     22     {        
     23 		watershed(image, path);                                     //using watershed segmenter
     24         path.convertTo(path,CV_8U);
     25         return path;
     26     }
     27 };
     28 
     29 int main( int argc, const char** argv )
     30 {
     31     Mat image1 = imread(argv[1],1);
     32 	Mat image;
     33 	resize(image1,image,Size(500,500),0,0,INTER_LINEAR);
     34     Mat gray;
     35     cvtColor(image, gray, CV_BGR2GRAY);
     36     threshold(gray, gray, 100, 255, THRESH_BINARY);                 //threasholding the grayscale image
     37     double t = 0;
     38 	t = (double)cvGetTickCount();                                   //setting up timer
     39     Mat Erode;
     40     erode(gray,Erode,Mat(),Point(2,2),7);   //eroding image 7 times.Experimentally best output given by 7 time erode
     41     Mat Dilate;                              
     42     dilate(gray,Dilate,Mat(),Point(2,2),7);                          //dilating the image
     43     threshold(Dilate,Dilate,1, 50,THRESH_BINARY_INV);
     44     Mat path_trace(gray.size(),CV_8U,Scalar(0));
     45     path_trace= Erode+Dilate;                                      //morphological addition
     46     findroad road;                                                 //creating an object for our class
     47     road.setpath(path_trace);                                      //preparing the input
     48     namedWindow("founded road");
     49 	namedWindow("input image");
     50     Mat road_found = road.getroad(image);                  
     51     road_found.convertTo(road_found,CV_8U);                       
     52     imshow("founded road", road_found);
     53 	imshow("input image",image);
     54     t = (double)cvGetTickCount() - t;                              //time of detection
     55     printf( "road got detected in = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
     56 	cout << endl << "cheers" << endl;
     57 	imwrite("ROAD.jpg",image);
     58 	imwrite( "ROAD_DETECTED.jpg",road_found);
     59     waitKey(0);
     60 	return 0;
     61 }