c++ - 使用 opencv convexHul 获取给定点的面积。- 在点到 Mat 转换过程中发生错误
问题描述
我按照这个链接制作点向量。为了计算给定点的面积,我使用了凸包。为此,我遵循了这个。当我尝试计算凸包内的面积时,会发生跟随错误。正如我在将矢量点转换为 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;
推荐阅读
- apache-spark - 如何自动为我的 csv 数据设置架构?
- javascript - 如何将数组中的下一个值加载到我的函数中
- python - 将数据附加到 csv 列 python
- java - 片段问题,片段不兼容
- jupyter-notebook - 我无法在 Jupyter 中创建新文件
- ag-grid-react - 可以将 ag-grid agSelectCellEditor 配置为允许选择多个选项吗?
- reactjs - 在 React 中单击按钮添加 1
- html - 更改 HTML 选择箭头背景颜色
- react-native - 如何在 React Native 中同时使用 headerRight 和 headerLeft?
- php - PHP如何防止邮件在标头中泄漏脚本和IP