I am trying to do the Fourier series animation in python like this
https://www.youtube.com/watch?v=QVuU2YCwHjw&app=desktop
So to do that from an image I need a list of points that forms a closed curve. I can do that for simple images like this :
by thresholding the image and finding contour, the contour object has points ordered in the path of a curve by default.
However, how do I do this for complex images like this
I can use canny to find the outlines(which are already in black) but how do I convert this to the form of an approximate closed curve.
This is how I did for the simple case
ret,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cts=contours[0].reshape(-1,2)
plt.plot(cts[:,0],cts[:,1])
plt.show()
This produces a nice plot like this(The inversion is not a problem)
but this fails in complex images since there are multiple contours
So basically TLDR: How do I join the edges found by Canny to form a single closed curve(or any other method to do so)?
NOTE: I require the points to be ordered as in a contour so that when I do
plt.plot(x,y)
I get a nice outline of the image and not lines crisscrossing everywhere
Any help is appreciated.