使用OpenCV 2.4在c ++中计算凸度缺陷 [英] Calculating convexityDefects using OpenCV 2.4 in c++

查看:787
本文介绍了使用OpenCV 2.4在c ++中计算凸度缺陷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenCV 2.4来计算图像的凸包。



我也在做一些处理,以从图像中移除一些噪音,这与问题并不真正相关。



计算convexHull的代码如下:

  ... 
cv :: Mat sourceImage ; //假设某项已经在这里请
cv :: vector< cv :: Vec4i>层次;
std :: vector< std :: vector< cv :: Point> >轮廓;

cv :: findContours(sourceImage,contour,hierarchy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,cv :: Point(0,0));

//为每个轮廓找到凸包对象
向量< cv :: vector< cv :: Point> > hull(contours.size());

for(int i = 0; i {
convexHull(contoururs [i],hull [i],false);
}
...

现在拥有convexhull和轮廓想要计算船体的凸度缺陷,通过查看opencv文档,我认为这将是这样:

  cv :: Vec4i缺陷; 
convexityDefects(cv :: Mat(contour),hull,defects);

这样做时出现错误:

  OpenCV错误:在convexityDefects中的断言失败(ptnum> 3),文件./opencv/opencv/modules/imgproc/src/contours.cpp,行1969 

使用convexityDefects时,我在做什么错误的任何想法?



Opencv convexityDefects文档



提前感谢。



UPDATE



感谢Innuendo回答,我将主循环代码更新为:

  std :: vector< Vec4i>缺陷; 
vector< cv :: vector< cv :: Point> > hull(contours.size());

for(int i = 0; i {
convexHull(contoururs [i],hull [i],false);
convexityDefects(contour [i],hull [i],defects [i]);使用这个,我现在得到的错误是:




b
$ b OpenCV错误:无效(hull.checkVector(1,CV_32S)> 2)在convexityDefects


解决方案

从openCV wiki:


找到轮廓的凸性缺陷。


所以你应该把它包含在你的循环中。

  std :: vector< Vec4i>缺陷; 
vector< cv :: vector< int> > hull(contours.size());

for(int i = 0; i< contours.size(); i ++)
{
convexHull(contoururs [i],hull [i],false);
convexityDefects(contour [i],hull [i],defects [i]);
}

此外,正如你所说,在wiki中说:


船体 - 输出凸包。它是索引的整数向量
或点的向量。在第一种情况下,船体元素是原始数组中的凸包点的基于0的
索引(因为凸包点的集合
是原始点集的子集)。在
第二种情况下,外壳元素是凸包本身。



I'm using OpenCV 2.4 to calculate the convex hull of a image.

I'm also doing some processing to remove some noise from the image, which is not really relevant to the question.

The code to calculate the convexHull is the following:

...
cv::Mat sourceImage; // assume something is already here please
cv::vector<cv::Vec4i> hierarchy;    
std::vector<std::vector<cv::Point> > contours;

cv::findContours( sourceImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE,cv::Point(0, 0));

// Find the convex hull object for each contour
vector<cv::vector<cv::Point> >hull( contours.size() );

for (int i = 0; i < contours.size(); i++)
{
    convexHull( contours[i], hull[i], false );
}
...

Having both the convexHull and the contours I now want to calculate the convexityDefects of the hull(s), which by looking at opencv documentation I thought it would be like this:

cv::Vec4i defects;  
convexityDefects(cv::Mat(contours),  hull, defects);

Doing this I get this error:

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

Any ideas on what I'm doing wrong when using convexityDefects?

Opencv convexityDefects documentation

Thanks in advance.

UPDATE

Thanks to Innuendo answer I updated the main loop code to:

std::vector<Vec4i> defects; 
vector<cv::vector<cv::Point> >hull( contours.size() );

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

Using this, the error that I now get is:

OpenCV Error: Assertion failed (hull.checkVector(1, CV_32S) > 2) in convexityDefects

解决方案

From openCV wiki :

Finds the convexity defects of a contour.

So you should include it in your loop.

std::vector<Vec4i> defects; 
vector<cv::vector<int> >hull( contours.size() );

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

Also, as you mentioned, in wiki is said:

hull – Output convex hull. It is either an integer vector of indices or vector of points. In the first case, the hull elements are 0-based indices of the convex hull points in the original array (since the set of convex hull points is a subset of the original point set). In the second case, hull elements are the convex hull points themselves.

这篇关于使用OpenCV 2.4在c ++中计算凸度缺陷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