Use Cmake with PCL and OpenCV - opencv

I am new to Computer Vision. On Cmake, i am trying to use PCL and OpenCV with a 2D Lidar sensor.
I saw this tutorial: [http://unanancyowen.com/en/pcl18/#Download1
And to configure PCL on CmakeLists.txt the following code is used:
cmake_minimum_required( VERSION 2.8 )
# Create Project
project( solution )
add_executable( project main.cpp )
set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "project" )
# Find Packages
find_package( PCL 1.8 REQUIRED )
if( PCL_FOUND )
# Additional Include Directories
# [C/C++]>[General]>[Additional Include Directories]
include_directories( ${PCL_INCLUDE_DIRS} )
# Preprocessor Definitions
# [C/C++]>[Preprocessor]>[Preprocessor Definitions]
add_definitions( ${PCL_DEFINITIONS} )
#add_definitions( -DPCL_NO_PRECOMPILE )
# Additional Library Directories
# [Linker]>[General]>[Additional Library Directories]
link_directories( ${PCL_LIBRARY_DIRS} )
# Additional Dependencies
# [Linker]>[Input]>[Additional Dependencies]
target_link_libraries( project ${PCL_LIBRARIES} )
endif()
And to configure CmakeLists.txt for OpenCV, the following code:
cmake_minimum_required( VERSION 3.6 )
# Create Project
project( solution )
add_executable( project main.cpp )
set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "project" )
# Find OpenCV
set( OpenCV_DIR "C:/Program Files/opencv/build" )
find_package( OpenCV REQUIRED )
# Project Settings for OpenCV
if( OpenCV_FOUND )
# Additional Include Directories
# [C/C++]>[General]>[Additional Include Directories]
include_directories( ${OpenCV_INCLUDE_DIRS} )
# Additional Library Directories
# [Linker]>[General]>[Additional Library Directories]
link_directories( ${OpenCV_LIB_DIR} )
# Additional Dependencies
# [Linker]>[Input]>[Additional Dependencies]
target_link_libraries( project ${OpenCV_LIBS} )
endif()
How do i make a CmakeLists.txt to use with both? PCL and OpenCV.

Found the answer asking on the website that i got those files:
http://unanancyowen.com/en/pcl18/#comment-1221
This is the code to pull OpenCV and PCL:
cmake_minimum_required( VERSION 2.8 )
# Create Project
project( solution )
add_executable( project main.cpp )
# Set StartUp Project (Option)
# (This setting is able to enable by using CMake 3.6.0 RC1 or later.)
set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "project" )
# Find Packages
# Find PCL
find_package( PCL 1.8 REQUIRED )
# Find OpenCV
set( OpenCV_DIR "C:/Program Files/opencv/build" )
find_package( OpenCV REQUIRED )
if( PCL_FOUND AND OpenCV_FOUND )
# [C/C++]>[General]>[Additional Include Directories]
include_directories( ${PCL_INCLUDE_DIRS} )
include_directories( ${OpenCV_INCLUDE_DIRS} )
# [C/C++]>[Preprocessor]>[Preprocessor Definitions]
add_definitions( ${PCL_DEFINITIONS} )
# For Use Not PreCompiled Features
#add_definitions( -DPCL_NO_PRECOMPILE )
# [Linker]>[General]>[Additional Library Directories]
link_directories( ${PCL_LIBRARY_DIRS} )
link_directories( ${OpenCV_LIB_DIR} )
# [Linker]>[Input]>[Additional Dependencies]
target_link_libraries( project ${PCL_LIBRARIES} )
target_link_libraries( project ${OpenCV_LIBS} )
endif()
And on this link there is an old explanation and here my question on the OpenCV here.

To use PCL and OpenCV simultaneously in the same project,You can write CMakeLists.txt file in minimum line as below:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(project_name)
find_package(PCL 1.4 REQUIRED)
find_package(OpenCV REQUIRED)
include_directories(${PCL_INCLUDE_DIRS} )
link_directories(${PCL_LIBRARY_DIRS} )
add_definitions(${PCL_DEFINITIONS} )
add_executable (project_executable main.cpp)
target_link_libraries (project_executable ${PCL_LIBRARIES} ${OpenCV_LIBS})

Related

Conflict between OpenCV 4 and cv_bridge in ROS Node

