1

我已经查看并尝试解决在 c++ 中使用 OpenCV 2.4 计算凸度缺陷中的类似问题,但他的解决方案似乎对我不起作用。

// Get contours
vector<vector<cv::Point> > contours;
findContours(inputMat, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

vector<vector<cv::Point> > hulls(contours.size());
vector<vector<int> > intHulls(contours.size());
vector<vector<Vec4i> > defects(contours.size());

for(int i = 0; i < contours.size(); i++){
    convexHull(Mat(contours[i]), hulls[i]);
    convexHull(Mat(contours[i]), intHulls[i]);
    convexityDefects(contours[i], intHulls[i], defects[i]);
}

我总是得到:

OpenCV Error: Assertion failed (ptnum > 3) in convexityDefects, file /Users/user/slave/ios_framework/src/opencv/modules/imgproc/src/contours.cpp, line 1969

所以我去下载了源代码来查看contours.cpp,第1969行。

1965 void cv::convexityDefects( InputArray _points, InputArray _hull, OutputArray _defects )
1966 {
1967     Mat points = _points.getMat();
1968     int ptnum = points.checkVector(2, CV_32S);
1969     CV_Assert( ptnum > 3 );

看起来它正在尝试制作轮廓点的垫子,然后检查垫子是否......什么?在http://docs.opencv.org/上没有任何关于 checkVector 的文档,但是谷歌搜索发现了这个文档

int cv::Mat::checkVector    (   int     elemChannels,
    int     depth = -1,
    bool    requireContinuous = true 
    )        const

returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise

在读了几次之后,我仍然完全迷失了,不确定是什么导致了这个错误,或者我能做些什么来修复它。上周我一直在尝试自己解决这个整体的 convexityDefects() 问题,但一点运气都没有。

编辑:我只是仔细检查了几件事以确保安全。

我的输入垫总是 100 列 x 120 行,就像它应该的那样。但是,我认为轮廓会像在屏幕上看起来一样平滑和连续,但显然不是。当我将它们转换为垫子并检查尺寸时,它们几乎总是 1 col x XXX 行。所以我想我的问题是我必须得到一个二维轮廓——有没有办法把它们连接在一起?它们在我的屏幕上看起来无缝。

编辑2:即使在适当的垫子上绘制轮廓并传递给我同样的“断言失败(ptnum> 3)”错误。我真的不知道现在该怎么办。

4

0 回答 0