numpy-opencv-converter icon indicating copy to clipboard operation
numpy-opencv-converter copied to clipboard

OpenCV 3 support

Open hcl14 opened this issue 9 years ago • 9 comments

Compilation yields errors which I believe are due to incompatibility with OpenCV 3 (e.g. ‘class cv::Mat’ has no member named ‘refcount):

/home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.cpp:188:16: error: cannot declare variable ‘g_numpyAllocator’ to be of abstract type ‘NumpyAllocator’
 NumpyAllocator g_numpyAllocator;
                ^
/home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.cpp:135:7: note:   because the following virtual functions are pure within ‘NumpyAllocator’:
 class NumpyAllocator : public MatAllocator
       ^
In file included from /usr/local/include/opencv2/core.hpp:59:0,
                 from /usr/local/include/opencv2/opencv.hpp:46,
                 from /home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.h:8,
                 from /home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.cpp:4:
/usr/local/include/opencv2/core/mat.hpp:417:23: note:   virtual cv::UMatData* cv::MatAllocator::allocate(int, const int*, int, void*, size_t*, int, cv::UMatUsageFlags) const
     virtual UMatData* allocate(int dims, const int* sizes, int type,
                       ^
/usr/local/include/opencv2/core/mat.hpp:419:18: note:   virtual bool cv::MatAllocator::allocate(cv::UMatData*, int, cv::UMatUsageFlags) const
     virtual bool allocate(UMatData* data, int accessflags, UMatUsageFlags usageFlags) const = 0;
                  ^
/usr/local/include/opencv2/core/mat.hpp:420:18: note:   virtual void cv::MatAllocator::deallocate(cv::UMatData*) const
     virtual void deallocate(UMatData* data) const = 0;
                  ^
/home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.cpp: In member function ‘cv::Mat NDArrayConverter::toMat(const PyObject*)’:
/home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.cpp:285:11: error: ‘class cv::Mat’ has no member named ‘refcount’
         m.refcount = refcountFromPyObject(o);
           ^
/home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.cpp: In member function ‘PyObject* NDArrayConverter::toNDArray(const cv::Mat&)’:
/home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.cpp:323:12: error: ‘class cv::Mat’ has no member named ‘refcount’
     if(!p->refcount || p->allocator != &g_numpyAllocator)
            ^
/home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.cpp:330:36: error: ‘class cv::Mat’ has no member named ‘refcount’
     return pyObjectFromRefcount(p->refcount);
                                    ^
CMakeFiles/np_opencv_converter.dir/build.make:86: recipe for target 'CMakeFiles/np_opencv_converter.dir/utils/conversion.cpp.o' failed
make[2]: *** [CMakeFiles/np_opencv_converter.dir/utils/conversion.cpp.o] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/np_opencv_converter.dir/all' failed
make[1]: *** [CMakeFiles/np_opencv_converter.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

All my other code is in OpenCV 3, so I wanted this converter to support it...

Suggested edit from the "pull requests" also doesn't work:

/home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.cpp: In member function ‘cv::Mat NDArrayConverter::toMat(const PyObject*)’:
/home/hcl/Documents/study/opencv/deepcut-cnn/python/pose/cpp/numpy-opencv-converter-master/utils/conversion.cpp:218:33: error: cannot convert ‘const PyObject* {aka const _object*}’ to ‘const PyArrayObject* {aka const tagPyArrayObject*}’ for argument ‘1’ to ‘int PyArray_TYPE(const PyArrayObject*)’
     int typenum = PyArray_TYPE(o);

...

etc..

hcl14 avatar Nov 03 '16 14:11 hcl14

I have the same issue. Any news on this?

andrewssobral avatar Mar 18 '17 23:03 andrewssobral

@andrewssobral Take a look at https://github.com/spillai/numpy-opencv-converter/pull/4 or https://github.com/spillai/numpy-opencv-converter/blob/master/utils/conversion.cpp#L66. I haven't moved to OpenCV 3 myself, so I haven't had time to identify the memory issues @Txordi00 mentions.

spillai avatar Mar 19 '17 14:03 spillai

Thank you @spillai I'm checking it... Best

andrewssobral avatar Mar 27 '17 03:03 andrewssobral

Just encounter this error, I went through all the code on the internet, all of them result in this exact same error.

Is there no hope?

AlvarHHM avatar Jun 19 '17 10:06 AlvarHHM

Same issue.. I'm also using OpenCV3...

hyperchris avatar Jul 11 '17 23:07 hyperchris

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 05: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

https://github.com/GarrickLin/numpy-opencv-converter I have adapt this repo to opencv3

GarrickLin avatar Dec 22 '17 09:12 GarrickLin

Have you solved your problem? @hcl14 I confronted the same problem as yours. Following kartik2112's steps, it doesn't work.

izengpan avatar Jul 05 '18 13:07 izengpan