I'm starting a project with OpenCV with ROS and i need to use xfeatures2d that is in opencv_contrib. I follow this instructions and modify my CMake File in this way:
cmake_minimum_required(VERSION 3.0.2)
project(visual_odometry)
find_package(catkin REQUIRED COMPONENTS
cv_bridge
image_transport
roscpp
rospy
sensor_msgs
nav_msgs
geometry_msgs
std_msgs
)
find_package(OpenCV 4 REQUIRED)
catkin_package()
include_directories(
# include
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
add_executable(show_image src/show_image.cpp)
target_link_libraries(show_image
${catkin_LIBRARIES}
${OpenCV_LIBRARIES})
add_executable(show_video src/show_video.cpp)
target_link_libraries(show_video
${catkin_LIBRARIES}
${OpenCV_LIBRARIES})
add_executable(visual_Odometry src/visual_Odometry.cpp)
target_link_libraries(visual_Odometry
${catkin_LIBRARIES}
${OpenCV_LIBRARIES})
After catkin_make :
/usr/bin/ld: warning: libopencv_imgproc.so.4.5, needed by /usr/local/lib/libopencv_xfeatures2d.so.4.5.3, may conflict with libopencv_imgproc.so.3.2
During run time, some functions of imgproc like CLAHE algorithm doesn't work and my node stop to work.
When i build with catkin_make, cv_bridge find opencv3.2 default in ros, but my node find opencv4 thanks to CMake file. So, can I fix this? It is possible build in opencv3.2 a opencv_contrib? It is possibile to change the version of opencv in cv_bridge?
I found a solution in Github:
In catkin_ws/src:
git clone https://github.com/fizyr-forks/vision_opencv/tree/opencv4
cd vision_opencv
git checkout opencv4

catkin package library link to the openCV library

I have been losing my head over this for some time now. So I need to use the opencv in a ros melodic package built by the catkin on the Ubuntu 18.04. The idea is to make a package which is actually just a library and this library using all the regular ros packages (image_transport, cv_bridge, roscpp, rospy, std_msgs, genmsg, tf, actionlib_msgs, actionlib ).
Just to let you know, I have 2 opencv installed, the openCV 3.2 in the usr/share and the openCV 4.3 in the usr/local/share, that is why I have specifically set the openCV_DIR to point to the 3.2 version.
I have tried to add message to the CMakeList.txt to print out the opencv_version, opencv_include_dir and opencv_libs and the version is 3.2.0, include_dirs is /usr/include;/usr/include/opencv and libs are opencv_calib3d;opencv_core;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_viz;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_text;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
in my pkg_library CMakeList.txt I have this:
cmake_minimum_required(VERSION 2.8.3)
project(pkg_libraries)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
set( OpenCV_DIR "/usr/share/OpenCV/" )
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(
catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
genmsg
tf
actionlib_msgs
actionlib
sensor_msgs
cv_bridge
image_transport
message_generation
)
find_package(
OpenCV REQUIRED
)
catkin_package(
INCLUDE_DIRS include
LIBRARIES pkg_libraries
CATKIN_DEPENDS roscpp rospy std_msgs tf actionlib actionlib_msgs cv_bridge
)
include_directories(
include
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
add_library(${PROJECT_NAME}
src/imageSaver.cpp
)
target_link_libraries( ${PROJECT_NAME} ${catkin_LIBRARIES} ${OpenCV_LIBS} )
install(
TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)
install(
DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
)
the compilation of the pkg_libraries with the catkin_build looks just fine, but later when I try to do catkin_build in the another package which includes the pkg_libraries, it fails with the error:
libpkg_libraries.so: undefined reference to
`cv::imwrite(std::__cxx11::basic_string,
std::allocator > const&, cv::_InputArray const&,
std::vector > const&)'
Which I would say looks like the linker error, because I have used the cv::imwrite only in the pkg_libraries. and the package which reports the error includes only the pkg_libraries. So the pkg_libraries but it is obviously not linked properly, but it is built without errors.
What am I missing here?
Thanks
the problem was because of the pkg_libraries is built like a shared library in which I have had another .cpp and .h which were using different openCV versions (default opencv 3.2) and I think the problem happened because by loading the libpkg_libraries.so I have made a conflict between openCV version.
Anyway, now I have made separate libraries and it works as it suppose to work.
Thanks

How to use OpenCV 4 with ROS?

I'm trying to run ROS Melodic with OpenCV 4.2.0 on windows What is wrong with my CMakeLists? I have added OpenCV to my system path, but Catkin_make always gives this error:
Could not find a configuration file for package "OpenCV" that is compatible
with requested version "4.2.0".
The following configuration files were considered but not accepted:
C:/opt/rosdeps/x64/CMake/OpenCVConfig.cmake, version: 3.4.1
The version it finds is the OpenCV shipped with ROS melodic distribution. How do I change where CMake searches?
This is my CMake:
cmake_minimum_required(VERSION 2.8.3)
project(nav_ross)
add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
cv_bridge
image_transport
pcl_ros
sensor_msgs
visualization_msgs
dynamic_reconfigure
geometry_msgs
)
find_package(OpenCV 4.2.0 REQUIRED)
add_message_files(
FILES
nav_msg.msg
)
generate_messages(
DEPENDENCIES
std_msgs
visualization_msgs
geometry_msgs
)
generate_dynamic_reconfigure_options(
cfg/post-process-config.cfg
)
catkin_package(
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime cv_bridge image_transport pcl_ros sensor_msgs visualization_msgs geometry_msgs
)
include_directories(
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
../../cpp
cfg/cpp/nav_ross
)
add_library(iasdk_static STATIC ../../cpp/radarclient.cpp ../../cpp/tcpradarclient.cpp ../../cpp/tcpsocket.cpp ../../cpp/timer.cpp ../../cpp/threadedclass.cpp)
add_executable(Movement-Detector Movement-Detector.cpp)
target_link_libraries(Movement-Detector ${catkin_LIBRARIES} ${OpenCV_LIBS})
add_dependencies(Movement-Detector nav_ross_generate_messages_cpp)
add_executable(talker1 talker1.cpp)
target_link_libraries(talker1 ${catkin_LIBRARIES} ${OpenCV_LIBS} iasdk_static)
add_dependencies(talker1 nav_ross_generate_messages_cpp ${devel2_pkg_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_executable(Mobile_Platform Mobile_Platform.cpp)
target_link_libraries(Mobile_Platform ${catkin_LIBRARIES} ${OpenCV_LIBS} iasdk_static)
add_dependencies(Mobile_Platform nav_ross_generate_messages_cpp ${devel2_pkg_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
Try to tell CMake where is your OpenCV, maybe it looks for the first one, and if found forces to use.
find_package(OpenCV REQUIRED PATHS Path/to/OpenCV NO_DEFAULT_PATH)

C++ Link two Shared Library to main.cpp

I'm having some problems try to link two shared library to my main.cpp file in a cmake file.
My folder tree is:
**mainfolder**
|_main.cpp
|_CMakeLists.txt
|
| **lib**
|_**lib1**
|_CMakeLists.txt
|_liblib1.so
|_**src**
|_lib1.cpp
|_**include**
|_lib1.h
|_**lib2**
|_CMakeLists.txt
|_liblib2.so
|_**src**
|_lib2.cpp
|_**include**
|_lib2.h
the two CMakeLists.txt for the two libraries are quite similar and created according to this link:
cmake_minimum_required(VERSION 2.6)
project( LibraryLib1 C CXX )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
set(CMAKE_BUILD_TYPE Release)
find_package( OpenCV REQUIRED )
# Find source files
file(GLOB SOURCES src/*.cpp)
# Include header files
include_directories(include)
# Create shared library
add_library(${PROJECT_NAME} SHARED ${SOURCES})
# Install library
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
# Install library headers
file(GLOB HEADERS include/*.h)
install(FILES ${HEADERS} DESTINATION include/${PROJECT_NAME})
my main.cpp file is:
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
#include "lib/lib1/lib1.h"
#include "lib/lib2/lib2.h"
using namespace cv;
int main()
{
printf("Executing main.cpp");
lib1 lib1object;
lib2 lib2object;
for(;;)
{
lib1object.Analize(param1, param2);
lib2object.Draw(param1, param2, param3);
}
return 0;
}
My main.cpp should call openCV + the two libraries. Could you please tell me which line I have to add to the CMakeLists.txt in the main folder to run my main?
in this moment the mainfolder/CMakeLists.txt is:
cmake_minimum_required(VERSION 2.8)
project( MyProject C CXX )
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS})
I really appreciate your help. Thanks!!
One more thing... If I modify tghe last row of the mainfolder/CMakeLists.txt as:
target_link_libraries( main ${OpenCV_LIBS} LibraryLib1 LibraryLib2)
where LibraryLib1 and LibraryLib2 are the name of the two library project, I obtain:
/usr/bin/ld: unable to find -lLibraryLib1
/usr/bin/ld: unable to find -lLibraryLib2
You have separate projects for the libraries and the main file. Therefore, the main file doesn't know the target names from the other projects. The easiest solution (as everything is in a single source tree) is to have just a single projects.
Therefore, use add_subdirectory to build your libarires directly in the main project:
cmake_minimum_required(VERSION 2.8)
project( MyProject C CXX )
add_subidrectory(lib/lib1)
add_subidrectory(lib/lib2)
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} LibraryLib1 LibraryLib2)
This ensures that target names are known. This assumes that you have calls in the subfolders like add_library(LibraryLib1.... Otherwise, exchange the names appropriately.

Configuring an c++ OpenCV project with Cmake

I consider this a fundamental step for creating projects that use OpenCV libraries so you don't need to manually include all the libraries. There is not detailed information on this topic, at least for a newbie that just wants to use OpenCV as soon as posible, so:
Which is the easiest and scalable way to create a multiplatform c++ OpenCV with Cmake?
First: create a folder Project containing two subfolders src and include, and a file called CMakeLists.txt.
Second: Put your cpp inside the src folder and your headers in the include folders.
Third: Your CMakeLists.txt should look like this:
cmake_minimum_required(VERSION 2.8)
PROJECT (name)
find_package(OpenCV REQUIRED )
set( NAME_SRC
src/main.cpp
)
set( NAME_HEADERS
include/header.h
)
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( name ${NAME_SRC} ${NAME_HEADERS} )
target_link_libraries( sample_pcTest ${OpenCV_LIBS} )
Fourth: Open CMake GUI and select the root folder as input and create a build folder for the output. Click configure, then generate, and choose the generator (VisualStudio, Eclipse, ...)
I am using opencv3.0 and cmake3.8,
config below work for me!
######## A simple cmakelists.txt file for OpenCV() #############
cmake_minimum_required(VERSION 2.8)
PROJECT(word)
FIND_PACKAGE( OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )
ADD_EXECUTABLE(word main.c)
TARGET_LINK_LIBRARIES (word ${OpenCV_LIBS})
########### end ####################################

Resources