Hof transform Line Detection

Source: Internet
Author: 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 ); // The unit of rock is pixel = 1, theta is radian = pi/180, and threshold is the value that must be reached in the cumulative plane when it is regarded as a straight line (int I = 0; I <MIN (lines-> total, 100); I ++) {float * line = (float *) cvGetSeqElem (lines, I); float rock = line [0]; float theta = line [1]; double a = cos (theta), B = sin (theta); double x0 = rock * a, y0 = rock * 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 (, 0), 0);} */lines = cvHoughLines2 (edge, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/18,); // param1 is the minimum length of the line segment to be returned, param2 is a separate line segment on a straight line and cannot be connected to a straight line to separate the pixel points 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 method can directly obtain the points at both ends of the detected line segment, stored in line [0], line [1]:

The cv_hough_standard method can only output the R and theta values of the detected straight line, but cannot determine the line segment:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.