你参考的资料有:
https://blog.csdn.net/qq_36679208/article/details/109639607
https://blog.csdn.net/qq_27278957/article/details/120283706
https://blog.csdn.net/fengxinzioo/article/details/105646969
一、CMAKE将OpenCV原码生成能编译出库的VS工程:
1、CMAKE编译只有警告,但是VS编译后有1686个错误,主要有一下几种类型:
(1)严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2059 语法错误:“)” (编译源文件 D:\OpenCV450\opencv-4.5.0_vs2017_64\modules\world\test\test_intrin512.avx512_skx.cpp) opencv_test_core_AVX512_SKX
(2)严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2019 无法解析的外部符号 “void __cdecl ncvSetDebugOutputHandler(void (__cdecl*)(class std::basic_string<char,struct std::char_traits,class std::allocator > const &))” (?ncvSetDebugOutputHandler@@YAXP6AXAEBV?
b
a
s
i
c
s
t
r
i
n
g
@
D
U
?
basic_string@DU?
basicstring@DU?char_traits@D@std@@V?KaTeX parse error: Expected group after '_' at position 40: …),该符号在函数 "bool _̲_cdecl nvidia_N…basic_string@DU?
c
h
a
r
t
r
a
i
t
s
@
D
@
s
t
d
@
@
V
?
char_traits@D@std@@V?
chartraits@D@std@@V?allocator@D@2@@std@@W4OutputLevel@@@Z) 中被引用 opencv_test_cudalegacy D:\OpenCV450\opencv-4.5.0_vs2017_64\modules\world\main_nvidia.obj 1
(3)严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2019 无法解析的外部符号 "double __cdecl cv::aruco::calibrateCameraAruco(class cv::_InputArray const &,class cv::_InputArray const &,class cv::InputArray const &,struct cv::Ptr const &,class cv::Size,class cv::_InputOutputArray const &,class cv::_InputOutputArray const &,class cv::_OutputArray const &,class cv::OutputArray const &,int,class cv::TermCriteria)" (?calibrateCameraAruco@aruco@cv@@YANAEBV_InputArray@2@00AEBU?
P
t
r
@
V
B
o
a
r
d
@
a
r
u
c
o
@
c
v
@
@
@
2
@
V
?
Ptr@VBoard@aruco@cv@@@2@V?
Ptr@VBoard@aruco@cv@@@2@V?Size@H@2@AEBV_InputOutputArray@2@3AEBV_OutputArray@2@4HVTermCriteria@2@@Z),该符号在函数 “struct _object * __cdecl pyopencv_cv_aruco_calibrateCameraAruco(struct _object *,struct _object *,struct _object *)” (?pyopencv_cv_aruco_calibrateCameraAruco@@YAPEAU_object@@PEAU1@00@Z) 中被引用 opencv_python3 D:\OpenCV450\opencv-4.5.0_vs2017_64\modules\python3\cv2.obj 1
(4)严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK1120 9 个无法解析的外部命令 opencv_interactive-calibration D:\OpenCV450\opencv-4.5.0_vs2017_64\bin\Release\opencv_interactive-calibration.exe 1
(5)严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK1120 1 个无法解析的外部命令 opencv_waldboost_detector D:\OpenCV450\opencv-4.5.0_vs2017_64\bin\Release\opencv_waldboost_detector.exe 1
点击工具中的“扩展与更新”进行2017版本的内的小版本(2017后面的数字不一样)更新,上面的inrin_avx512.hpp文件中的300多个错误就解决了,参考https://www.pianshen.com/article/66371558174/ 但会新报下面错误:
解决:工具中的“获取工具与功能”给下图中的Windows 10的对应的SDK版本选上即可,这里选了所有的!
但还会报如下错误,这个下面的
此时得到了看“输出”纠错的经验!!! 输出显示nvcc检测的到的CUDA版本10.1不行,匹配不了8.6算力的3070laptop,所以后面你又重新建工程配了个11.6的,注意CMAKE的配置除了下面两篇博客的配置外(两篇博客中设置兼顾就没有错误),还需要注意一下几点:
https://blog.csdn.net/qq_27278957/article/details/120283706
https://blog.csdn.net/qq_36679208/article/details/109639607
注意点:
(1)在cmake之前,给下载的.cache文件夹复制到D:\OpenCV450_4\opencv-4.5.0目录下并解压
(2)pyhon27的安装与环境变量的设置(你电脑配置好了,就不用重复配置了)
(3)在configure(cmake)时出现如下警告则需要去勾选(cmake时,gui中的configure相当于cmake命令,generation相当于make命令,此处不是生成能运行的动态链接库,而是生成VS工程,此处不报警告也不需要去勾选这一项)
(3)4、下载错误有D:\OpenCV450_3\opencv-4.5.0_vs2017_64\CMakeDownloadLog.txt打印信息,(缺少相应文件,或者不对的需要下载覆盖的,走里面相应的网址下载然后覆盖,一般开了外网就不需要了)
(4)bgm_i文件配置,参考博客:https://blog.csdn.net/qq_36679208/article/details/109639607。(一般开了外网也不需要了)
二、 VS编译
换成11.6的CUDA后Release版本是编译生成和安装生成都没有问题了,但会有7千多个警告,其中有一部分是因为你没有删掉小于你显卡算力的算力设置导致的!
配置好的Release库位于下面目录中:D:\OpenCV450_4\opencv-4.5.0_vs2017_64\install
博客上是成功两个,这里是成功1个,可能是漏了啥库了,以后可能还需要在配置,注意你配了很多次,就最后的D:\OpenCV450_4文件夹下面配置成功了!!!
三、配置环境变量:
**
四、 测试(对应的OpenCV+CUDA处理图像的工程建立与配置方法见world文档或者CSDN上对应的自己记录的一篇文章,两个地方的内容是一样的!)
测试代码:
#include
#include “opencv2/opencv.hpp”
#include “opencv2/core.hpp”
#include <opencv2/cudawarping.hpp>
//#include “opencv2/cudawarping.hpp”
#include “opencv2/cudaarithm.hpp”
#include “opencv2/core/cuda.hpp”
using namespace std;
using namespace cv;
//#define CUDA_OPENCV
int main()
{
Mat img1 = imread(“1.jpg”);
Mat img2 = imread(“2.jpg”);
if (img1.empty() | img2.empty()) {
cout << "Could not open an image!" << endl;
return -1;
}
int len = 10000;
auto start = std::chrono::system_clock::now();
#ifdef CUDA_OPENCV
//Create memory for resorting images on device
cuda::GpuMat d_result, d_img1, d_img2;// noArray;
//cuda::Stream & String1 = cuda::Stream::Null();
//Upload images to device
d_img1.upload(img1);
d_img2.upload(img2);
Mat h_result;
for (size_t i = 0; i < len; i++)
{
cv::cuda::add(d_img1, d_img2, d_result);
}
//Download result back to host
d_result.download(h_result);
#else
Mat h_result;
for (size_t i = 0; i < len; i++)
{
cv::add(img1, img2, h_result);
}
#endif // CUDA_OPENCV
auto end = std::chrono::system_clock::now();
cout << "Times:" << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() * 1.0f / len << "ms";
imshow("Image1", img1);
imshow("Image2", img2);
imshow("Result", h_result);
waitKey();
system("pause");
return 0;
}
报错:
(1)严重性 代码 说明 项目 文件 行 禁止显示状态
错误 The CUDA Toolkit v10.1 directory ‘’ does not exist. Please verify the CUDA Toolkit is installed properly or define the CudaToolkitDir property to resolve this error. test_CSDN_3 C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\BuildCustomizations\CUDA 10.1.targets 587
解决: 参考的官方论坛:https://forums.developer.nvidia.com/t/the-cuda-toolkit-v10-0-directory-does-not-exist/65821
(2)error : namespace “cv::cuda” has no member “add” 1> 找不到cv::cuda 的成员(搜也搜不到完全对应问题的解决)
注意这里百分百是没有添加相应的头文件hpp,添加了对应的就编译不报错了:
#include
#include “opencv2/opencv.hpp”
#include “opencv2/core.hpp”
#include <opencv2/cudawarping.hpp>
//#include “opencv2/cudawarping.hpp”
#include “opencv2/cudaarithm.hpp”
#include “opencv2/core/cuda.hpp”
(3)运行的时候老师报内存不行,(搜也搜不到完全对应问题的解决)
这里内存不行不是真的内存不行,而是没有添加编译好的库里的dll文件到工程的Release|Debug文件下或者系统文件夹下!
运行后结果如下:
CPU:
GPU:
这里的GPU运行时间比CPU长,说明你电脑上的CPU性能非常好,GPU在这里的运算优势体现不出来!
下面Debug库编译的时候的cmakeGUI设置集合了两篇博客里的设置,所以一点错误没有,后面Release也重新生成了一次!
BUID生成输出窗口
BUILD生成错误列表窗口
INSTALL生成输出窗口
INSTALL生成错误列表窗口
配置好Debug后报错:
VSDebug调试报错无法定位程序输入点于动态链接库 .exe
解决:你把Debug和Release的dll文件拷混了
**