Setting CMakeLists.txt for OpenCV GPU and CUDA

I couldn’t find a simple example of CMakeLists to compile my very first program using Opencv GPU like this:

#include <opencv2/opencv.hpp>
#include <opencv2/gpu/gpu.hpp> 
using namespace cv; 
... 
Mat image = imread("file.png"); 
gpu::GpuMat image_gpu; 
image_gpu .upload(image); 
gpu::GpuMat result; 
gpu::threshold(image_gpu, result, 128, CV_THRESH_BINARY); 
result.download(image); 
imshow("WindowName", image); 
waitKey ();

Can anyone suggest me a CMakeLists file with/without CUDA… please
and if you’ve any interesting tutorials to start with opencv gpu (mostly for compilation cmake…etc) would be great

Are you trying to compile this on the TX2? If so, the gpu:: libraries will not work if you’re using Opencv4Tegra right now. There was a compilation mistake which makes the gpu:: libraries unusable. The next update should have this fixed.

If you’ve build opencv from source, then a CMakeLists.txt file with contents similar to this should work!

cmake_minimum_required(VERSION X)
project(project_name)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(project_name source_file.cpp)
target_link_libraries(project_name ${OpenCV_LIBS})

You can find some OpenCV w/ CUDA and OpenCV w/o CUDA code on my GitHub page (ex: https://github.com/ShreyasSkandan/nvidia-tx1-tx2/tree/master/opencv_stuff/optical-flow)

Hope that answers your questions.

Yes I’m using TX2 and yes Opencv4Tegra is the one installed… So do you say that I can’t use opencv gpu functions ? shoud I install another version of opencv from source?

I can compile all my codes using opencv cpu. Now I want to move to gpu so I started with simple code (posted above in my question) but it doesn’t work with my cmakelistes nor with yours.

I checked your GitHub but it doesn’t have any code with GPU all with opencv cpu in which I don’t have any issues…

You can find more information regarding the problems with the gpu:: functions and Opencv4Tegra here:

https://devtalk.nvidia.com/default/topic/1000106/?comment=5114203
https://devtalk.nvidia.com/default/topic/1000701/?comment=5176691

You are advised to build from source if you wish to use the gpu:: functions immediately,

https://devtalk.nvidia.com/default/topic/1000106/jetson-tx2/opencv-convertto-failure/post/5133788/#5133788

Yes, my GitHub code lacks any gpu:: functions because I’d rather not build from source at this point and lose some of the SIMT optimizations that Opencv4Tegra has.

Okay thanks for the links… the issue was posted the first time on 03/18/2017 so it is months ago and we don’t know when it will be fixed. I prefer to build from source for now.

Do you think that the same CMakeLists you gave me or that I use normally for CPU should work with gpu:: library?

Yup, it should/does work. I’ve tried the gpu:: libraries with the older Opencv4Tegra on the TX1 and it works!

I uninstalled OpenCV4Tegra from TX2 and installed the new version of OpenCv from source and it works (the gpu lib).

Hai, Can i ask u something bro?
When i run comand “cmake .”
I get this error.
Please help me.

I use ubuntu 16.04, cuda-8.0, opencv3.1

CMake Error at /usr/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find CUDA: Found unsuitable version “7.5”, but required is exact
version “8.0” (found /usr)
Call Stack (most recent call first):
/usr/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:386 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-3.5/Modules/FindCUDA.cmake:949 (find_package_handle_standard_args)
/usr/local/share/OpenCV/OpenCVConfig.cmake:48 (find_package)
/usr/local/share/OpenCV/OpenCVConfig.cmake:291 (find_host_package)
CMakeLists.txt:3 (find_package)

– Configuring incomplete, errors occurred!
See also “/home/panjaitan/Documents/HoughLine/CMakeFiles/CMakeOutput.log”.
See also “/home/panjaitan/Documents/HoughLine/CMakeFiles/CMakeError.log”.