#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: