Using libfreenect and OpenCV with cmake - opencv

So I am trying to work with the Kinect by using the libfreenect driver and OpenCV. I want to be able to create the project using CMake. I was able to get the proper CMakeList for me to be able to load the OpenCV librery. Now I want to input video using the kinect but cant find any help for this.
Also I'm using Ubuntu 12.04 64bit on a laptop.
How can I do this using Cmake?
p.s. I was able to install libfreenect properly, the demo programs run just fine.

You might want to have a look at this:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project("My Project")
find_package(OpenCV REQUIRED)
find_package(Threads REQUIRED)
find_package(libfreenect REQUIRED)
include_directories("/usr/include/libusb-1.0/")
add_executable(regtest src/regtest.cpp
src/features.cpp)
target_link_libraries(regtest ${OpenCV_LIBS}
${CMAKE_THREAD_LIBS_INIT}
${FREENECT_LIBRARIES})
add_executable(main src/main.cpp
src/features.cpp)
target_link_libraries(main ${OpenCV_LIBS}
${CMAKE_THREAD_LIBS_INIT}
${FREENECT_LIBRARIES})
This is the CMakeLists.txt I used for a project using both OpenCV and freenect.

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

Forcing cmake to use a specific OpenCV version

My Ubuntu 14.04 has OpenCV 2.4.8 installed by default, and I also have a hand-built OpenCV 2.4.11 that I need for the cv::fisheye classes, which I build with cmake to produce my executable. The problem is that I am using ROS, which also defaults to 2.4.8, but I need to link with 2.4.11. The hack I have in place is:
find_package(OpenCV REQUIRED)
# Needed to force OpenCV 2.4 to link with 2.4.11
#message("Initial value for ${OpenCV_VERSION}: ${OpenCV_LIBS}")
if(OpenCV_VERSION VERSION_LESS "3.0")
set(OpenCV_VERSIONED_LIBS "")
macro(set_opencv_version version)
foreach( LIB_FILE ${ARGN} )
set(OpenCV_VERSIONED_LIBS ${OpenCV_VERSIONED_LIBS} :lib${LIB_FILE}.so.${version})
endforeach()
endmacro()
set_opencv_version("2.4.11" ${OpenCV_LIBS})
else()
set(OpenCV_VERSIONED_LIBS ${OpenCV_LIBS})
endif()
#message("Updated value: ${OpenCV_VERSIONED_LIBS}")
target_link_libraries(my_fisheye_application
${OpenCV_VERSIONED_LIBS}
${catkin_LIBRARIES}
cv_bridge # This by default pulls in OpenCV 2.4.8 libraries
# ...etc...
)
As this script needs to work on different machines and with OpenCV 3.0 as well as 2.4.11, I cannot really make any assumptions about the directories in use. Is there a better way to do this?
If you want to force CMake (the FindOpenCV module) to search for an OpenCV library different from the package/system installed one,
you need to set OpenCV_ROOT_DIR variable to the base directory of OpenCV tree to use.
Also, because you want at least a specific minimum version of the OpenCV library, it's better to state it in the find_package() function, as the following:
find_package(OpenCV 2.4.11 REQUIRED)

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 use cuda with opencv on ubuntu 11.10

I would like to run surfgpu on ubuntu .But do not know how to write cmakelists.I have installed the CUDA 4.2 SDK and Toolkit, and C inside the program can run.My development environment is Qt.ubuntu 11.10.opencv2.4.2 any good suggestions are appreciated.
How about this?
# CMakeLists.txt to build OpenCV project
cmake_minimum_required(VERSION 2.8)
project( testOpenCV )
Find OpenCV and CUDA package
find_package(OpenCV REQUIRED )
find_package(CUDA 4.2 REQUIRED)
Include from some directories
# Since surf is a non free package, you also have to add non free include dir
include_directories( ${OpenCV_INCLUDE_DIR} "${OpenCV_SOURCE_DIR}/modules/nonfree/include" ${CUDA_INCLUDE_DIRS})
Compile source
cuda_add_executable( exefile source.cpp source2.cpp )
target_link_libraries( exefile ${OpenCV_LIBS} ${otherlibsyouneed} )
All of the above only work if you compiled OpenCV with -DHAVE_CUDA

Resources