欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > cv::RotatedRect通过3点构造函数程序崩溃

cv::RotatedRect通过3点构造函数程序崩溃

2025/7/4 3:56:02 来源:https://blog.csdn.net/Kelvin_Yan/article/details/143644905  浏览:    关键词:cv::RotatedRect通过3点构造函数程序崩溃
RotatedRect(const Point2f& point1, const Point2f& point2, const Point2f& point3);

debug抛出异常,崩在这里

 CV_Assert( abs(vecs[0].dot(vecs[1])) / (norm(vecs[0]) * norm(vecs[1])) <= FLT_EPSILON );

原因是此处采用单精度浮点运算,导致明明两条边是垂直的也大于float的机器精度了

解决办法:
自己写一个生成旋转矩形的函数,将机器精度替换为更低的精度

RotatedRect MyRotatedRect(const Point2f& _point1, const Point2f& _point2, const Point2f& _point3)
{RotatedRect rotrect;Point2f _center = 0.5f * (_point1 + _point3);Vec2f vecs[2];vecs[0] = Vec2f(_point1 - _point2);vecs[1] = Vec2f(_point2 - _point3);// check that given sides are perpendicularCV_Assert(abs(vecs[0].dot(vecs[1])) / (norm(vecs[0]) * norm(vecs[1])) <= 1e-5);  //此处会导致报异常,其实是float精度不足!// wd_i stores which vector (0,1) or (1,2) will make the width// One of them will definitely have slope within -1 to 1int wd_i = 0;if (abs(vecs[1][1]) < abs(vecs[1][0])) wd_i = 1;int ht_i = (wd_i + 1) % 2;float _angle = atan(vecs[wd_i][1] / vecs[wd_i][0]) * 180.0f / (float)CV_PI;float _width = (float)norm(vecs[wd_i]);float _height = (float)norm(vecs[ht_i]);rotrect.center = _center;rotrect.size = Size2f(_width, _height);rotrect.angle = _angle;return rotrect;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词