首页 > 解决方案 > 使用 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

标签: c++opencv

解决方案


该错误意味着该函数convexHull需要将点坐标存储为浮点数而不是双精度数。为了克服这个问题,将向量的定义更改为:

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

推荐阅读