Main.cpp
#include <stdio.h> #include <iostream> #include <vector> #include <opencv2/core/core.hpp># Include <opencv2/highgui/highgui.hpp> #include <string> #include <sstream> #include "linefinder.h" # Include<time.h> #include <opencv2\opencv.hpp> using namespace cv;using namespace Std;int main () { StringStream SS; String str; StringStream SSS; String STRs; clock_t Start,finish; Double totaltime; Start=clock (); for (int i=1;i<=80;i++) {str= "c:\\users\\hsn\\desktop\\ line detection \ \ line detection \ \ Job 2\\";//Select 80 pictures ss.clear (); ss<<str; ss<<i; ss<< ". jpg"; ss>>str; Mat Image=imread (str,1); Read in Picture if (!image.data) return 0; Mat Img=image (Rect (0.4*image.cols,0.58*image.rows,0.4*image.cols,0.3*image.rows));//select area of interest//canny transform mat contours; Canny (img,contours,80,100); Mat Contoursinv;threshold (CONTOURS,CONTOURSINV,128,255,THRESH_BINARY_INV); Linefinder a variable of type Ld;//linefinder, call header file function//set hough probability parameter Ld.setlinelengthandgap (60,40); Ld.setminvote (30);//CheckTest line vector<vec4i> li= ld.findlines (contours);//Draw Straight Ld.drawdetectedlines (IMG);/////////Detect round Mat Imggry;cvtcolor ( Image,imggry,cv_bgr2gray); Gaussianblur (Imggry,imggry,size (5,5), 1.5);//Gaussian smoothing vector<vec3f> circles; Houghcircles (Imggry, Circles, cv_hough_gradient, 2,//accumulator resolution (half of image size) 150,//two circle min distance between,//canny transform high threshold 100,//Minimum support number 25, 50); Min and Max radius cout << "Circles:" << circles.size () << endl;//Output Circle number///////////////////Draw Circle vector<vec3f >::const_iterator itc= Circles.begin (); while (Itc!=circles.end ()) {Circle (image, point (*ITC) [0], (*ITC) [1]),//Center (*ITC) [2],//RADIUS scalar (255),//color 2); Width ++ITC;} Save Picture strs= "c:\\users\\hsn\\desktop\\ line detection \ \ line detection \ \";//Select 80 pictures sss.clear (); sss<<strs; sss<<i; sss<< ". jpg"; sss>>strs; Imwrite (strs,image);//write to}finish=clock (); Totaltime= (Double) (Finish-start)/clocks_per_sec; cout<< "\ nthe average run time per frame is" <<totaltime/80<< "seconds! "<<endl;////////////////////will picture composite video int num = 1; CvsizE size = cvsize (1024,960); The size of the video frame format double fps = 10;////frame rate per second cvvideowriter *writer = Cvcreatevideowriter ("c:\\users\\hsn\\desktop\\ line detection \ \ Line Detection \ \ Route signs detection. avi ", -1,fps,size); Create video file char cname[100]; sprintf (CNAME, "c:\\users\\hsn\\desktop\\ line detection \ \ line detection \ \ \\%d.jpg after processing", num); Load picture folder, picture name number is 1 start 1,2,3,4,5 .... Iplimage *src = cvloadimage (CNAME); if (!SRC) {return 0; } iplimage *src_resize = Cvcreateimage (size,8,3); Create a video file format size picture Cvnamedwindow ("avi"); while (SRC) {cvshowimage ("avi", src_resize); Cvwaitkey (1); Cvresize (src,src_resize); <span style= "White-space:pre" > </span>//the picture to be read is set to the same size as the video format cvwriteframe (writer,src_resize); Save picture for Video stream format cvreleaseimage (&SRC);//<span style= "White-space:pre" > </span>//Free space num++; sprintf (CNAME, "c:\\users\\hsn\\desktop\\ line detection \ \ line detection \ \ \\%d.jpg after processing", num); src = cvloadimage (CNAME); Cyclic reading of data} cvreleasevideowriter (&writer); Cvreleaseimage (&src_resize); Waitkey (); return 0;}
LineFinder.h
#if!defined linef#define linef#include<cmath> #include <opencv2/core/core.hpp> #include <opencv2/ Imgproc/imgproc.hpp> #define PI 3.1415926using namespace cv;using namespace Std;class linefinder {private://Original image mat Img;vector<vec4i> lines;//contains the end point of the detection Line//accumulator resolution double deltarho;double deltatheta;//line is supported by the minimum number of int minvote;// The shortest length of the line double minlength;//maximum allowable error double maxgap;public://default accumulator resolution is pi/180//no maximum allowable error, no minimum length Linefinder (): Deltarho (1), Deltatheta (pi/180), Minvote (Ten), MinLength (0.), Maxgap (0.) {}//Set accumulator resolution void Setaccresolution (double Drho, double dtheta) {deltarho= drho;deltatheta= Dtheta;} Set minimum support number void setminvote (int minv) {minvote= MINV;} Set maximum allowable error and minimum line length void setlinelengthandgap (double length, double gap) {minlength= length;maxgap= gap;} Applying probability Hough Transform vector<vec4i> findlines (mat& binary) {lines.clear (); Houghlinesp (Binary,lines,deltarho,deltatheta,minvote, MinLength, maxgap); return lines;} Draw the line found on the image void Drawdetectedlines (Mat &image, Scalar COLOR=SCAlar (255,0,0)) {//Draw a straight line vector<vec4i>::const_iterator it2= lines.begin (); while (It2!=lines.end ()) {Point pt1 (* IT2) [0], (*it2) [1]); Point Pt2 ((*IT2) [2], (*IT2) [3]);d ouble slope = fabs (double ((*it2) [1]-(*it2) [3])/((*IT2) [0]-(*it2) [2]));//Calculate slope if (( slope>0.53) && (slope<2)//If the slope satisfies the range to draw line {lines (image, Pt1, pt2, color,3,8,0);} ++it2;}}; #endif
OPENCV2 realization of road Signs detection _ Computer Vision Big Job 2 Final edition