c++ - Subtract all contours from image except the one with the largest area -
cv::mat thr; std::vector<std::vector<cv::point> > contours; std::vector<std::vector<cv::vec4i> > hierarchy; int largest_area = 0; int largest_contour_index = 0; cv::findcontours( thr, contours, hierarchy,cv_retr_ccomp, cv_chain_approx_simple ); // find contours in image for( int = 0; < contours.size(); i++ ) // iterate through each contour. { double = contourarea( contours[i], false ); // find area of contour if(a > largest_area) { largest_area = a; largest_contour_index = i; // store index of largest contour } }
what should after finding index of largest contour? how can delete other contours inner areas? image binary (cv::mat thr). black background white areas. thanks.
in case, deleting contours inner areas equal fill them black. can done drawing contour regions black color:
for (size_t i=0; i<contours.size(); ++i) { if (i != largest_contour_index) { // not largest 1 cv::drawcontours(thr, contours, i, cv::scalar(0,0,0), cv_filled); } }
Comments
Post a Comment