Optimize&Fix fitEllipse sample

Optimize&Fix fitEllipse sample
This commit is contained in:
TuNanTang 2023-03-14 21:17:39 +08:00
parent b204c39815
commit 68e2df56e7

View File

@ -218,6 +218,11 @@ int main( int argc, char** argv )
return 0;
}
inline static bool isGoodBox(const RotatedRect& box) {
//size.height >= size.width awalys,only if the pts are on a line or at the same point,size.width=0
return (box.size.height <= box.size.width * 30) && (box.size.width > 0);
}
// Define trackbar callback function. This function finds contours,
// draws them, and approximates by ellipses.
void processImage(int /*h*/, void*)
@ -276,39 +281,30 @@ void processImage(int /*h*/, void*)
{
vector<Point2f> pts = points[i];
if (pts.size()<=5) {
//At least 5 points can fit an ellipse
if (pts.size()<5) {
continue;
}
if (fitEllipseQ) {
box = fitEllipse(pts);
if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 ||
MAX(box.size.width, box.size.height) <= 0 ||
MIN(box.size.width, box.size.height) <= 0){continue;};
if (isGoodBox(box)) {
paper.drawEllipseWithBox(box, fitEllipseColor, 3);
}
}
if (fitEllipseAMSQ) {
boxAMS = fitEllipseAMS(pts);
if( MAX(boxAMS.size.width, boxAMS.size.height) > MIN(boxAMS.size.width, boxAMS.size.height)*30 ||
MAX(box.size.width, box.size.height) <= 0 ||
MIN(box.size.width, box.size.height) <= 0){continue;};
if (isGoodBox(boxAMS)) {
paper.drawEllipseWithBox(boxAMS, fitEllipseAMSColor, 2);
}
}
if (fitEllipseDirectQ) {
boxDirect = fitEllipseDirect(pts);
if( MAX(boxDirect.size.width, boxDirect.size.height) > MIN(boxDirect.size.width, boxDirect.size.height)*30 ||
MAX(box.size.width, box.size.height) <= 0 ||
MIN(box.size.width, box.size.height) <= 0 ){continue;};
if (isGoodBox(boxDirect)){
paper.drawEllipseWithBox(boxDirect, fitEllipseDirectColor, 1);
}
}
if (fitEllipseQ) {
paper.drawEllipseWithBox(box, fitEllipseColor, 3);
}
if (fitEllipseAMSQ) {
paper.drawEllipseWithBox(boxAMS, fitEllipseAMSColor, 2);
}
if (fitEllipseDirectQ) {
paper.drawEllipseWithBox(boxDirect, fitEllipseDirectColor, 1);
}
paper.drawPoints(pts, cv::Scalar(255,255,255));
paper.drawPoints(pts, fitEllipseTrueColor);
}
imshow("result", paper.img);