標籤:opencv2 視頻 lines circle 路線路牌檢測
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 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 is 1 pixel by 1 degree // no gap, no mimimum length LineFinder() : deltaRho(1), deltaTheta(PI/180), minVote(10), 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)));//求直線在座標系中的斜率//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));////線段的長度//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, pt2, 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\\直線檢測\\直線檢測\\作業2\\";//選擇F:\\圖片\\中的5張圖片 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(30);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) 150, // minimum distance between two circles200, // Canny high threshold 100, // 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\\直線檢測\\直線檢測\\處理後\\";//選擇F:\\圖片\\中的5張圖片 sss.clear(); sss<<strs; sss<<i; sss<<".jpg"; sss>>strs; imwrite(strs,image);}int num = 1; CvSize size = cvSize(1024,960); //視訊框架格式的大小 double fps = 3;// <span style="white-space:pre"> </span>//每秒鐘的幀率 CvVideoWriter *writer = cvCreateVideoWriter("C:\\Users\\hsn\\Desktop\\直線檢測\\直線檢測\\1.avi",-1,fps,size); //建立視頻檔案 char cname[100]; sprintf(cname,"C:\\Users\\hsn\\Desktop\\直線檢測\\直線檢測\\處理後\\%d.jpg",num); //載入圖片的檔案夾,圖片的名稱編號是1開始1,2,3,4,5.。。。 IplImage *src = cvLoadImage(cname); if (!src) { return 0; } IplImage *src_resize = cvCreateImage(size,8,3); //建立視頻檔案格式大小的圖片 cvNamedWindow("avi"); while (src) { cvShowImage("avi",src_resize); cvWaitKey(1); cvResize(src,src_resize); //<span style="white-space:pre"> </span>//將讀取的圖片設定為視頻格式大小相同 cvWriteFrame(writer,src_resize); //儲存圖片為視頻流格式 cvReleaseImage(&src);// <span style="white-space:pre"> </span> //釋放空間 num++; sprintf(cname,"C:\\Users\\hsn\\Desktop\\直線檢測\\直線檢測\\處理後\\%d.jpg",num); src = cvLoadImage(cname); //迴圈讀取資料 } cvReleaseVideoWriter(&writer); cvReleaseImage(&src_resize); waitKey();return 0;}
opencv2實現多張圖片路線路牌(直線和圓)檢測並將處理後的圖片合成視頻_電腦視覺大作業2