Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating convexityDefects using OpenCV 2.4 in c++
    primarykey
    data
    text
    <p>I'm using OpenCV 2.4 to calculate the convex hull of a image. </p> <p>I'm also doing some processing to remove some noise from the image, which is not really relevant to the question.</p> <p>The code to calculate the convexHull is the following:</p> <pre><code>... cv::Mat sourceImage; // assume something is already here please cv::vector&lt;cv::Vec4i&gt; hierarchy; std::vector&lt;std::vector&lt;cv::Point&gt; &gt; 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&lt;cv::vector&lt;cv::Point&gt; &gt;hull( contours.size() ); for (int i = 0; i &lt; contours.size(); i++) { convexHull( contours[i], hull[i], false ); } ... </code></pre> <p>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:</p> <pre><code>cv::Vec4i defects; convexityDefects(cv::Mat(contours), hull, defects); </code></pre> <p>Doing this I get this error:</p> <pre><code>OpenCV Error: Assertion failed (ptnum &gt; 3) in convexityDefects, file ./opencv/opencv/modules/imgproc/src/contours.cpp, line 1969 </code></pre> <p>Any ideas on what I'm doing wrong when using convexityDefects? </p> <p><a href="http://opencv.itseez.com/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=convexitydefects#cv.ConvexityDefects" rel="nofollow" title="Opencv 2.4 ConvexityDefects documentation">Opencv convexityDefects documentation</a></p> <p>Thanks in advance.</p> <p><strong>UPDATE</strong></p> <p>Thanks to Innuendo answer I updated the main loop code to:</p> <pre><code>std::vector&lt;Vec4i&gt; defects; vector&lt;cv::vector&lt;cv::Point&gt; &gt;hull( contours.size() ); for (int i = 0; i &lt; contours.size(); i++) { convexHull( contours[i], hull[i], false ); convexityDefects(contours[i], hull[i], defects[i]); } </code></pre> <p>Using this, the error that I now get is:</p> <pre><code>OpenCV Error: Assertion failed (hull.checkVector(1, CV_32S) &gt; 2) in convexityDefects </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload