Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2

    • 0.引言
    • 1.Cmake配置OpenCV
      • 1.1.不带contrib的配置(configure)
      • 1.2.带contrib的配置(configure)
    • 2.Visual Studio编译OpenCV
    • 3.配置环境
      • 3.1.debug x64
      • 3.2.release x64
      • 3.3.Test
    • 4.opencv430 with contrib and CUDA10.2
      • 4.1.Cmake与VS编译
      • 4.2.环境配置
      • 4.3.Test

0.引言

因为需要用到GPU的一些功能,重新安装一下opencv并做一个记录。

  • cmake download.
  • visual studio download.
  • opencv download.
  • opencv contrib download.

OpenCV下载注意分支对应.OpenCV保存路径:D:\Program Files\opencv430

相关配置:

  • win10x64
  • VS2017
  • NVIDIA:GeForce GTX 1050
  • QT 5.11.1
  • CUDA:cuda_10.2.89_441.22_win10
  • cuDNN:cudnn-10.2-windows10-x64-v7.6.5.32
  • opencv4.3.0 with contrib
  • cmake-3.16

1.Cmake配置OpenCV

就在opencv430根目录建build文件夹。
Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第1张图片

D:/Program Files/opencv430/opencv-4.3.0
D:/Program Files/opencv430/build

1.1.不带contrib的配置(configure)

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第2张图片

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第3张图片

有几个文件下载失败,根据红色部分的提示打开CMakeDownloadLog.txt里面有下载改文件的地址,下载后将其改名字然后放到相应的目录。

后面还有下载失败的,所以先建个文件夹暂存下载的文件吧。

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第4张图片

日志文件:

#do_copy "opencv_videoio_ffmpeg.dll" "3b094c37d270a30f0b20a0bc8d3ecafb" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/3d2e97081683265950316c65a52c2e8858ffba1b/ffmpeg/opencv_videoio_ffmpeg.dll" "D:/Program Files/opencv430/build/3rdparty/ffmpeg"
#missing "D:/Program Files/opencv430/build/3rdparty/ffmpeg/opencv_videoio_ffmpeg.dll"
#check_md5 "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/3b094c37d270a30f0b20a0bc8d3ecafb-opencv_videoio_ffmpeg.dll"
#mismatch_md5 "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/3b094c37d270a30f0b20a0bc8d3ecafb-opencv_videoio_ffmpeg.dll" "d41d8cd98f00b204e9800998ecf8427e"
#delete "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/3b094c37d270a30f0b20a0bc8d3ecafb-opencv_videoio_ffmpeg.dll"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/3b094c37d270a30f0b20a0bc8d3ecafb-opencv_videoio_ffmpeg.dll" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/3d2e97081683265950316c65a52c2e8858ffba1b/ffmpeg/opencv_videoio_ffmpeg.dll"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "opencv_videoio_ffmpeg_64.dll" "388ee23a7ca44eef2344e265fafd5940" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/3d2e97081683265950316c65a52c2e8858ffba1b/ffmpeg/opencv_videoio_ffmpeg_64.dll" "D:/Program Files/opencv430/build/3rdparty/ffmpeg"
#missing "D:/Program Files/opencv430/build/3rdparty/ffmpeg/opencv_videoio_ffmpeg_64.dll"
#check_md5 "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/388ee23a7ca44eef2344e265fafd5940-opencv_videoio_ffmpeg_64.dll"
#mismatch_md5 "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/388ee23a7ca44eef2344e265fafd5940-opencv_videoio_ffmpeg_64.dll" "d41d8cd98f00b204e9800998ecf8427e"
#delete "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/388ee23a7ca44eef2344e265fafd5940-opencv_videoio_ffmpeg_64.dll"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/388ee23a7ca44eef2344e265fafd5940-opencv_videoio_ffmpeg_64.dll" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/3d2e97081683265950316c65a52c2e8858ffba1b/ffmpeg/opencv_videoio_ffmpeg_64.dll"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "ffmpeg_version.cmake" "ad57c038ba34b868277ccbe6dd0f9602" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/3d2e97081683265950316c65a52c2e8858ffba1b/ffmpeg/ffmpeg_version.cmake" "D:/Program Files/opencv430/build/3rdparty/ffmpeg"
#missing "D:/Program Files/opencv430/build/3rdparty/ffmpeg/ffmpeg_version.cmake"
#check_md5 "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/ad57c038ba34b868277ccbe6dd0f9602-ffmpeg_version.cmake"
#mismatch_md5 "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/ad57c038ba34b868277ccbe6dd0f9602-ffmpeg_version.cmake" "d41d8cd98f00b204e9800998ecf8427e"
#delete "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/ad57c038ba34b868277ccbe6dd0f9602-ffmpeg_version.cmake"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/ffmpeg/ad57c038ba34b868277ccbe6dd0f9602-ffmpeg_version.cmake" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/3d2e97081683265950316c65a52c2e8858ffba1b/ffmpeg/ffmpeg_version.cmake"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

根据日志中网址下载后改名字:

#放置路径 D:\Program Files\opencv430\opencv-4.3.0\.cache\ippicv
ippicv_2020_win_intel64_20191018_general.zip     ====改为hash值的名字===>
879741a7946b814455eee6c6ffde2984-ippicv_2020_win_intel64_20191018_general.zip

#以下三个放置路径 D:\Program Files\opencv430\opencv-4.3.0\.cache\ffmpeg
ffmpeg_version.cmake     ====改为hash值的名字===>
ad57c038ba34b868277ccbe6dd0f9602-ffmpeg_version.cmake

opencv_videoio_ffmpeg.dll    ====改为hash值的名字===>
3b094c37d270a30f0b20a0bc8d3ecafb-opencv_videoio_ffmpeg.dll

opencv_videoio_ffmpeg_64.dll    ====改为hash值的名字===>
388ee23a7ca44eef2344e265fafd5940-opencv_videoio_ffmpeg_64.dll

再次配置就OK了!

1.2.带contrib的配置(configure)

OPENCV_EXTRA_MODULES_PATH : D:/Program Files/opencv430/opencv_contrib-4.3.0/modules

如果需要用 SURF,SIFT算法的话把OPENCV_ENABLE_NONFREE勾上。BUILD_opencv_world勾上,最后编译的结果为两个,一个debug版本,一个release版本。

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第5张图片
configure后依然有文件下载失败,同样打开CMakeDownloadLog.txt查看:

#use_cache "D:/Program Files/opencv430/opencv-4.3.0/.cache"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_IPPICV_HASH_3rdparty_ippicv_ippicv_2020_win_intel64_20191018_general_zip"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_ADE_HASH_3rdparty_ade_v0_1_1f_zip"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_opencv_videoio_ffmpeg_dll"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_opencv_videoio_ffmpeg_64_dll"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_ffmpeg_version_cmake"
#do_copy "boostdesc_bgm.i" "0ea90e7a8f3f7876d450e4149c97c74f" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/boostdesc_bgm.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/boostdesc/0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "boostdesc_bgm_bi.i" "232c966b13651bd0e46a1497b0852191" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/boostdesc_bgm_bi.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/boostdesc/232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "boostdesc_bgm_hd.i" "324426a24fa56ad9c5b8e3e0b3e5303e" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/boostdesc_bgm_hd.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/boostdesc/324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "boostdesc_binboost_064.i" "202e1b3e9fec871b04da31f7f016679f" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/boostdesc_binboost_064.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/boostdesc/202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "boostdesc_binboost_128.i" "98ea99d399965c03d555cef3ea502a0b" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/boostdesc_binboost_128.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/boostdesc/98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "boostdesc_binboost_256.i" "e6dcfa9f647779eb1ce446a8d759b6ea" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/boostdesc_binboost_256.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/boostdesc/e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "boostdesc_lbgm.i" "0ae0675534aa318d9668f2a179c2a052" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/boostdesc_lbgm.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/boostdesc/0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "vgg_generated_48.i" "e8d0dcd54d1bcfdc29203d011a797179" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/vgg_generated_48.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/vgg/e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "vgg_generated_64.i" "7126a5d9a8884ebca5aea5d63d677225" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/vgg_generated_64.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/vgg/7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "vgg_generated_80.i" "7cd47228edec52b6d82f46511af325c5" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/vgg_generated_80.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/vgg/7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "vgg_generated_120.i" "151805e03568c9f490a5e3a872777b75" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i" "D:/Program Files/opencv430/build/downloads/xfeatures2d"
#missing "D:/Program Files/opencv430/build/downloads/xfeatures2d/vgg_generated_120.i"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/xfeatures2d/vgg/151805e03568c9f490a5e3a872777b75-vgg_generated_120.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

#do_copy "face_landmark_model.dat" "7505c44ca4eb54b4ab1e4777cb96ac05" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat" "D:/Program Files/opencv430/build/testdata/cv/face/"
#missing "D:/Program Files/opencv430/build/testdata/cv/face//face_landmark_model.dat"
#cmake_download "D:/Program Files/opencv430/opencv-4.3.0/.cache/data/7505c44ca4eb54b4ab1e4777cb96ac05-face_landmark_model.dat" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Couldn't resolve host 'raw.githubusercontent.com'
# Closing connection 0
# 

这里网址输入后是txt,右键另存为会自动显示保存的名称。
Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第6张图片

根据日志文件提示分别放入

#文件夹需要自己根据路径建
D:\Program Files\opencv430\build\downloads\xfeatures2d
D:\Program Files\opencv430\build\testdata\cv\face

再次configure,就OK了。

我感觉上一步也可以这样,不用改名字,放在missing路径下就OK

configure成功后,Generate生成vs工程文件。 之后直接Open Project,则会使用前面选择的vs版本打开工程。
Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第7张图片

2.Visual Studio编译OpenCV

在菜单栏上选择:生成 ->批生成。可同时编译debug版本和release版本。
编译完后,把build/install文件夹放到opencv430文件夹下就OK。

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第8张图片

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第9张图片

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第10张图片
Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第11张图片

3.配置环境

这个就是平时遇到的操作了。

环境变量配置:此电脑右键属性–>高级系统设置–>环境变量–>系统变量–>Path–>新建

D:\Program Files\opencv430\install\x64\vc15\bin

3.1.debug x64

打开VS新建一个工程,新建属性表opencv430x64d.props

  • include目录:在通用属性–>VC++目录–>包含目录中添加
D:\Program Files\opencv430\install\include
D:\Program Files\opencv430\install\include\opencv2
  • lib目录:通用属性–>VC++目录–>库目录中添加
D:\Program Files\opencv430\install\x64\vc15\lib
  • 库文件:通用属性–>链接器–>输入–>附加的依赖项中添加
opencv_img_hash430d.lib
opencv_world430d.lib

3.2.release x64

新建属性表opencv430x64r.props

  • include目录:在通用属性–>VC++目录–>包含目录中添加
D:\Program Files\opencv430\install\include
D:\Program Files\opencv430\install\include\opencv2
  • lib目录:通用属性–>VC++目录–>库目录中添加
D:\Program Files\opencv430\install\x64\vc15\lib
  • 库文件:通用属性–>链接器–>输入–>附加的依赖项中添加
opencv_img_hash430.lib
opencv_world430.lib

然后将属性表拷贝到安装目录,以便后续查找。

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第12张图片
最后,注销一下电脑,进行测试,不需要重启。

3.3.Test

#include 
#include 
#include 
#include 
#include 
 
using namespace cv;
using namespace std;
 
int main()
 
