OpenCV - find or get shape outline not surrounded by bg, only separated by outline

I am trying to find the outline of a single shape on a very simple background using OpenCV findContour

(I would like to use C ++ syntax). However, he continues to outline the outline, not the shape itself. I think about it because of the white edge I got from Canny which doesn't cover the form.

Case A: Shape around the edge of an image (This is not an actual input image, but a simple input image to illustrate this problem.)

Case B: Background surrounds the form

There are main functions that I have used:

findContours( grayImage, contours, hierarchy, RETR_LIST,CHAIN_APPROX_SIMPLE);
approxPolyDP(Mat(contours.at(largestContourIndex)),poly,3,true);
drawContours(output, contours, largestContourIndex, RGB(250,0,100), -1, 8, hierarchy, 0, Point() );

      

EDIT: Scrolling detection gives the outline I want, but I need to have the best outline I can get.

Thanks in advance.

+3


source to share


3 answers


I still haven't found the reason why I can't get the outline of the shape, but I found a workaround. After erosion and dilatation, I basically have to draw a border or rectangle on the outer pixels of the input image so that the background surrounds the shape, ...

rectangle(input,Point(0,0),Point(input.cols-1,input.rows-1),Scalar(0,0,0),1,8,0);

      



... hence allowing Canny to draw the closed shape path and give me the shape path I want. I'm still trying to successfully invert Canny's output as @dvhamme suggested, but it still gives me errors. It would be better if someone can point out how to properly access or access the outline of a shape, but thanks everyone for the help.

0


source


Have you tried playing with morphological operations ?

If your main problem is that the outline you are getting is on the outside of the object and not inside, and especially if your object is made of such clear and mostly correct shapes, morphology can help.



I know that OpenCV has expansion and erosion implementations as well as open and close operations. A very simple approach that might work in your situation is to undermine the form a bit (1-2-3 iterations perhaps) and then do what you are doing already. Hopefully, you get the outer contours of the eroded shape, which should actually be the inner paths of the original shape.

I think OpenCV actually implements an even more complex morphology, but as always, try the simple stuff first: D

+1


source


It seems to me that the outline you are looking for is probably found, but you are not using it. Instead, you use the largest outline. Try to build all the found contours one by one and see if there are any.

If not, try invert the image and repeat the process.

+1


source







All Articles