i need retrieve outermost contour of several silhouettes, possibly storing contour coordinates in clockwise or counterclockwise order. i've read, kind of result can archived using opencv's canny + findcontours. unfortunately, majority of silhouettes have elaborate has jagged edges or holes, therefore, "standard procedure" not working properly. instance, if image quite simple , without holes, result want (just outermost contour , ordered coordinates): cup example
in case of pictures holes, segmented outermost contour (different colours , see attached pictures) , still displays inner holes in final image. worst results jagged edges. holes displayed , contour highly segmented (cat). holes , jagged edges
code:
//add small padding. otherwise, in case of images border partially cut out won't considered "closed" contour int topbottom = (int) (0.05*image.rows); int rightleft = (int) (0.05*image.cols); copymakeborder( image, image, topbottom, topbottom, rightleft, rightleft, border_constant); //consider alpha channel create silhouette mat silhouette; vector<mat> ch; split(image, ch); canny(ch[3], silhouette, 100, 200); vector<vector<point>> contours; vector<vec4i> hierarchy; //find external contour findcontours( silhouette, contours, hierarchy, cv_retr_external, cv_chain_approx_simple, point(0, 0)); rng rng(12345); mat drawing = mat::zeros(silhouette.size(), cv_8uc3); for(int = 0; < contours.size(); i++) { scalar colour = scalar(rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255)); drawcontours(drawing, contours, i, colour, 1, 8, hierarchy, 0, point()); }
is there way avoid segmentation , remove contours of holes?
opencv has function called cv2.contourarea()
, allows count area of input contour. if have more 1 contour, use function find areas of contours , delete except largest contour (one largest area, because contours inside large contour not have larger area). after have largest outer contour left.
Comments
Post a Comment