霍夫变换直线检测

#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
#include "cxcore.h"

void main(int argc,char** argv)  
{  
    IplImage *srcRGB = cvLoadImage("text.jpg",1);  
  
	cvNamedWindow("src",1);  
    cvShowImage("src",srcRGB); 

	IplImage* src=cvCreateImage(cvSize(srcRGB->width,srcRGB->height),8,1);  
	cvCvtColor(srcRGB,src,CV_RGB2GRAY); 

	IplImage* edge = cvCreateImage(cvSize(src->width,src->height),8,1);  
    cvZero(edge);  
	cvCanny(src,edge,40,90);
	cvNamedWindow("edge",1);  
    cvShowImage("edge",edge);  
 
	IplImage* dst = cvCloneImage(srcRGB);  
	CvMemStorage *storage = cvCreateMemStorage();  
	CvSeq *lines = 0; 
	
/*	lines = cvHoughLines2(edge,storage,CV_HOUGH_STANDARD,1,CV_PI/180,120,0,0);
	//rho单位是像素=1,theta单位是弧度=pi/180,threshold是认定为一条直线时在累计平面中必须达到的值

	for(int i=0;i<MIN(lines->total,100);i++)
	{
		float* line=(float*) cvGetSeqElem(lines,i);
		float rho=line[0];
		float theta=line[1];
		double a=cos(theta),b=sin(theta);
		double x0=rho*a,y0=rho*b;
		CvPoint p1,p2;
		p1.x=cvRound(x0+1000*(-b));
		p1.y=cvRound(y0+1000*a);
		p2.x=cvRound(x0-1000*(-b));
		p2.y=cvRound(y0-1000*a);
		cvLine(dst,p1,p2,CV_RGB(255,0,0),1,8,0);
	}*/

	lines = cvHoughLines2(edge,storage,CV_HOUGH_PROBABILISTIC,1,CV_PI/180,50,0,10);  
	//param1为将要返回的线段的最小长度,param2为一条直线上分离线段不能连成一条直线的分隔像素点数
    for(int i = 0;i<lines->total; ++i)
	{  
        CvPoint *line = (CvPoint*)cvGetSeqElem(lines,i);  
        cvLine(dst,line[0],line[1],CV_RGB(255,0,0),3,8);  
    }  

	cvNamedWindow("dst",1);  
    cvShowImage("dst",dst);  

      
    cvWaitKey(0);  
  
        cvDestroyWindow("src");  
    cvReleaseImage(&src);  


method = CV_HOUGH_PROBABILISTIC方法可以直接取到检测到的线段的两端点,存放于line[0],line[1]:霍夫变换直线检测_第1张图片

而CV_HOUGH_STANDARD方法只能输出检测到直线的极坐标r和theta值,确定不了线段:

霍夫变换直线检测_第2张图片

你可能感兴趣的:(include,float,p2p,DST)