how to use cuda with opencv on ubuntu 11.10 - opencv

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

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?

CMake fails to find OpenCV installed via vcpkg under wsl (returns opencv for Windows Conda Environment)

So I am currently trying to get my CMake Project to build after installing packages via vcpkg for the first time.
I am using WSL2 under Win10 with Ubuntu 20.04 for the project, but building throws the following:
philip#DESKTOP-2CHKCMO:/mnt/c/Users/phili/_Documents/Projects/pixelator$ cmake .
-- OpenCV ARCH:
-- OpenCV RUNTIME:
-- OpenCV STATIC: ON
CMake Warning at /mnt/c/Users/phili/anaconda3/Library/opencvConfig.cmake:140 (message):
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.
Call Stack (most recent call first):
CMakeLists.txt:13 (find_package)
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/phili/_Documents/Projects/pixelator
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(pixelator LANGUAGES CXX)
#include("~/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(CMAKE_TOOLCHAIN_FILE "~/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(OpenCV_DIR "~/vcpkg/packages/opencv4_x64-linux/share/opencv")
find_package(protobuf CONFIG REQUIRED)
find_package(opencv CONFIG REQUIRED)
find_package(gflags CONFIG REQUIRED)
add_library(pixeling ${CMAKE_SOURCE_DIR}/source/pixeling.cpp)
target_link_libraries(pixeling ${OpenCV_LIBS})
add_executable(io ${CMAKE_SOURCE_DIR}/source/io.cpp)
target_link_libraries(io pixeling gflags)
The Folder is located in Windows10, but I am building via Ubuntu.
Apparently CMake seems to ignore that I set the OpenCV_DIR and returns an openCV installation for my python anaconda environment (which is not located in wsl, but Windows), which it can't use. Also, I was under the impression that I would not even be required to set the OpenCV_DIR after linking the Toolchain file?
How do I get it to find the right openCV installation?

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)

Error configuring OpenCV project with CMake on Windows

I used the prebuilt OpenCV 3.0.0 version. It works when I create a Visual Studio project directly, and when I add include and library directories.
Now I am making a CMakeList.txt file (so that I can run the same code on different systems). It will be used to create the Visual Studio project on Windows, and Makefile on Unixes, etc.
cmake_minimum_required(VERSION 2.8)
project( Facade )
find_package( OpenCV REQUIRED )
add_executable( Facade grammar.cpp )
target_link_libraries( Facade ${OpenCV_LIBS} )
When I generate the Visual Studio file on Windows, I got the following error:
CMake Warning at C:/opencv/build/OpenCVConfig.cmake:166 (message):
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.
Call Stack (most recent call first):
CMakeLists.txt:3 (find_package)
CMake Error at CMakeLists.txt:3 (find_package):
Found package configuration file:
C:/opencv/build/OpenCVConfig.cmake
but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
NOT FOUND.
Currently, OpenCV_DIR is C:\opencv\build, where it holds the CMake files. If I point OpenCV_DIR to C:\opencv\build\x64\vc12, it says, can't find CMake files.
How do I fix this?
What generator string did you use for the CMake configuration step? For VC12 x64, you need to use -G "Visual Studio 12 Win64" (if you were using CMake 3, you would use -G "Visual Studio 12 2013 Win64"). Note that you need to include Win64 at the end to get the x64 platform.
It is rather convenient that the OpenCV CMake config script is checking to make sure you are using the x64 binaries. You configured CMake to build 32-bit binaries, which would cause a runtime error if you were able to get an executable built.
Just in case anybody else has this problem, I got it from using old CMake config scripts. With VS2017 installed, I was getting this error- turns out that some auto-generated CMAKE configs have something like this:
elseif(MSVC_VERSION EQUAL 1910)
set(OpenCV_RUNTIME vc15)
I checked MSVC_VERSION and it was 1916, which is still VC15, but the files erroneously missed that. I changed to:
elseif((MSVC_VERSION EQUAL 1910) OR (MSVC_VERSION GREATER 1910))
set(OpenCV_RUNTIME vc15)
and it worked like a charm.

Using libfreenect and OpenCV with cmake

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.

Resources