首页 > 代码库 > opencv2实现多张图片路线路牌(直线和圆)检测并将处理后的图片合成视频_计算机视觉大作业2

opencv2实现多张图片路线路牌(直线和圆)检测并将处理后的图片合成视频_计算机视觉大作业2

linefinder.h

#if !defined LINEF
#define LINEF
#include<cmath>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define PI 3.1415926
using 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.1415926

int 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 instance
		LineFinder ld;

		// Set probabilistic Hough parameters
		ld.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 circles
			200, // Canny high threshold 
			100, // minimum number of votes 
			25, 50); // min and max radius

		cout << "Circles: " << circles.size() << endl;

		// Draw the circles

		vector<Vec3f>::const_iterator itc= circles.begin();

		while (itc!=circles.end()) {

			circle(image, 
				Point((*itc)[0], (*itc)[1]), // circle centre
				(*itc)[2], // circle radius
				Scalar(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 = http://www.mamicode.com/cvLoadImage(cname);  >

opencv2实现多张图片路线路牌(直线和圆)检测并将处理后的图片合成视频_计算机视觉大作业2