Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use convexityDefects in Opencv 2.4
    primarykey
    data
    text
    <p>I try to use <code>convexHull</code> and <code>convexityDefects</code> to define a hand. But when the program hit the <code>convexityDefects</code>, there is always an error says "vector subscript out of range". here is my code.</p> <p>Did I use the function in a wrong way? </p> <pre><code>#include "stdafx.h" #include &lt;opencv2\opencv.hpp&gt; #include &lt;math.h&gt; #include &lt;iostream&gt; using namespace cv; using namespace std; const int w = 500; int levels = 3; vector&lt;vector&lt;Point&gt; &gt; contours; vector&lt;Vec4i&gt; hierarchy; void on_trackbar(int, void*) { Mat cnt_img = Mat::zeros(w, w, CV_8UC3); int _levels = levels - 3; drawContours( cnt_img, contours, _levels &lt;= 0 ? 3 : -1, Scalar(128,255,255), 3, CV_AA, hierarchy, std::abs(_levels) ); imshow("contours", cnt_img); } int main( int argc, char**) { Mat img = imread("out.jpg",0); threshold(img,img,200,255,cv::THRESH_BINARY); namedWindow( "image", 1 ); imshow( "image", img ); //Extract the contours so that vector&lt;vector&lt;Point&gt; &gt; contours0; findContours( img, contours0, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE); contours.resize(contours0.size()); for( size_t k = 0; k &lt; contours0.size(); k++ ){ std::cout&lt;&lt;"contours0.size"&lt;&lt;contours0[k].size()&lt;&lt;endl; approxPolyDP(Mat(contours0[k]), contours[k], 0, true); } std::vector&lt;Vec4i&gt; defects; vector&lt;cv::vector&lt;int&gt; &gt;hull( contours.size() ); for (int i = 0; i &lt; contours.size(); i++) { std::cout&lt;&lt;"contours.size"&lt;&lt;contours[i].size()&lt;&lt;endl; convexHull( contours[i], hull[i], false ); if (contours[i].size() &gt;150 ) { convexityDefects(contours[i], hull[i], defects[i]); std::cout&lt;&lt;"defects"&lt;&lt;defects[i].depth&lt;&lt;endl; } } namedWindow( "contours", 1 ); createTrackbar( "levels+3", "contours", &amp;levels, 7, on_trackbar ); on_trackbar(0,0); waitKey(); return 0; } </code></pre> <p>Thank you in advance!</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    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