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://Origina L Image Mat img; Vector containing the end points//of the detected lines vector<vec4i> lines; Accumulator resolution parameters double Deltarho; Double Deltatheta; Minimum number of votes that a line//must receive before being considered int minvote; Min length for a line double minLength; Max allowed Gap along the line double maxgap; Public://Default accumulator resolution are 1 pixel by 1 degree//no gap, no mimimum length Linefinder (): Deltarho (1 ), Deltatheta (pi/180), Minvote (Ten), MinLength (0.), Maxgap (0.) {}//Set the resolution of the accumulator void Setaccresolution (double Drho, double dtheta) {deltarho= Drho; Deltatheta= Dtheta; }//Set the minimum number of votes void setminvote (int minv) { Minvote= MINV; }//Set line length and Gap void setlinelengthandgap (double length, double gap) {minlength= length; Maxgap= Gap; }//Apply probabilistic Hough Transform vector<vec4i> findlines (mat& binary) {lines.clear (); Houghlinesp (Binary,lines,deltarho,deltatheta,minvote, MinLength, maxgap); return lines; }//Draw the detected lines on an image void Drawdetectedlines (Mat &image, Scalar color=scalar (255,0,0)) {//Draw The Lines Vector<vec4i>::const_iterator it2= lines.begin (); while (It2!=lines.end ()) {Point pt1 ((*IT2) [0], (*it2) [1]); Point Pt2 ((*IT2) [2], (*IT2) [3]); Double slope = fabs (double ((*it2) [1]-(*it2) [3])/((*IT2) [0]-(*it2) [2])); Double slope = fabs ((double) (LINES[1].Y-LINES[0].Y)/(double) (lines[1].x-lines[0].x));//Find the slope of the line in the coordinate system//double Length=sqrt ((LINE[1].Y-LINE[0].Y) * (LINE[1].Y-LINE[0].Y) + (line[1].x-line[0].x) * (line[1].x-line[0].x)); Length of////segment if ((slope>0.3) && (slope<1000) && (length>50) && (length<500))//{//line[0].y= line[0].y+roi_rect_src.y;//line[1].y =line[1].y+roi_rect_src.y;//cvline ( Frame, line[0], line[1], Cv_rgb (255,0,0), 3, CV_AA, 0);//}if ((slope>0.52) && (slope<2)) {line (image, PT1, p T2, color,3,8,0);} ++it2; }}}; #endif
Main.cpp
#include <stdio.h> #include <iostream> #include <vector> #include <opencv2/core/core.hpp>//# Include <opencv2/imageproc/imageproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <string > #include <sstream> #include "linefinder.h"//#include "edgedetector.h" #include <opencv2\opencv.hpp> Using namespace cv;using namespace std;//#define PI 3.1415926int Main () {StringStream ss; String str; StringStream SSS; String STRs; for (int i=1;i<=80;i++) {str= "c:\\users\\hsn\\desktop\\ line detection \ \ line detection \ Job 2\\";//select f:\\ picture \ \ 5 pictures ss.clear (); ss<<str; ss<<i; ss<< ". jpg"; ss>>str; Mat Image=imread (str,1); Read input Image//mat image= imread ("1.jpg", 1); if (!image.data) return 0; /*namedwindow ("Original image"), Imshow ("Original image", image); */mat img=image (Rect (0.4*image.cols,0.58* Image.rows,0.4*image.cols,0.3*image.rows)); Mat contours; Canny (img,contours,80,100); Mat Contoursinv;threshold (Contours,contoursinv,128,255,thresh_bINARY_INV);//Display the image of Contours/*namedwindow ("Canny contours"); Imshow ("Canny contours", CONTOURSINV); */// Create linefinder instancelinefinder ld;//Set probabilistic Hough parametersld.setlinelengthandgap (60,40); Ld.setminvote (+);vector<vec4i> li= ld.findlines (contours); Ld.drawdetectedlines (IMG);/*namedWindow ("HoughP "); Imshow (" HOUGHP ", IMG), *//*namedwindow (" detected Lines with HOUGHP "); Imshow (" Detected Lines with HOUGHP ", image); */ Mat Imggry;cvtcolor (Image,imggry,cv_bgr2gray); Gaussianblur (Imggry,imggry,size (5,5), 1.5);vector<vec3f> circles; Houghcircles (Imggry, Circles, cv_hough_gradient, 2,//accumulator resolution (size of the IMAGE/2),//Minimum D Istance between-circles200,//Canny high threshold,//minimum number of votes 25, 50); Min and Max radiuscout << "Circles:" << circles.size () << endl;//Draw the Circlesvector<vec3f> ;:: Const_iterator itc= Circles.begin (); while (Itc!=circles.end ()) {Circle (image, point (*iTC) [0], (*ITC) [1]),//Circle Centre (*ITC) [2],//Circle Radiusscalar (255),//color 2); THICKNESS++ITC;} /*namedwindow (str); imshow (str,image); */strs= "c:\\users\\hsn\\desktop\\ line detection \ \ line detection \ \";//select f:\\ pictures \ \ 5 pictures Sss.clear (); sss<<strs; sss<<i; sss<< ". jpg"; sss>>strs; Imwrite (strs,image);} int num = 1; Cvsize size = cvsize (1024,960); Video frame format size double fps = 3;//<span style= "White-space:pre" > </span>//frame rate per second cvvideowriter *writer = CvC Reatevideowriter ("c:\\users\\hsn\\desktop\\ linear detection \ \ Line Detection \\1.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>Set the read picture 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;}
OPENCV2 implementation of multiple picture road signs (lines and circles) detection and the processing of the picture synthesis video _ Computer Vision Big Job 2