Note that there are some explanatory texts on larger screens.

plurals
  1. POContours / Connected components in OpenCV 2.4.4
    primarykey
    data
    text
    <p>I'm currently working on an image with a lot of detected contours. My goal is to narrow down the number of contours to end up with only the one I'm looking for. For that I conduct a bunch of tests based on area and bounding box.</p> <p>For now I do after every step a <code>drawContours</code> for the contours that I want to keep followed by a <code>findContours</code>.</p> <p><strong>My problem is</strong> that I would like to do <code>findContours</code> only once and then just erase the contours I don't want, is this possible?</p> <p>Current way :</p> <pre><code>Mat src; Mat BW; src = imread("img.bmp", 0); if( src.channels() &gt; 1) { cvtColor(src, src, CV_BGR2GRAY); } threshold(src, BW, 100, 255, CV_THRESH_OTSU); imshow( "Tresh", BW ); Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3); Mat dstP = Mat::zeros(src.rows, src.cols, CV_8UC3); Mat dst1 = Mat::zeros(src.rows, src.cols, CV_8UC3); Mat dst2 = Mat::zeros(src.rows, src.cols, CV_8UC3); vector&lt;vector&lt;Point&gt; &gt; contours; vector&lt;Vec4i&gt; hierarchy; findContours( BW, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); for( int i = 0; i &lt; contours.size(); i++ ) { Scalar color( rand()&amp;255, rand()&amp;255, rand()&amp;255 ); drawContours( dst, contours, i, color, 2/*CV_FILLED*/, 8, hierarchy ); } /// Test on area ****************************************************************** for( int i = 0; i &lt; contours.size(); i++ ) { if ( contourArea(contours[i], false) &gt; 100 &amp;&amp; contourArea(contours[i], false) &lt; 200000) { Scalar color( rand()&amp;255, rand()&amp;255, rand()&amp;255 ); drawContours( dst1, contours, i, color, CV_FILLED, 8, hierarchy ); } } /// Next test ********************************************************************** cvtColor(dst1, dstP, CV_BGR2GRAY); findContours( dstP, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); </code></pre> <p>etc</p> <p>Wanted way :</p> <pre><code>if ( contourArea(contours[i], false) &lt; 100 &amp;&amp; contourArea(contours[i], false) &gt; 200000) { contours.erase(i); // Doesn't work } </code></pre> <p>Does anyone now how to erase those contours?</p> <p>PS : I don't care about inner contours, I want all of them to go trough my tests.</p> <hr> <p><strong>EDIT</strong>, the solution (pointed out by limonana) is : <code>contours.erase(contours.begin()+i);</code></p>
    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.
    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