Recently began to learn OPENCV, want to detect the ring on the picture, found that Hough transform can do such an effect, so try to use the Opencv3 Hough transform to do the next ring detection.
OpenCV in the Hough transform function:
The first parameter, the Inputarray type of image, is the 8-bit grayscale single-channel image, which is the source image.
The second argument, the Inputarray type of circles, after calling the Houghcircles function, stores the output vectors of the detected circles, each of which is represented by a floating-point (vec3d) vector (x, y, radius) that contains 3 elements.
The third parameter, the type of int method, that is, the use of detection methods, the current OPENCV in the Hough gradient method can be used, its identifier is cv_hough_gradient, this parameter is filled with this identifier.
The fourth parameter, a double of type DP, is used to detect the inverse of the ratio of the image of the accumulator in the center of the input image, and this parameter allows the creation of an accumulator with a lower resolution than the input image. If the above words are difficult to understand, let's look at examples. For example, if dp= 1 o'clock, the accumulator and the input image have the same resolution. If dp=2, the accumulator has a width and height that is half as large as the input image.
The fifth parameter, the mindist of the double type, is the minimum distance between the center of the circle that the Hough transform detects, that is, the minimum distance between the two different circles that allow our algorithm to differentiate clearly. If this parameter is too small, multiple adjacent circles may be incorrectly detected as a coincident circle. Conversely, if this parameter is set too large, some circles cannot be detected.
The sixth argument, a double of type param1, has a default value of 100. It is the corresponding parameter of the detection method set by the third parameter method. The current only method cv_hough_gradient the Hough gradient method, which represents the high threshold value passed to the canny edge detection operator, while the low threshold is half the high threshold value.
The seventh argument, a double of type param2, also has a default value of 100. It is the corresponding parameter of the detection method set by the third parameter method. The current only method cv_hough_gradient the Hough gradient method, which represents the accumulator threshold at the center of the detection phase. The smaller it is, the more it can detect a circle that doesn't exist, and the bigger it is, the more rounded it is to be able to pass through the circle.
The eighth parameter, Minradius of type int, has a default value of 0, which represents the minimum value of the circle radius.
The Nineth parameter, Maxradius of type int, also has a default value of 0, which represents the maximum value of the circle radius.
Above is online Baidu to, in addition to the fourth parameter, other parameters are very good understanding, the following paste program and the actual effect diagram, the program should pay special attention to the vector type is vec3d and not vec3f, if it is 3f will error, and Hough Transform input requirements are 8 single-channel picture, This can be done by setting the second parameter of Imread to 0 or by using Bgr2gray.
#include <iostream> #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include <
opencv2/highgui/highgui.hpp> #include <vector> #include <boost/concept_check.hpp> using namespace std;
using namespace CV;
int main (int argc, char **argv) {Mat src = imread ("board.jpg");
Imshow ("1", SRC);
if (Src.empty ()) {cout << "can not open" << Endl;
return-1;
} Mat cimg;
Medianblur (SRC, SRC, 5);
Cvtcolor (Src,cimg,color_bgr2gray);
Gaussianblur (cimg, Cimg, Size (9, 9), 2, 2);
Medianblur (Cimg, cimg, 5);
Canny (cimg,cimg,10,250,5);
Imshow ("Canny", cimg);
Vector<vec3f> circles;
Houghcircles (cimg, Circles, hough_gradient, 1, 30,100, 30, 10, 120); for (size_t i = 0; i < circles.size (); i++) {Point Center (cvround (circles[i][0)), Cvround (circles[i][1
]));
int radius = Cvround (circles[i][2]); Draw Center Circle (SRC, ceNter, 3, Scalar (0, 255, 0),-1, 8, 0);
Draw Circle Contour Circle (SRC, center, RADIUS, Scalar (155, 50, 255), 3, 8, 0);
} imshow ("Picture", SRC);
Cv::waitkey (0);
return 0;
}
After the smooth filtering Gaussian filtering edge detection, the effect as shown above, can be seen or can be better recognized. However, the same parameters are significantly less effective on other images.
Could be said to be a very failure.
Change the parameter to:
Houghcircles (cimg, Circles, hough_gradient, 1, 10,100, 35, 10, 30);
Then get the effect
The error rate is greatly reduced, but the detection effect is unsatisfactory, the specific parameters of how to adjust, this will continue to learn in the future