Ask Your Question
2

How to use opencv-contrib-python fourierDescriptor()

asked 2018-05-04 01:27:00 -0600

Rein gravatar image

The question: For a python3 script I'd like some sequence of fourier descriptors from input values (please), hopefully from a rank 2 numpy array. It's unclear how to use this function, I can't find documentation on its intended use.

The 'however' fourierDescriptor() throws {error}/io/opencv_contrib/modules/ximgproc/src/fourier_descriptors.cpp:214: error: (-215) _src.empty() || (_src.channels() == 2 && (_src.depth() == 4 || _src.depth() == 5 || _src.depth() == 6)) in function fourierDescriptor

Steps to reproduce

On ubuntu 16.04: pip3 install opencv-contrib-python opencv-contrib-python in ./venv/lib/python3.5/site-packages (3.4.0.12)

from cv2.ximgproc import fourierDescriptor

Feeding some dummy values:

fourierDescriptor([12, 11])
{TypeError}src is not a numpy array, neither a scalar

Fair enough...

Feeding a scalar:

fourierDescriptor(12)
{error}/io/opencv_contrib/modules/ximgproc/src/fourier_descriptors.cpp:214: error: (-215) _src.empty() || (_src.channels() == 2 && (_src.depth() == 4 || _src.depth() == 5 || _src.depth() == 6)) in function fourierDescriptor

Feeding a rank 1 numpy array:

fourierDescriptor(np.array([12]))
{error}/io/opencv_contrib/modules/ximgproc/src/fourier_descriptors.cpp:214: error: (-215) _src.empty() || (_src.channels() == 2 && (_src.depth() == 4 || _src.depth() == 5 || _src.depth() == 6)) in function fourierDescriptor

Feeding a rank 2 numpy array:

fourierDescriptor(np.array([[12, 11], [10, 9]]))
{error}/io/opencv_contrib/modules/ximgproc/src/fourier_descriptors.cpp:214: error: (-215) _src.empty() || (_src.channels() == 2 && (_src.depth() == 4 || _src.depth() == 5 || _src.depth() == 6)) in function fourierDescriptor

Many thanks for any pointers, help greatly appreciated!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-05-04 01:59:22 -0600

berak gravatar image

updated 2018-05-04 03:00:37 -0600

ok, that's a bit tricky ;)

fourierDescriptor() expects a list of points, that would be a rank 1, 2 channel array.

if you started like this:

a = np.array([[12, 11], [10, 9]])

you'll have to reshape it:

 b = a.reshape(2,1,2)

also, it needs float or double type, not int currently (that's a bug), so, another conversion:

c = np.asarray(b, np.float32)

now you can calculate proper fourierDescriptors:

cv2.ximgproc.fourierDescriptor(c)

array([[[11., 10.]],

       [[ 1.,  1.]]], dtype=float32)
edit flag offensive delete link more

Comments

@LBerger , imho, there's a bug hidden here

dft needs float values, and contourSampling() will convert from int to float, but only if that function is actually called !

for some contours, getOptimalDFTSize() will return rows*cols, so no contourSampling() in that case, and integer points will throw an exception from dft()

berak gravatar imageberak ( 2018-05-04 02:06:26 -0600 )edit

this if (z.rows*z.cols!=nbElt || _src.depth() == CV_32S) contourSampling(_src, z,nbElt);

solves problem ?

LBerger gravatar imageLBerger ( 2018-05-04 03:14:06 -0600 )edit
1

yea, that, or an else if ( _src.depth() == CV_32S) _src.convertTo(z,CV_32F);

berak gravatar imageberak ( 2018-05-04 03:17:25 -0600 )edit

@berak I cannot find createTrackBar doc in python but there is a createTrackbar in opencv python ?

LBerger gravatar imageLBerger ( 2018-05-04 10:32:00 -0600 )edit
1

@LBerger, yes, it exists, but it's kinda hacked in through the backdoor

(there is no CV_EXPORTS_W for the resp. c++ function, so it's not documented)

(there are also tutorials)

berak gravatar imageberak ( 2018-05-04 10:53:28 -0600 )edit
1

@Rein I wrote new function in fourierdescriptor.cpp and a new example in python

LBerger gravatar imageLBerger ( 2018-05-18 08:06:44 -0600 )edit

@berak@LBerger thanks for your replies!

Rein gravatar imageRein ( 2018-05-18 11:25:25 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2018-05-04 01:27:00 -0600

Seen: 1,569 times

Last updated: May 04 '18