opencv3 linking from ROS kinetic - opencv

all!
I am trying to use OpenCV library in CLion project, but this is unsuccessful. I have opencv 3.2.0 installed with ROS kinetic at once (I can see it in /opt/ros/kinetic/include/opencv-3.2.0 directory and I can import cv2 by python). But when I use such CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(visual_slam)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
set(OpenCV_DIR /opt/ros/kinetic/include/opencv-3.2.0)
include_directories( ${OpenCV_DIR})
add_executable(visual_slam ${SOURCE_FILES})
with the next simple code:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
return 0;
}
I get the next error:
CMakeFiles/visual_slam.dir/main.cpp.o: In function cv::String::~String()':
/opt/ros/kinetic/include/opencv-3.2.0/opencv2/core/cvstd.hpp:664: undefined reference tocv::String::deallocate()'
CMakeFiles/visual_slam.dir/main.cpp.o: In function cv::String::operator=(cv::String const&)':
/opt/ros/kinetic/include/opencv-3.2.0/opencv2/core/cvstd.hpp:672: undefined reference tocv::String::deallocate()'
Such error was discussed also in OpenCV linking problems with ROS, but is it really so necessary to uninstall completely and then install again OpenCV? Is there any more quick solution?
Second question, how to correctly add OpenCV from ROS to CMakeLists.txt? Current CMakeLists (look above) does not look like to be flexible. I've already tried to add
find_package(OpenCV 3 REQUIRED)
target_link_libraries(visual_slam ${OpenCV_LIBRARIES} )
but the error is when CMake builds
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided
by "OpenCV" (requested version 3) with any of the following names:
OpenCVConfig.cmake
opencv-config.cmake

The second error you are getting (namely the missing OpenCV.cmake) is actually not only related to the first one (the undefined reference to) but is the cause.
If cmake fails to find the OpenCV module the ${OpenCV_LIBS} won't do a thing meaning that target_link_libraries(visual_slam ${OpenCV_LIBRARIES} ) doesn't link against the libraries your code requires. That said I also believe that it's OpenCV_LIBS instead of OpenCV_LIBRARIES.
You can of course specify the path where the find_package(...) command looks at to find cmake modules (I think it was the CMAKE_FIND_ROOT_PATH variable) or even manually specify the root folder of your OpenCV installation and manually handle all the things that the OpenCV.cmake handles for you.
Last but not least I'm not sure if ROS Kinetic uses the latest OpenCV (what you are using namely the v3.2). You should check that and if different versions are used (very likely), you will have to build all ROS OpenCV-related packages from scratch. You might ask "Why should I do that?". Well, to avoid compatibility issues and various weird errors that may or may not occur (depending on what OpenCV functionality you use) due to version X of OpenCV being used for the binary packages of ROS and version Y present on your system (with X != Y). I had to do that with PCL (Point Cloud Library) once and it took me several days to come to this conclusion since the errors that I was getting were (typical for C++) cryptic as hell. To avoid conflicts make sure that only the one version of OpenCV is present on your system (that is can be found by cmake) that you want to use with your ROS installation. That's also the reason why the ROS binary packages are shipped through the Ubuntu repos using dependencies that are resolved by the package manager (OpenCV, PCL etc.).
PS: Since I haven't used Clion I would also suggest (before doing anything I've mentioned above) to check if it's not some hidden cmake-related setting inside the IDE that screws things up.

Related

Linking ROS Noetic to opencv build from source

I am writing a ROS node which uses OpenCV and SIFT (ROS Noetic, Ubuntu 20.04).
As of OpenCV version 4, the SIFT algorithm is part of the opencv_contrib package.
Within my ROS node, I want to use the ROS package cv_bridge, to convert between the OpenCV image format and ROS image sensor messages.
My understanding is that
Code in the opencv_contrib package is only usable if such package is built together with OpenCV from source.
cv_bridge depends on the Ubuntu package version of OpenCV, libopencv-dev.
I currently have both installed (the Ubuntu package and the version compiled from source) and I am trying to have my node depend on the source-compiled one, in order to use the non-free algorithms.
My procedure in order to do this is (after compiling OpenCV with the additional modules and OPENCV_ENABLE_NONFREE=ON) adding to the CMakeLists.txt of my package the following lines:
find_package(OpenCV PATHS .../opencv-4.5.4/cmake)
include_directories(include ${catkin_INCLUDE_DIRS} .../opencv-4.5.4/include)
link_directories(.../opencv-4.5.4/lib)
add_executable(...)
target_link_libraries(nodeName ${catkin_LIBRARIES} .../opencv-4.5.4/lib)
in order to link my code to the compiled version of OpenCV. However, when I try to build it with catkin build I obtain:
In file included from [...]:
[...]/markerDetection.h:8:10: fatal error: opencv2/xfeatures2d.hpp: No such file or directory
8 | #include <opencv2/xfeatures2d.hpp>
I suspect that ROS is trying to link to the version I installed through the package manager, which in fact does not have the opencv2/xfeatures2d.hpp library.
Am I setting the wrong options in CMakeLists.txt? Is what I am trying to do even possible?

Why cv_bridge uses OpenCV 3.2 in ROS Melodic?

I have OpenCV 3.4 installed in Ubuntu 18. I also have installed ROS Melodic according to the website instructions. However, I keep on getting an error that libopencv_core.so.3.2 is required.
I already set my CMakeLists files to point to OpenCV 3.4.
However, I found out that in the file:
/ros/melodic/share/cv_bridge/cmake/cv_bridgeConfig.cmake
there is the following line hardcoded in opencv3.2:
set(libraries "cv_bridge;/usr/lib/x86_64-linux-gnu/libopencv_core.so.3.2.0;/usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.3.2.0;/usr/lib/x86_64-linux-gnu/libopencv_imgcodecs.so.3.2.0").
I tried to change it to 3.4 but I can not rebuild it.
The error I am getting is:
/opt/ros/melodic/lib/image_view/image_view: error while loading shared libraries: libopencv_core.so.3.2: cannot open shared object file: No such file or directory
Why is OpenCV 3.2 hardcoded in cv_bridge and how can I rebuild it with OpenCV 3.4?
Update:
I eventually installed OpenCV 3.2 and it worked properly.
Because opencv development speed is much faster than ROS individual module. And a lot of ROS modules went depreciated after someone left the job.
But that's by no means the end of the day( maybe end of the day for noobs). You can build it directly with any version of opencv core function(besides imshow kind of function) others should perform just fine.
The easiest way is to do is: in the console before executing catkin_make try to execute the following
export CMAKE_PREFIX_PATH=/usr/local:$CMAKE_PREFIX_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
This should give preference to your custom OpenCV installation when doing the find_package(OpenCV 3.X.0 REQUIRED). Then compile and use the function of that version.
Well if you do have to use 3.4 then I think you have to build ros version of opencv and image transport and cvbridge to the 3.4 if that's what you are targeting.
You can find the link here https://github.com/ros-gbp/opencv3-release The highest they provide seems to be 3.3

Why doesn't CLion link my project with OpenCV for Windows?

I'm attempting to use OpenCV for Windows as supplied by opencv.org in a project I'm building with JetBrains' CLion IDE. I've installed the opencv library and configured CLion (set(OpenCV_DIR) to reference the build directory under it, but CMake issues the warning:
Found OpenCV Windows Pack but it has no binaries compatible with your configuration.
You should manually point CMake variable OpenCV_DIR to your build of OpenCV library.
I've tried some of the older distributions from opencv.org with the same results. It appears CMake is locating the OpenCV libraries, but doesn't want to use them. Why, and how do I get the OpenCV libraries to work under CLion?
The short answer is, you will probably need to build OpenCV from source in order to use it with CLion. But given the number and range of partially answered and unanswered questions here* and elsewhere on using JetBrains' CLion IDE with the OpenCV library, I think an overview is needed (my notes are from CLion 2016.3 and OpenCV 3.1, YMMV):
Though not produced by JetBrains, CMake is very central to CLion's operation. Understanding CMake therefore helps greatly in diagnosing CLion build problems. In particular CMake maintains a disk "cache" of settings which you may need to clear to incorporate changes to your environment (Tools->CMake->Reset Cache and Reload Project).
To make use of OpenCV in your build you must specify it in your project's CMakeLists.txt file. You request that CMake locate your OpenCV location and link it to your TARGET. An example of a sequence of commands from CMakeLists.txt for an executable named mushroom follows:
add_executable(mushroom ${SOURCE_FILES})
FIND_PACKAGE(OpenCV REQUIRED)
TARGET_LINK_LIBRARIES(mushroom ${OpenCV_LIBS})
(For more on FIND_PACKAGE, see CMake:How To Find Libraries.)
FIND_PACKAGE for package XXX works either by way of FindXXX.cmake files located at CMake's Modules directory, or by consulting environment variable XXXX_DIR. On my system, no FindOpenCV.cmake file was present, so I relied on the OpenCV_DIR environment variable instead. This must be set, not to the root of your OpenCV installation, but to the build folder beneath it. I used an entry in CMakeLists.txt to set this variable, e.g.:
set(OpenCV_DIR C:/Users/myacct/AppData/Local/opencv-3.0.0/build)
To link with OpenCV, CMake uses either FindOpenCV.cmake or OpenCV_DIR (see previous point above) to locate a file named OpenCVConfig.cmake. This file is generated by and ships with a particular build of OpenCV in order to document what components are present and where they are located.
Problems may occur when variable names used by OpenCVConfig.cmake conflict with those CLion has stored in its environment. In particular, if your OpenCV was built by Microsoft Visual C (MSVC), as is the Windows distribution from opencv.org, it won't work with CLion.
Because CLion's build toolchain (ControlAltS-toolchain) uses either MinGW or Cygwin, OpenCVConfig.cmake will search for OpenCV binaries under a subdirectory named mingw or cygwin and will find none because the binaries were built with MSVC (it will look in a directory like vc11 or vc12 instead). This probably means you will need to build OpenCV from source in order to use it with CLion.
Would reconfiguring OpenCVConfig.cmake to point to the MSVC binaries make this work? you may ask. Unfortunately the answer is still no, because libraries built with one compiler typically cannot be linked with another one.
OpenCVConfig.cmake or FindOpenCV.cmake likely contain diagnostic messages, but when CLion executes CMake for you, message(STATUS) calls are not displayed. To make them display, change them to message(WARNING) or message(FATAL_ERROR). But CLion 2016.3 EAP relieves this problem; see https://stackoverflow.com/a/39398373/5025060.
CLion does not indicate which .cmake script issued which diagnostics; don't assume they all come from the same script.
Hopefully this provides some general guidance on resolving CLion / CMake / OpenCV compatibility problems. Note that this does not cover compiler or linker issues; these will not appear until CMake completes its initial makefile build. Compiler or linker issues occur at a later stage and are controlled by include*(), link*() and other commands in CMakeLists.txt.
*Some related SO questions:
OpenCV Windows setup with CLion
OpenCV CLion (Cmake) linking issue - cmake reports a strange error
use OpenCV with Clion IDE on Windows
Compiling OpenCV on Windows with MinGW
Could not find module FindOpenCV.cmake ( Error in configuration process)
CMake: Of what use is find_package() if you need to specify CMAKE_MODULE_PATH anyway?

ros can't find OpenCV , the path /usr/local/

I am working with ros and OpenCV. I installed OpenCV in the default path /usr/local, but when I build programs with catkin_make in my workstation, it shows Project cv_bridge specifies /usr/include/opencv as an include dir, which is not found.
Can you show me what can I do to solve the problem? Thank you.
I recommend you use the opencv2_catkin package that automatically finds and links your package to OpenCV. As per the Readme, just add the following dependency to your own package:
<build_depend>opencv2_catkin</build_depend>
You would also need to download catkin_simple to use opencv2_catkin.
I found you said you use catkin_make, so in new version of ROS, opencv and pcl are no longer integrate in ROS, so you need find you opencv yourself. For example, in your workspace, the CMakeLists.txt file, you should add something like find_package(OpenCV REQUIRED), and add include dir and libs to your project. If you also have problems, please paste your details of errors.

How to cross-compile opencv

I'm trying (with no success) to cross-compile OpenCV on a embedded board.
I followed this: http://docs.opencv.org/doc/tutorials/introduction/crosscompilation/arm_crosscompile_with_cmake.html.
To summarize I installed the cross compilation tools for ARM, the I ran the cmake (providing the proper toolchain file), the make and the make install commands.
I next copied the lib/ include and bin/ directories with the opencv installation in the embedded board.
However, when I try to compile a simple hello world with opencv I get undefined reference to __gnu_thumb1_case_uqi and other similar symbols.
Does anybody faced this problem and know how to solve it?
Alternative approach
Just take some embedded distro like Buildroot, OpenEmbedded or Debian. They all provide OpenCV.
As I'm mostly experienced in Buildroot, I'd like to point to the newly introduced feature to keep your project separate from BR: http://nightly.buildroot.org/manual.html#outside-br-custom. This will give you a main idea on how to compile your software against BR's OpenCV.

Resources