opencv-ndarray-conversion icon indicating copy to clipboard operation
opencv-ndarray-conversion copied to clipboard

error: cannot declare variable ‘g_numpyAllocator’ to be of abstract type ‘NumpyAllocator’

Open hiostreas opened this issue 10 years ago • 5 comments

I fixed the compiler now I am able to use the command -std=c++11, but I've got that error

hiostreas avatar May 08 '15 13:05 hiostreas

Hello hiostreas,

I would like to add something about this error. I think I found the reason : between opencv 2.4.X and opencv 3, the virtual class 'MatAllocator' is not described in the same way, for instance the virtual methods allocate and deallocate are not defined in the same way in both versions of opencv. So if you use opencv 2.4.x, you will probably compile successfully this utility, but I'm quite sure you won't with opencv 3. It took me some hours to find out, I just wanted to share that with future users.

Please tell me if I'm missing something or if I'm totally wrong.

PS : Sorry for my English, my apologies :)

Regards,

Adem

ademoverflow avatar Apr 01 '16 14:04 ademoverflow

@ademstoce Do you think if there's any way to solve this problem? I've been trying to compile this with python3 and opencv3

xgenvn avatar Aug 31 '16 00:08 xgenvn

@xgenvn You can use the code from opencv3 repo: https://github.com/opencv/opencv/blob/master/modules/python/src2/cv2.cpp I think it is going to work if you take the NumpyAllocator and the pyopencv_from method from there.

detorto avatar Sep 08 '16 17:09 detorto

For opencv3 apparently a lot has changed! Even I was facing the problem. You need to use the latest wrapper functions files from opencv. Opencv2 wrapper function files

Check out this folder(Opencv3 wrapper function files)

This folder has the wrapper functions that you need to use. This is actually for generating wrapper functions for all modules. But we are concerned only with the cv2.cpp (which has the needed wrapper function definitions) and pycompat.hpp in this folder.

You need to add both these files to your directory. Now from cv2.cpp I needed only the functions that would convert Mat to PyObject and vice-versa. So I deleted the code from line 443 (template<typename _Tp, int m, int n> PyObject* pyopencv_from(const Matx<_Tp, m, n>& matx)) till end

Also for making this file work I had to do the following steps(This may or may not be needed depending on your usage):

  • Instead of
#include "pyopencv_generated_include.h"
#include "opencv2/core/types_c.h"
#include "opencv2/opencv_modules.hpp"

on lines 12-15, I used

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/core/types_c.h"
#include "opencv2/opencv_modules.hpp"
  • Removed #define MODULESTR "cv2" on line 8 because I had changed the file name cv2.cpp to something else

  • Removed lines 87-116 because didn't look much useful for the functions that I was using

  • Removed lines template<> from lines 393, 426

  • Added line import_array as the first line in body of bool pyopencv_to(PyObject* o, Matx<_Tp, m, n>& mx, const char* name) and PyObject* pyopencv_from(const Mat& m). This is because if I wasn't adding this, it was resulting in a segmentation fault. So somewhere I found a solution that import_array() is necessary for initializing this.

kartik2112 avatar Dec 01 '17 06:12 kartik2112

I do not know the internal logic but apparently instead of having refcount directly in Mat and using it like p->refcount, in opencv3, they have added some UMatData class which will give you this refcount. Hence, this line becomes p->u

And there are many other minute changes as well in the wrapper function definitions as well

kartik2112 avatar Dec 01 '17 06:12 kartik2112