Tuesday, September 2, 2014

Traffic Sign Detection




Project 1: Traffic Sign Detection using OpenCV


Introduction


The automatic traffic sign recognition is not a recent theme of study, with work done at over a decade ago. In the year of 1993, a research program PROMETHEUS (PROgraMme for a European Traffic with Highest Efficiency and Unprecedented Safety) with the goal to do the autonomous driving feasible. For that purpose, visual interpretation was one of the major focuses of the research. However, image processing revealed to be very time consuming, being problematic, since fast responses are needed as cars move at high velocities.


Video



Using Code


Here is the complete code for traffic sign detection using the opencv. Following code completed using visual studio 2008 and Opencv Libraries.

Process of Setting up the visual Studio -  Click to View.

User Libraries and Study Materials - Click to View



Code for Traffic Sign Detection Using OpenCV


/**
Program: detect triangle and circles
*/

#include <cv.h>
#include <highgui.h>
#include <vector>


using namespace cv;

void in_rang_filter();


IplImage *img ,*img2,*test,*imgHsv,*imgResult;
int mR_val=35,mG_val=80,mB_val=115,MAR_val=256,MAG_val=256,MAB_val=256;

int main(int argc, char** argv)
{
      //CAPTURE VIDEO FROM FILE
      CvCapture *capture = cvCaptureFromFile( "E://Users//SOHAM//Desktop//Kas Lake Video//Video0002.mp4" ) ;

      while(true)
      {    
            //RETRIVES THE FRAME
            img = cvQueryFrame( capture );
            cvWaitKey(10);

            //BREAK AFTER END
            if( !img )  break;
           
            const int w = img->width;
            const int h = img->height;
            imgResult = cvCreateImage( cvSize( w , h ) , IPL_DEPTH_8U , 1 ) ;
            imgHsv=cvCreateImage( cvSize( w , h ) , IPL_DEPTH_8U , 3 ) ;

            cvNamedWindow( "Capture" , CV_WINDOW_AUTOSIZE ) ;
            cvNamedWindow( "Capture1" , CV_WINDOW_AUTOSIZE ) ;

            //CHANGE COLOR-SPACE TO HSV
            cvCvtColor( img , imgHsv , CV_BGR2HSV ) ;
           
            //FILTER THE FRAME
            in_rang_filter();

            cvWaitKey( 10 );
      }

      // RELEASE RESOURCE AND DESTROY IT
      cvReleaseImage( &imgResult ) ;
      cvReleaseImage( &imgHsv ) ;
      cvDestroyAllWindows() ;
      cvReleaseCapture( &capture ) ;
}


void in_rang_filter()
{
      //---------------------IMAGE FILTERING
      IplImage* test = cvCreateImage( cvGetSize(img) , IPL_DEPTH_8U , 3);
      CvScalar min_color = CV_RGB( mR_val , mG_val , mB_val);
      CvScalar max_color = CV_RGB( MAR_val , MAG_val , MAB_val);
      //---------------------SEARCH FOR RED COLOR
      cvInRangeS( imgHsv , min_color , max_color , imgResult);
      cvCvtColor( imgResult , test ,CV_GRAY2RGB );

      /////-------------------READ OLY LEFT HALH REAGION----------------------------------
      cvSetImageROI( imgResult , cvRect( 0 , 0 , ( imgResult->width ) / 2 , ( ( imgResult->height ) / 2 ) ) ) ;
      ///----------------------------------------------------------------------

      Mat tst=( imgResult ) ;
      Mat gray1 ;

      threshold( tst , gray1 , 200 , 255, THRESH_BINARY ) ;
      GaussianBlur( gray1 , gray1 ,Size( 9 , 9 ) , 3.1 , 1.1 ) ;

      vector<Vec3f> circles;
      //---------------------FINDS CIRCLES IN IMAGE
      HoughCircles( gray1 , circles , CV_HOUGH_GRADIENT , 2 , 200 , 10 , 10 , 1 , 20 ) ;
      for( size_t i = 0 ; i < circles.size() ; i++ )
      {
            Point center( cvRound( circles[i][0] ) , cvRound( circles[i][1] ) ) ;
            int radius = cvRound( circles[i][2] ) ;
            int x = cvRound( circles[i][0] ) - radius ;
            int y = cvRound( circles[i][1] ) - radius ;

            //-----------------CROP IMAGE
            cvSetImageROI( img , cvRect( x , y , ( radius * 2 ) + 10 , ( radius * 2 ) + 10 ) ) ;
            img2 = cvCreateImage( cvGetSize( img ) , img->depth , img->nChannels ) ;
            cvCopy( img , img2 , NULL) ;
            cvResetImageROI( img ) ;

            if( img2->height <= 35 && img2->width <= 35 )
                                          cvShowImage( "Capture1" , img2 ) ;
           
            //--------------------DRAW THE RECTANGLE
            cvRectangle( img , cvPoint( ( x - 5 ) , ( y - 5 ) ) , cvPoint( ( x + ( radius * 2 ) + 10 ) ,
                  ( y + ( radius * 2 ) + 10 ) ) , Scalar( 255 , 0 , 255 ) , 1 , 8 , 0 ) ;                      
      }
      cvShowImage( "Capture" , img ) ;
      cvReleaseImage( &test ) ;
      cvReleaseImage( &img2 ) ;

}

Point of Interest

  • Learn how to set up OpenCV with Visual Studio.
  • Learn how to capture video from file.
  • Learn how to process the image.
  • Learn how to filter the color space .

References