Ask Your Question

Unclear what is returned in cv2.findContours in Python 3.6

asked 2018-11-06 16:21:16 -0500

rmendels gravatar image

Let edge_image be the edges calculated from a call to say cv2.Canny(). According to the docs, I can calculate contours in Python using say (this is for version 3):

im2, contours, hierarchy = cv2.findContours(edge_image , cv2.RETR_TREE,  cv2.CHAIN_APPROX_SIMPLE)

I understand what is returned in contours and hierarchy, but the docs only say that im2 is the "modified image", but they do not say how modified. Does im2 contain the edges modified by the contours. If not, what does it contain. And if not, if I want to create an edge-matrix from then contours, then I would have to create an matrix of the correct size, read the positions of each, and modify the matrix to have an edge at that location. I do not just want to draw the contours, I want to be able to store an edge-matrix with the edges now defined by the contours. If that is what is contained in im2 than I can just extract the values from that, but it is not at all clear, and web searches have not turned up anything useful.

Thanks for any help.

edit retag flag offensive close merge delete



your python version does not matter, the opencv version does, here. so which opencv version is it, exactly ?

berak gravatar imageberak ( 2018-11-06 21:10:23 -0500 )edit

opencv 3.4.3, installed using conda.

rmendels gravatar imagermendels ( 2018-11-06 21:32:01 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-11-07 00:34:52 -0500

berak gravatar image

updated 2018-11-07 09:34:34 -0500

this might be a bit confusing among different opencv versions, but as of

  • versions before 3.0 would write a freeman chain into the image, and return that
  • between 3.0 and 3.4.3, the image would be left unchanged (the original input)
  • current master version does no more return the image at all (only contours, hierarchy)
edit flag offensive delete link more



Yes it looked like it was the original image, but I couldn't be certain, So if I want to look at all the contours together I need to parse out what is returned in contours, which is easy enough. The docs state that each contour contains the "position", and I am assuming this means the option in the array, 0-indexed. Is this correct (contours returns a nested list of 2-d vectors).

rmendels gravatar imagermendels ( 2018-11-07 09:28:57 -0500 )edit

sorry for bad typing - that should be it returns the 0-indexed location in the array.

rmendels gravatar imagermendels ( 2018-11-07 09:30:04 -0500 )edit

Question Tools

1 follower


Asked: 2018-11-06 16:21:16 -0500

Seen: 976 times

Last updated: Nov 07 '18