记录 vs2019+opencv4.7.0+cuda11.8 cmake编译
原文链接:cmake 编译opencv4.5.5/opencv4.6.0/opencv4.7.0 gpu版以及vs2022配置opencv-gpu_小广向前进的博客-CSDN博客
1、Debug
这里可以改编译Debug还是Release,默认是Release。
2、测试代码
#include <iostream> #include "opencv2/core.hpp" #include "opencv2/cudaarithm.hpp" #include <opencv2/opencv.hpp> #include <opencv2/highgui.hpp> #include <opencv2/cudafeatures2d.hpp> #include <opencv2/xfeatures2d/cuda.hpp> using namespace std; using namespace cv; using namespace cv::cuda; int main() { int num_devices = getCudaEnabledDeviceCount(); if (num_devices == 0) { std::cout << "OpenCV is compiled without CUDA support" << endl; return -1; } else if (num_devices == -1) { std::cout << "CUDA driver is not installed" << endl; return -1; } else if (num_devices >= 1) { std::cout << "CUDA-Opencv can be used and the number of GPU is :" << num_devices << endl; return -1; } return 0; }
当num_devices等于0的时候,表示没有使用gpu,可能的原因有两个:一是cuda没配置好,二是受到以前配置过cpu版的opencv的影响,在vs配置里面把之前的opencv路径都去掉,然后在环境变量那里,将cpu版的opencv路径移动到gpu版的路径下方。