【问题标题】:Exception with Convexity Defects有凸缺陷的例外
【发布时间】:2013-04-09 17:22:53
【问题描述】:

我正在尝试从以下代码中获取凸面缺陷,但不断收到未处理的异常。 我做错了什么?

vector<Vec4i> defects;
ContourPoly = vector<Point>(contour.size());
approxPolyDP( Mat(contour), ContourPoly,20, false );
convexHull(Mat(ContourPoly), HullPoints, false, true);
// The following line wont work
convexityDefects(Mat(ContourPoly),HullPoints,defects);

虽然 HullPoints 的类型是 vector&lt;Point&gt; 异常如下

OpenCV Error: Assertion Failed (ptnum >3) is unknown function, file ..\..\..\src\opencv\modules\imgproc\src\contours.cpp, line 1969

但是使用vector&lt;Point&gt; defects;vector&lt;Vec4i&gt; defects 我得到以下异常

 OpenCV Error: Assertion Failed (hull.checkVector(1,CV_32S) is unknown function, file ..\..\..\src\opencv\modules\imgproc\src\contours.cpp, line 1971

【问题讨论】:

  • 以下哪个原因会触发异常?你的调试说明了什么?
  • 我刚刚添加了一个例外
  • ptnum &gt;3 断言意味着您的HullPoints 包含少于convexityDefects 所需的最少3 个点
  • 不走运,我又试了一次,现在它给了我上面提到的第二个错误\

标签: visual-c++ opencv convexity-defects


【解决方案1】:

defects 应该是vector&lt;Vec4i&gt;

来自文档:

每个凸面缺陷表示为 4 元素整数向量(又名cv::Vec4i):(start_index, end_index, farthest_pt_index, fixpt_depth),其中索引是凸面缺陷开始、结束和最远点的原始轮廓中从 0 开始的索引,并且@ 987654325@ 是最远轮廓点和船体之间距离的定点逼近(带有 8 个小数位)。即得到深度的浮点值将是fixpt_depth/256.0

【讨论】:

  • 但是我已经将convexHull方法的最后一个参数设置为true,返回vector,缺陷不应该也是vector的吗?还是我弄错了?
  • 只针对convexityDefects
【解决方案2】:

首先

vector<vector<Vec4i> > defects;

应该是:

vector<vector<Vec4i> > defects( contour.size() );

另外,在调用convexityDefects函数之前,检查HullPoints的大小是否大于3。

【讨论】:

    猜你喜欢
    • 2013-02-20
    • 2011-10-12
    • 2012-06-13
    • 1970-01-01
    • 2015-09-29
    • 2012-10-10
    • 1970-01-01
    • 2012-01-05
    • 2014-01-29
    相关资源
    最近更新 更多