Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed to make provision for using OpenCV 4 #507

Open
wants to merge 2 commits into
base: melodic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cv_bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ if(NOT ANDROID)
else()
find_package(Boost REQUIRED)
endif()
find_package(OpenCV 3 REQUIRED

set(_opencv_version 4)
find_package(OpenCV 4 QUIET)
if(NOT OpenCV_FOUND)
message(STATUS "Did not find OpenCV 4, trying OpenCV 3")
set(_opencv_version 3)
endif()

find_package(OpenCV ${_opencv_version} REQUIRED
COMPONENTS
opencv_core
opencv_imgproc
Expand Down
20 changes: 10 additions & 10 deletions cv_bridge/src/module_opencv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ void NumpyAllocator::deallocate( int* refcount, uchar* datastart, uchar* data )
}

// Declare the object
NumpyAllocator g_numpyAllocator;
NumpyAllocator *g_numpyAllocator;

int convert_to_CvMat2(const PyObject* o, cv::Mat& m)
int convert_to_CvMat2(const PyObject* o, cv::Mat& m, int* refcount)
{
// to avoid PyArray_Check() to crash even with valid array
do_numpy_import();

if(!o || o == Py_None)
{
if( !m.data )
m.allocator = &g_numpyAllocator;
m.allocator = g_numpyAllocator;
return true;
}

Expand Down Expand Up @@ -230,33 +230,33 @@ int convert_to_CvMat2(const PyObject* o, cv::Mat& m)

if( m.data )
{
m.refcount = refcountFromPyObject(o);
refcount = refcountFromPyObject(o);
m.addref(); // protect the original numpy array from deallocation
// (since Mat destructor will decrement the reference counter)
};
m.allocator = &g_numpyAllocator;
m.allocator = g_numpyAllocator;

if( transposed )
{
cv::Mat tmp;
tmp.allocator = &g_numpyAllocator;
tmp.allocator = g_numpyAllocator;
transpose(m, tmp);
m = tmp;
}
return true;
}

PyObject* pyopencv_from(const Mat& m)
PyObject* pyopencv_from(const Mat& m, const int* refcount)
{
if( !m.data )
Py_RETURN_NONE;
Mat temp, *p = (Mat*)&m;
if(!p->refcount || p->allocator != &g_numpyAllocator)
if(!refcount || p->allocator != g_numpyAllocator)
{
temp.allocator = &g_numpyAllocator;
temp.allocator = g_numpyAllocator;
ERRWRAP2(m.copyTo(temp));
p = &temp;
}
p->addref();
return pyObjectFromRefcount(p->refcount);
return pyObjectFromRefcount(refcount);
}