有没有办法将 OpenCV 中的 minAreaRect 与 double 一起使用

Is there a way to use minAreaRect from OpenCV with double?

本文关键字:double 一起 minAreaRect 中的 OpenCV 有没有      更新时间:2023-10-16

我在3D环境和相机中工作,所以我在一边有一些3D坐标,在另一边玩OpenCV。

我需要找到一个多边形的最小边界矩形,考虑 2D 坐标,并希望为此使用 OpenCV。

问题是我的坐标以双精度表示。

我试过了:

  std::vector<cv::Point2d> poly {{1.1, 2.2}, {3.3, 4.4}, {5.5, 6.6}, {7.7, 8.8}};
  cv::RotatedRect box = cv::minAreaRect(poly); // crashes here
  cv::Point2f corners[4];
  box.points(corners);

但出现以下错误:

OpenCV 错误:断言失败 (points.checkVector(2)>= 0 && (points.depth() == CV_32F || points.depth() == CV_32S)) in minAreaRect, file/build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/contours.cpp, 第 1913 行

如果我使用一些Point而不是Point2d则生成的矩形的坐标将被截断

// narrowing conversions
std::vector<cv::Point2d> poly {{1.1, 2.2}, {3.3, 4.4}, {5.5, 6.6}, {7.7, 8.8}};
cv::RotatedRect box = cv::minAreaRect(poly);
cv::Point2f corners[4];
box.points(corners);

不是百分百确定我是否以正确的方式使用 OpenCV,但我偶然发现了这一点,并希望避免编写自己的旋转卡尺函数。

谢谢!

minAreaRect只接受PointPoint2f,即类型为CV_32SCV_32F的点。

如果float点具有足够的精度,则可以使用Point2f而不是Point2d

std::vector<cv::Point2f> poly{ { 1.1f, 2.2f }, { 3.3f, 4.4f }, { 5.5f, 6.6f }, { 7.7f, 8.8f } };
cv::RotatedRect box = cv::minAreaRect(poly); // now it works!