[OpenCV Getting Started Guide] Article 7 Line Segment Detection and Circle Detection
The Section 5 contour detection in [OpenCV Getting Started Guide] and section 6 contour detection in [OpenCV Getting Started Guide] Explain the contour detection of OpenCV. This article describes how to use Line Segment Detection and Circle Detection in OpenCV.
Line Segment Detection and Circle Detection mainly use the Hough transform, which is a method that uses the Global features of the image to connect the edge of a specific shape to form a continuous smooth edge. It shadows vertices from the source image to the parameter space used for accumulation to recognize known analytic curves.
In OpenCV programming, Line Segment Detection and Circle Detection have been encapsulated into functions. You can directly use cvHoughLines2 and cvHoughCircles. Let's take a look at the function introduction and actual code.
OpenCV Getting Started Guide series Article address: http://blog.csdn.net/morewindows/article/category/863841
Function: detects a line segment in an image.
Function prototype:
CvSeq * cvHoughLines2 (
CvArr * image,
Void * line_storage,
Int method,
Double Rock,
Double theta,
Int threshold,
Double param1 = 0, double param2 = 0
);
Parameter description:
The first parameter indicates the input image, which must be a binary image (black-and-white image ).
The second parameter indicates the storage container. Like the Profile Detection in the previous article, you can input a CvMemStorage pointer.
The third parameter represents the variable transformation. The following values can be removed:
CV_HOUGH_STANDARD-traditional or standard Hough transformation. each line segment is represented by two floating point numbers (p, θ), where p is the distance between the line segment and the origin (0, 0), and the angle between the θ line segment and the x-axis.
CV_HOUGH_PROBABILISTIC-probabilistic Hough Transformation (higher efficiency if the image contains long linear segmentation ). It returns the line segment instead of the entire line segment. Each split is represented by the start and end points.
CV_HOUGH_MULTI_SCALE-multi-scale variant of the traditional Hough transformation. The line segment is encoded in the same way as CV_HOUGH_STANDARD.
The fourth parameter indicates the Distance Precision of the pixel-related units.
The fifth parameter indicates the angle accuracy of the radian measurement.
The sixth parameter indicates the maximum number of lines to be detected. If so many lines have been detected, the function returns.
The seventh parameter is related to the third parameter. Its meaning is as follows:
Do not use (0) for traditional Hough transformations ).
This is the minimum line segment length.
For the multi-scale Hough transformation, it is the denominator of the Distance Precision, and the approximate distance precision is, and the precise precision should be ).
The eighth parameter is related to the third parameter. Its meaning is as follows:
Do not use (0) for traditional Hough transformations ).
This parameter represents the maximum gap between broken line segments on the same line segment ), that is, when the interval between two broken lines on the same line segment is less than param2, It is combined into one.
For multi-scale Hough transformation, it is the denominator of angle precision theta (the approximate angle precision is theta, and the precise angle should be theta/param2 ).
The principle of Line Segment Detection is still a bit complicated. If you are interested, please read the professional books. The following is a sample code:
// Line Segment Detection in the image // By MoreWindows (http://blog.csdn.net/MoreWindows) # include <opencv2/opencv. hpp> using namespace std; # pragma comment (linker, "/subsystem: \" windows \ "/entry: \" mainCRTStartup \ "") int main () {const char * pstrWindowsSrcTitle = "source image (http://blog.csdn.net/MoreWindows)"; const char * pstrWindowsLineName = "Line Segment Detection"; // load source image IplImage * pSrcImage = cvLoadImage ("201.jpg ", CV_LOAD_IMAGE_UNCHANGED); // grayscale image IplImage * pGrayImage = cvCreateImage (cvGetSize (pSrcImage), IPL_DEPTH_8U, 1); cvCvtColor (pSrcImage, pGrayImage, CV_BGR2GRAY ); // edge image IplImage * pCannyImage = cvCreateImage (cvGetSize (pSrcImage), priority, 1); cvkan (pGrayImage, pCannyImage, 30, 90); // cvSmooth (pCannyImage, pCannyImage ); // Line Segment Detection (for binary images only) CvMemStorage * pcvMStorage = cvcreatemstorage (); double fRho = 1; double fTheta = CV_PI/180; int nMaxLineNumber = 50; // maximum number of lines detected: double fMinLineLen = 50; // Minimum Line Segment Length: double fMinLineGap = 10; // Minimum Line Segment interval: CvSeq * pcvSeqLines = cvHoughLines2 (pCannyImage, pcvMStorage, Hangzhou, fRho, fTheta, nMaxLineNumber, fMinLineLen, fMinLineGap); // draw a line segment IplImage * pColorImage = cvCreateImage (cvGetSize (pSrcImage), watermark, 3); cvCvtColor ); int I; for (I = 0; I <pcvSeqLines-> total; I ++) {CvPoint * line = (CvPoint *) cvGetSeqElem (pcvSeqLines, I); cvLine (pColorImage, line [0], line [1], CV_RGB (255, 0, 0), 2);} cvNamedWindow (partition, CV_WINDOW_AUTOSIZE); cvShowImage (pstrWindowsSrcTitle, pSrcImage); cvNamedWindow (pstrWindowsLineName, values); cvShowImage (pstrWindowsLineName, pColorImage); cvWaitKey (0); values (& pcvMStorage); cvDestroyWindow (values); cvDestroyWindow (pstrWindowsLineName); cvReleaseImage (& pSrcImage ); cvReleaseImage (& pGrayImage); cvReleaseImage (& pCannyImage); cvReleaseImage (& pColorImage); return 0 ;}
The running result is as follows:
The original form of the cvHoughCircles function used by the Circle Detection Function is as follows:
CVAPI (CvSeq *) cvHoughCircles (
CvArr * image, void * circle_storage,
Int method,
Double dp,
Double min_dist,
Double param1 CV_DEFAULT (100 ),
Double param2cv_default (100 ),
Int min_radius CV_DEFAULT (0 ),
Int max_radius CV_DEFAULT (0)
);
We can see that cvHoughCircles is similar to the cvHoughLines2 function above, so the meaning of the following parameters can be described:
The second parameter represents the Hough transformation method. Currently, only CV_HOUGH_GRADIENT can be used.
The third parameter indicates the cumulative resolution of the arc center. It is usually set to 1.
The fourth parameter represents the minimum distance between two different circles. Because the distance is calculated by the center of the circle, the detection of concentric circles is powerless.
Note that the Circle Detection Function can use grayscale images.
The Circle Detection code is as follows:
// Circle Detection in the image // By MoreWindows (http://blog.csdn.net/MoreWindows) # include <opencv2/opencv. hpp> using namespace std; # pragma comment (linker, "/subsystem: \" windows \ "/entry: \" mainCRTStartup \ "") int main () {const char * pstrWindowsSrcTitle = "source image (http://blog.csdn.net/MoreWindows)"; const char * pstrWindowsLineName = "Circle Detection"; // load the source image IplImage * pSrcImage = cvLoadImage ("201.jpg ", watermark); // grayscale image IplImage * pGrayImage = cvCreateImage (cvGetSize (pSrcImage), watermark, 1); cvCvtColor (pSrcImage, pGrayImage, watermark); // cvSmooth (pGrayImage, pGrayImage ); // Circle Detection (grayscale) CvMemStorage * pcvMStorage = cvCreateMemStorage (); double fMinCircleGap = pGrayImage-> height/10; CvSeq * pcvSeqCircles = cvHoughCircles (pGrayImage, pcvMStorage, done, 1, fMinCircleGap); // each circle is represented by three floating point numbers: Center Coordinate (x, y) and radius // draw a straight line IplImage * pColorImage = cvCreateImage (cvGetSize (pSrcImage), IPL_DEPTH_8U, 3); cvCvtColor (pGrayImage, pColorImage, CV_GRAY2BGR); int I; for (I = 0; I <pcvSeqCircles-> total; I ++) {float * p = (float *) cvGetSeqElem (pcvSeqCircles, I); cvCircle (pColorImage, cvPoint (cvRound (p [0]), cvRound (p [1]), cvRound (p [2]), CV_RGB (255, 0, 0), 2);} cvNamedWindow (rows, CV_WINDOW_AUTOSIZE); cvShowImage (pstrWindowsSrcTitle, pSrcImage); cvNamedWindow (pstrWindowsLineName, values); cvShowImage (pstrWindowsLineName, pColorImage); cvWaitKey (0); values (& pcvMStorage); cvDestroyWindow (values); cvDestroyWindow (pstrWindowsLineName); cvReleaseImage (& pSrcImage ); cvReleaseImage (& pGrayImage); cvReleaseImage (& pColorImage); return 0 ;}
The running result is as follows. It can be seen that the circle detection accuracy is not high.
[OpenCV Getting Started Guide] Chapter 5 contour detection, [OpenCV Getting Started Guide] section 6 contour detection, and section 7 Line Segment Detection and Circle Detection this article introduces the contour detection of images, line Segment Detection and Circle Detection.
The following sections describe the histogram of images, which is very helpful for Statistical Image Information and Image Enhancement Based on these information. Welcome to [OpenCV Getting Started Guide] Chapter 8 grayscale histogram, [OpenCV Getting Started Guide] Chapter 9 grayscale histogram equalization, and [OpenCV Getting Started Guide] Article 10 color histogram equalization.
Articles in the OpenCV Getting Started Guide series:
Http://blog.csdn.net/morewindows/article/category/1291764
Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8266985
Welcome to Weibo: http://weibo.com/MoreWindows