霍夫變換直線檢測

來源:互聯網
上載者:User
#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]:

而CV_HOUGH_STANDARD方法只能輸出檢測到直線的極座標r和theta值,確定不了線段:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.