c++ - Best fit a circle from a binary image using contours or any other technique -
i've have binary image computed algorithms. there hole in image , want best fit circle in hole. tried using bestminenclosingcircle
function don't give best results.
here binary image
here function
here expected
i want exclude part
here code finding contours
vector<vec4i> hierarchy; vector<vector<point> > contours; findcontours(src, contours, hierarchy, retr_tree, chain_approx_simple, point(0, 0));
check code below
#include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include <iostream> using namespace cv; using namespace std; int main(int, char** argv) { mat src, src_gray; /// load source image , convert gray src = imread("e:/test/iffz9.png"); resize(src, src, size(), 0.25, 0.25); /// convert image gray , blur cvtcolor(src, src_gray, color_bgr2gray); blur(src_gray, src_gray, size(3, 3)); imshow("source", src); mat threshold_output; vector<vector<point> > contours; /// detect edges using threshold threshold(src_gray, threshold_output, 127, 255, thresh_binary); /// find contours findcontours(threshold_output, contours, retr_tree, chain_approx_simple); (size_t = 0; < contours.size(); i++) { if (contours[i].size() > 50) { rotatedrect minellipse = fitellipse(contours[i]); int size = min(minellipse.size.width, minellipse.size.height) / 2; // ellipse if (size < src.rows / 2) ellipse(src, minellipse.center, size(size, size), 0, 360, 0, scalar(0, 0, 0255), 2, 8); } } imshow("contours", src); waitkey(0); return 0; }
Comments
Post a Comment