{
	Mat src = imread("F:/vs_test/lena.jpg");
 
	if (src.data == NULL)
	{
		cout << "No exist" << endl;
		return -1;
	}
	namedWindow("input image", WINDOW_AUTOSIZE);
	namedWindow("output image", WINDOW_AUTOSIZE);
	imshow("input image", src);
	Ptr<Feature2D> sift = xfeatures2d::SIFT::create(400); //括号参数越大越精确,自定
	vector <KeyPoint> keypointsa;
	Mat a;
 
	sift->detectAndCompute(src, noArray(), keypointsa, a); //得到特征点和特征点描述
	drawKeypoints(src, keypointsa, src);//画出特征点
 
	//显示
	imshow("output image", src);
 
	waitKey();
	return 0;
}

4.opencv430 with contrib and CUDA10.2

4.1.Cmake与VS编译

  • 为什么带CUDA与不带CUDA版本要分开编译?

不带CUDA版本的opencv方便拷贝到其他电脑使用,同时拷贝install文件夹和属性表,按照安装目录放置就OK,只需要配置一个环境变量就可以直接使用了,很方便。但是带CUDA的版本就不一样了,需要CUDA版本相同,稍微麻烦一点,同时为了降低出错率,选择分开编译。

  • win10安装opencv

将上面编译好的文件夹opencv430复制一份,并改名为opencv430withCUDA。将build文件夹里面的testdatadownloads保留,其他的删除,将opencv-4.3.0\.cacha\ffmpegippicv保留,其他的删除,保留的就是上面自己下载的文件。

打开CMake,点击File-->Delete Cache重新输入路径。

D:/Program Files/opencv430withCUDA/opencv-4.3.0
D:/Program Files/opencv430withCUDA/build

D:/Program Files/opencv430withCUDA/opencv_contrib-4.3.0/modules

需要勾选的选项有:

BUILD_opencv_world 
OPENCV_ENABLE_NONFREE
WITH_CUDA
MKL_WITH_OPENMP  #这是一个并行加速库,可以不选,只是我需要用
OPENCV_DNN_CUDA
WITH_TBB    #默认情况下是OFF、勾选可以获得并行处理支持
CUDA_ARCH_BIN  6.1 #删除其他的数字,这个6.1是显卡计算力,根据自己的显卡填写,就是这里十分耗时

按照上面的流程走一遍,下载失败的文件都已经下载了,只要CUDA安装没出错就比较快 ,带CUDA 的编译好慢!!流程上没什么大问题,就是VS编译很慢。

configure完成后,Open Project打开工程:生成–>批生成。把install文件夹拷贝出来。

4.2.环境配置

环境变量配置:此电脑右键属性–>高级系统设置–>环境变量–>系统变量–>Path–>新建

D:\Program Files\opencv430withCUDA\install\x64\vc15\bin

注销一下电脑,或者全部配置完后再注销。

4.2.1.debug x64

打开VS新建一个工程,新建属性表opencv430x64d.props

  • include目录:在通用属性–>VC++目录–>包含目录中添加
D:\Program Files\opencv430WithCUDA\install\include
D:\Program Files\opencv430withCUDA\install\include\opencv2
  • lib目录:通用属性–>VC++目录–>库目录中添加
D:\Program Files\opencv430withCUDA\install\x64\vc15\lib

如果前面没有勾选BUILD_opencv_world,库文件会比较多,此时库文件的名称可以使用命令行获取,区别于Linux的命令行,Windows的脚本可以使用后缀名为**.bat的文件:
Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第13张图片

cd  D:\Program Files\opencv430withCUDA\install\x64\vc15\lib
dir /b *430d.lib  >DebugLibName.txt && move  "DebugLibName.txt"  "D:\Program Files\opencv430withCUDA\DebugLibName.txt"
dir /b *430.lib  >ReleaseLibName.txt && move  "ReleaseLibName.txt"  "D:\Program Files\opencv430withCUDA\ReleaseLibName.txt"

双击运行后在D:\Program Files\opencv430withCUDA\中打开txt文档复制就好。
Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第14张图片

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第15张图片

在这里插入图片描述

  • 库文件:通用属性–>链接器–>输入–>附加的依赖项中添加
opencv_img_hash430d.lib
opencv_world430d.lib

