使用 opencv 凸胡尔 获取给定点的面积. - 点到垫子转换期间发生错误

Using opencv convexHul Get area of given points. - Errors occurred during points to Mat conversion

本文关键字:转换 错误 胡尔 opencv 获取 使用      更新时间:2023-10-16

我点击了这个链接来制作点的向量。为了计算给定点的面积,我使用了凸包。为此,我遵循了这个。当我尝试计算凸体内部的面积时,发生了以下错误。正如我在将矢量点转换为 Mat 后注意到的那样,没有深度。CV_Assert(total >= 0 && (depth == CV_32F || depth == CV_32S));我如何克服这个问题。任何帮助,不胜感激。提前谢谢你。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <math.h>
using namespace std;
using namespace cv;
int main()
{
vector<Point2d> originalPoints;
vector<Point2d> hull;
vector<Point2f> contour;
double epsilon = 0.001;
for(int dataPointCount=0; dataPointCount < 10; dataPointCount++)
{
cv::Point2d point;
point.x = 10 * ( (double)rand() / (double)RAND_MAX ) + 2; // just genarate random point
point.y = 5 * ( (double)rand() / (double)RAND_MAX ) + 2;
originalPoints.push_back(point);
}
convexHull(Mat(originalPoints) , hull , true);
approxPolyDP(Mat(hull), contour, 0.001, true);
cout << "====>"<< fabs(contourArea(Mat(contour)));
return 0;
}

错误

OpenCV Error: Assertion failed (total >= 0 && (depth == CV_32F || depth == CV_32S)) in convexHull, file /home/ve/workspace/opencv-3.3.0/modules/imgproc/src/convhull.cpp, line 136
terminate called after throwing an instance of 'cv::Exception'
what():  /home/ve/workspace/opencv-3.3.0/modules/imgproc/src/convhull.cpp:136: error: (-215) total >= 0 && (depth == CV_32F || depth == CV_32S) in function convexHull

该错误意味着函数convexHull要求将点坐标存储为浮点数而不是双精度。要解决此问题,请将向量的定义更改为:

vector<Point2f> originalPoints;
vector<Point2f> hull;
vector<Point2f> contour;