drawContours and duplicate points (solved)

I’ve looked in a lot of places to try and figure out an answer to the issue I am having but no luck. So in a brief summary I need to have 1000 contour points, I use draw contour on binary images of leaves to generate the contours, if there is < 1000 contours then I essentially generate new points as an average of the surrounding points until I reach 1000 points. I have successfully generated the new points but when I try and drawContours using the new array of points I generate, it appears sparse and far worse than the original image. I was hoping someone could help me figure out what about what I am doing could be causing issues, my preliminary hypothesis is that drawContours doesn’t display points if they are a duplicate which would lead to sparsity in my resulting drawContours image. Here are images of my images:


(above; after generating new points)

I can provide any code or explanation if anyone asks, appreciate it!

p.s. can only upload the sparse image for now since I am a new user

1 Like

here is the original image of only 736 points:
image

MRE is required. this is always so.

you are having issues with array shapes. you are attempting to draw each point as its own contour, rather than one contour consisting of many points.

1 Like

I see, I will take a look at what you said. Thank you for linking the MRE, appreciate your help greatly!

Figured out my issue, it seems like drawContours needed a tuple of numpy arrays passed in in order to properly draw the contours. My solution was to create a new tuple with my edited np array of points and pass that into drawContours.

new_contour = np.array(new_contour, dtype=np.int32)
new_contours = (new_contour,)

1 Like

you can just pass [new_contour], which is a list of one contour. (new_contour,) works too. both are considered equivalent to std::vector by OpenCV’s Python bindings.

1 Like