4.2.2.release x64

新建属性表opencv430x64r.props

  • include目录:在通用属性–>VC++目录–>包含目录中添加
D:\Program Files\opencv430WithCUDA\install\include
D:\Program Files\opencv430withCUDA\install\include\opencv2
  • lib目录:通用属性–>VC++目录–>库目录中添加
D:\Program Files\opencv430withCUDA\install\x64\vc15\lib
  • 库文件:通用属性–>链接器–>输入–>附加的依赖项中添加
opencv_img_hash430.lib
opencv_world430.lib

最后将属性表拷贝到安装目录,以便后续查找。
Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第16张图片
安装好后可以删除opencv430opencv430withCUDA下的build文件夹。很大。注销一下电脑,进行测试,不需要重启。

4.3.Test

#include 
 
#include "opencv2/opencv_modules.hpp"
#include "opencv2/core.hpp"
#include "opencv2/features2d.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()
{
	GpuMat img1, img2;
	img1.upload(imread("1.bmp", IMREAD_GRAYSCALE));
	img2.upload(imread("2.bmp", IMREAD_GRAYSCALE));
 
	cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
 
	SURF_CUDA surf;
 
	// detecting keypoints & computing descriptors
	GpuMat keypoints1GPU, keypoints2GPU;
	GpuMat descriptors1GPU, descriptors2GPU;
	surf(img1, GpuMat(), keypoints1GPU, descriptors1GPU);
	surf(img2, GpuMat(), keypoints2GPU, descriptors2GPU);
 
	cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl;
	cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl;
 
	// matching descriptors
	Ptr<cv::cuda::DescriptorMatcher> matcher = cv::cuda::DescriptorMatcher::createBFMatcher(surf.defaultNorm());
	vector<DMatch> matches;
	matcher->match(descriptors1GPU, descriptors2GPU, matches);
 
	// downloading results
	vector<KeyPoint> keypoints1, keypoints2;
	vector<float> descriptors1, descriptors2;
	surf.downloadKeypoints(keypoints1GPU, keypoints1);
	surf.downloadKeypoints(keypoints2GPU, keypoints2);
	surf.downloadDescriptors(descriptors1GPU, descriptors1);
	surf.downloadDescriptors(descriptors2GPU, descriptors2);
 
	// drawing the results
	Mat img_matches;
	drawMatches(Mat(img1), keypoints1, Mat(img2), keypoints2, matches, img_matches);
 
	namedWindow("matches", 0);
	imshow("matches", img_matches);
	waitKey(0);
 
	return 0;
}

安装完测试时报错,无法定位程序输入点于动态库上:

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第17张图片
很显然库没找到!在路径D:\Program Files\opencv430withCUDA\install\x64\vc15下有两个文件夹,一个bin,一个lib,从前面的配置可以看到,bin文件路径是添加到环境变量的,lib路径是添加到属性表里面的。其中bin里面是动态库dll和可执行文件exelib里面就是静态库。所以,如果是lib库没找到,那就是库目录,和库名称的问题;如果是动态库dll文件没找到,那就是环境变量的问题。但是环境变量已经设置了,那就真像只有一个,环境变量的顺序问题!!因为前面已经添加了不带CUDA版本的环境变量,这次添加的环境变量在其之后,故找错了库所以报错。

顺便查了一下可执行文件在运行时寻找和调用dll文件的路径搜索顺序。一般是:

  • A 可执行文件所在目录;
  • B 进程当前目录;
  • C 系统目录,%SystemRoot%/system32
  • D 16位系统目录;,%SystemRoot%/system
  • E Windows目录,%SystemRoot%/
  • F 环境变量PATH中的目录;

移动顺序后,成功运行:
Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第18张图片

Win10 Install OpenCV4.3.0 With Contrib and CUDA10.2_第19张图片

编译多个版本可能就会存在动态库查找顺序的问题,自己得清楚版本之间的差别在哪儿,遇到报错能反应过来。enjoy.

你可能感兴趣的:(DeBug备忘)