There is a problem, clearly seems to be closed boundary, choose one of the points with pointpolygontest test result does not, only to get the correct result, after convexhull
Forget the great god answer
CodePudding user response:
Can you give a look at the image processing results, canny out there are a lot of tiny burrs, I think this could be the causeCodePudding user response:
Fyi:const static Scalar colors [15]={
CV_RGB (0, 0128),
CV_RGB (0128, 0),
CV_RGB (0128128),
CV_RGB (128, 0, 0),
CV_RGB (128, 0128),
CV_RGB (128128, 0),
CV_RGB (128128128),
CV_RGB (160160160),
CV_RGB (0, 0255),
CV_RGB (0255, 0),
CV_RGB (0255255),
CV_RGB (255, 0, 0),
CV_RGB (255, 0255),
CV_RGB (255255, 0),
CV_RGB (255255255),
};
Mat img, gray, bw;
VectorVector Contours.
FindContours (bw, contours, hierarchy, RETR_EXTERNAL CHAIN_APPROX_SIMPLE);
if (! Contours. The empty () & amp; & ! Hierarchy. The empty ()) {
Independence idx=0;
n=0;
VectorApprox.
For (; Idx>=0; Independence idx=hierarchy [independence idx] [0]) {
Color=colors [15] independence idx %;
//drawContours (smallImg, contours, independence idx, color, and 1, 8, hierarchy);
ApproxPolyDP (Mat (contours [independence idx]), approx, arcLength (Mat (contours [independence idx]), true) * 0.005, true);//0.005 will flash straightening coefficient
Const Point * p=& amp; Approx [0].
Int m=(int) approx. The size ();
Polylines (smallImg, & amp; P, & amp; M, 1, true, color);
Circle (smallImg, Point (p [0]. X, p [0]. Y), 2, color);
Circle (smallImg, Point (p [1]. X, p [1]. Y), 1, color);
n++;
If (1==n) {
Maxrect=boundingRect (Mat (contours [independence idx]));
} else {
Brect=boundingRect (Mat (contours [independence idx]));
CvRect Mr (maxrect), br (brect);
Maxrect=cvMaxRect (& amp; Mr, & amp; Br);
}
}
Circle (smallImg, Point (maxrect. X + maxrect. Width/2, maxrect. Y + maxrect. Height/2), 2, CV_RGB (0, 255));
}
Imshow (" display ", smallImg);
WaitKey (0);