Zernike moments error on irregular shapes?

321 Views Asked by At

When I call mahotas.features.zernike_moments on certain images containing very irregular shapes I get the following error:

fv = mahotas.features.zernike_moments(mask, cv2.minEnclosingCircle(cnt)[1], degree=8)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\site-packages\mahotas\features\zernike.py", line 59, in zernike_moments
    c0,c1 = center_of_mass(im)
ValueError: too many values to unpack (expected 2)

It works on simple shapes like below:

enter image description here

But I get the error on the below image with the following code. Any idea what I am doing wrong?

enter image description here

penStrokes = np.array(penStrokes)
peri = cv2.arcLength(penStrokes, True)
approx = cv2.approxPolyDP(penStrokes, 0.02 * peri, True) 
mask = np.zeros((frame.shape[0], frame.shape[1], 1), dtype='uint8')
cv2.drawContours(mask, [approx], -1, (255,), -1)
cnt = cv2.findContours(mask.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[1][0]
cv2.imshow('mask', mask)


# Determine shape from zernike moments
fv = mahotas.features.zernike_moments(mask, cv2.minEnclosingCircle(cnt)[1], degree=8)
print(fv)

Edit It seems if I resize the mask to 500 pixels wide the code works? Does mahotas have a minimum size the shape needs to be?

1

There are 1 best solutions below

0
On

Author of mahotas here.

mask is a 3-D image (although the last dimension is 1), while the function only works for 1-D images. Try:

fv = mahotas.features.zernike_moments(mask[:,:,0], \
                        cv2.minEnclosingCircle(cnt)[1], \
                        degree=8)