pyefd icon indicating copy to clipboard operation
pyefd copied to clipboard

Contour chain approximation "simple" is buggy or numerically instable

Open MikeTkachuk opened this issue 1 year ago • 0 comments

Description

I was running Fourier descriptors extraction on contours that naturally contain long straight lines. I used cv.CHAIN_APPROX_SIMPLE as usual but was having weird results as if the method does not converge:

image

I tried storing the contour as cv.CHAIN_APPROX_NONE instead and it fixed the problem for all of my cases: image

Minimal setup to reproduce:

img = np.zeros((100,100), dtype=np.uint8)
img = cv.rectangle(img, (25,25), (75,75), (255,255,255), -1)
cnt, h = cv.findContours(img,cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
coeffs = pyefd.elliptic_fourier_descriptors(cnt[0].reshape(-1,2), order=10, normalize=True)
pyefd.plot_efd(coeffs)
plt.show()

img = np.zeros((100,100), dtype=np.uint8)
img = cv.rectangle(img, (25,25), (75,75), (255,0,0), -1)
cnt, h = cv.findContours(img,cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
coeffs = pyefd.elliptic_fourier_descriptors(cnt[0].reshape(-1,2), order=10, normalize=True)
pyefd.plot_efd(coeffs)
plt.show()

I get: image image

MikeTkachuk avatar Aug 07 '22 14:08 MikeTkachuk