ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Please can someone help me to link ros node with opencv correctly.

asked 2023-05-06 13:45:57 -0500

elpidiovaldez gravatar image

I am at my wits end trying to get a simple ROS node to link with opencv correctly. The problem is that there are two installations of opencv4 on my system.

I have a simple C++ program to read video from an esp32 chip over wifi. The program works (displays video from the esp32 cam) when I build it using this makefile:

all:
g++ -o2 -o esp32cam_to_ros_node esp32cam_to_ros_node.cpp ESP32-CAM\ Library.cpp `pkg-config opencv4 --cflags --libs` -lpthread -lcurl -std=c++11 -m64 -D _MAC_

Then I build the exact same program using CMakeLists.txt, and it no longer works (Note that the program makes no use of any ROS functionality at the moment - I stripped all of that out whilst trying to get to the root of the problem). I am simply building an executable using catkin_make under the ROS framework.

I looked at catkin_ws/build/esp32_cam_to_ros_node/link.txt and see that the opencv_core library is:

/usr/lib/x86_64-linux-gnu/libopencv_core.so.4.2.0

whereas using ldd on the working executable, from my own makefile shows:

/usr/local/lib/libopencv_core.so.4.2

So it appears the solution is to force catkin_make to use the version of opencv from /usr/local/lib

I tried to do this by adding a line to CMakeLists.txt:

set(OpenCV_DIR /usr/local/lib/cmake/opencv4/)

But now catkin_make fails saying:

CMake Warning at esp32cam_to_ros/CMakeLists.txt:141 (add_executable):
Cannot generate a safe runtime search path for target esp32cam_to_ros_node
because files in some directories may conflict with libraries in implicit
directories:
runtime library [libopencv_calib3d.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
  /usr/local/lib
runtime library [libopencv_dnn.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
  /usr/local/lib

So now I am completely stumped ! I don't understand what cmake deems to be implicit directories. I don't know whether I should be trying to make the version of opencv in /usr/lib/x86_64-linux-gnu work, or whether there is a straightforward way of getting catkin_make to use /usr/local/lib

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-07 07:28:41 -0500

Mike Scheutzow gravatar image

updated 2023-05-07 08:26:01 -0500

set(OpenCV_DIR /usr/local/lib/cmake/opencv4/)

Don't do this in your CMakeLists.txt. Don't override the standard opencv library paths.

If there are multiple copies of a shared library file, the decision about which one to use is made at app startup time, not at compile time.

The typical cause of this error is that the directory /usr/local/lib is present in your environment variable LD_LIBRARY_PATH. If you are running a ros app, you should not have this directory in the list. [Edit: ubuntu has /usr/local/lib in the default library search path, so fixing the environment variable is not sufficient. I think the best solution is to remove this alternate opencv install from /usr/local/lib/.]

If you want to use the ros apt binary repository, you are expected to use the standard opencv libraries installed by apt (which are put in /usr/lib/...).

edit flag offensive delete link more

Comments

I have done what you suggested - removed all of opencv from /usr/local. Now both the working and non-working programs appear to be using /lib/x86_64-linux-gnu. BUT it has not made any difference. I am completely at a loss as to the difference between the programs. I have updated question with the latest findings.

elpidiovaldez gravatar image elpidiovaldez  ( 2023-05-07 08:56:48 -0500 )edit

I don't see any edit in your description.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2023-05-07 11:40:26 -0500 )edit

I was doing the edit. Possibly I never committed it. It is not worth doing it again because I found the problem - and it was nothing to do with the second copy of opencv. I'm kicking myself for wasting so much time on that theory, when it turned out that I had missed seeing a flag in the makefile of the working copy. -D _MAC_ was all it needed. Thanks so much for trying to help.

elpidiovaldez gravatar image elpidiovaldez  ( 2023-05-07 19:43:43 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2023-05-06 13:45:57 -0500

Seen: 136 times

Last updated: May 07 '23