Skip to content

Commit

Permalink
add opencv4 into the dependency list for ROS2 (#324)
Browse files Browse the repository at this point in the history
* add opencv4 into the dependency list.

Signed-off-by: Lewis Liu <[email protected]>

* update CMakefile for automatically selecting openCV4 or openCV3 judging from
openCV installation situation.
  • Loading branch information
LewisLiuPub authored Apr 30, 2020
1 parent c2de79c commit eae9586
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
12 changes: 11 additions & 1 deletion cv_bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,23 @@ else()
endif()

find_package(sensor_msgs REQUIRED)
find_package(OpenCV 3 REQUIRED

find_package(OpenCV 4 QUIET
COMPONENTS
opencv_core
opencv_imgproc
opencv_imgcodecs
CONFIG
)
if(NOT OpenCV_FOUND)
find_package(OpenCV 3 REQUIRED
COMPONENTS
opencv_core
opencv_imgproc
opencv_imgcodecs
CONFIG
)
endif()

if(NOT ANDROID)
add_subdirectory(python)
Expand Down
10 changes: 7 additions & 3 deletions cv_bridge/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ if(PYTHON_VERSION_MAJOR VERSION_EQUAL 3)
add_definitions(-DPYTHON3)
endif()

if(OpenCV_VERSION_MAJOR VERSION_EQUAL 3)
add_library(${PROJECT_NAME}_boost module.cpp module_opencv3.cpp)
else()
if(OpenCV_VERSION_MAJOR VERSION_EQUAL 4)
add_definitions(-DOPENCV_VERSION_4)
endif()

if(OpenCV_VERSION_MAJOR VERSION_LESS 3)
add_library(${PROJECT_NAME}_boost module.cpp module_opencv2.cpp)
else()
add_library(${PROJECT_NAME}_boost module.cpp module_opencv3.cpp)
endif()
target_link_libraries(${PROJECT_NAME}_boost
${PYTHON_LIBRARIES}
Expand Down
9 changes: 7 additions & 2 deletions cv_bridge/src/module_opencv3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ class NumpyAllocator : public MatAllocator
NumpyAllocator() {stdAllocator = Mat::getStdAllocator();}
~NumpyAllocator() {}

// To compile openCV3 with OpenCV4 APIs.
#ifndef OPENCV_VERSION_4
#define AccessFlag int
#endif

UMatData * allocate(PyObject * o, int dims, const int * sizes, int type, size_t * step) const
{
UMatData * u = new UMatData(this);
Expand All @@ -115,7 +120,7 @@ class NumpyAllocator : public MatAllocator
}

UMatData * allocate(
int dims0, const int * sizes, int type, void * data, size_t * step, int flags,
int dims0, const int * sizes, int type, void * data, size_t * step, AccessFlag flags,
UMatUsageFlags usageFlags) const
{
if (data != 0) {
Expand Down Expand Up @@ -148,7 +153,7 @@ class NumpyAllocator : public MatAllocator
return allocate(o, dims0, sizes, type, step);
}

bool allocate(UMatData * u, int accessFlags, UMatUsageFlags usageFlags) const
bool allocate(UMatData * u, AccessFlag accessFlags, UMatUsageFlags usageFlags) const
{
return stdAllocator->allocate(u, accessFlags, usageFlags);
}
Expand Down

0 comments on commit eae9586

Please sign in to comment.