2

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 :

music note

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 homer

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) plot 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.

  • You need to compute the convex hull. Have a look at this: https://stackoverflow.com/questions/50392449/extract-characters-using-convex-hull-coordinates-opencv-python ...and this: https://docs.opencv.org/3.4/d7/d1d/tutorial_hull.html – ma3oun Jul 02 '19 at 09:40
  • @ma3oun but that would just form a hull around the outer shape of the image, the eyes and all(from the above image) won't be part of the curve right? – Kousik Rajesh Jul 02 '19 at 13:52

0 Answers0