抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

靖待的技术博客

小清新IT旅程 | 为中华之崛起而读书

OpenCV文件编译及问题记录。

目前最新OpenCV官网发布最新版为3.4.0(2017年12月23日发布)。

OpenCV库使用方法

下载地址:https://github.com/opencv/opencv/releases/tag/3.4.0

https://sourceforge.net/projects/opencvlibrary/

法一:使用官方提供的已编译好的文件

优点:方便快捷
缺点:版本限制
选择opencv-3.4.0-vc14_vc15.exe,下载完成后可以直接打开解压,即可在自己的项目中直接使用。
具体步骤为:

  1. 配置环境变量
    打开电脑右键属性-高级系统设置-环境变量,Path中新增:E:\opencv\build\x64\vc14\bin,(此处为解压对应目录)。

  2. 引入相应头文件、库文件、链接库
    打开自己的工程,右键属性-配置属性-VC++目录-包含目录,新增:
    E:\opencv\build\include\opencv
    E:\opencv\build\include\opencv2
    E:\opencv\build\include

库目录,新增:
E:\opencv\build\x64\vc14\lib
(注意修改为你解压对应的目录)。

链接器-输入-附加依赖项,新增:
opencv_world340d.lib
点击确定即可。
至此所有准备工作就绪,可以愉快地编译自己的工程啦。

但想如此一劳永逸解决问题,需符合以下特点:你的VS版本是VC14(2015)或VC15(2017)。
另附几种对应关系:
Microsoft Visual Studio 6.0: VC6.0
Microsoft Visual Studio 2002: VC7.0
Microsoft Visual Studio 2003: VC7.1
Microsoft Visual Studio 2005: VC8.0
Microsoft Visual Studio 2008: VC9.0
Microsoft Visual Studio 2010: VC10.0
Microsoft Visual Studio 2012: VC11.0
Microsoft Visual Studio 2013: VC12.0
Microsoft Visual Studio 2014: VC13.0
Microsoft Visual Studio 2015: VC14.0
Microsoft Visual studio 2017: VC15.0

由于我使用VS2013,因此无法使用该方法。(当然,可以自行下载VS2015的运行库)。
我使用法二。

法二:自行编译

下载源码Source code并解压

下载CMake

下载地址:https://cmake.org/download/
选择相应操作系统版本,例如我选择Windows win64-x64 Installer。安装,打开bin\cmake-gui.exe。

使用CMake

在Where is the source code中选择OpenCV解压目录,如:E:/opencv-3.4.0/opencv-3.4.0。
一般可以在解压目录下新建一个build文件夹,用来存放编译生成的文件。
于是,在Where to build the binaries中填入该路径,如:E:/opencv-3.4.0/opencv-3.4.0/build。(也可自行选择路径)

点击Configure,选择所需环境,如:Visual Studio 12 2013。进入漫长的等待。
如果区域有红色出现,需再次点击Configure,直到全部变成白色。

第一次Configure后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
General configuration for OpenCV 3.4.0 =====================================
Version control: unknown

Platform:
Timestamp: 2018-01-29T14:29:24Z
Host: Windows 10.0.16299 AMD64
CMake: 3.10.2
CMake generator: Visual Studio 12 2013
CMake build tool: C:/Program Files (x86)/MSBuild/12.0/bin/MSBuild.exe
MSVC: 1800

CPU/HW features:
Baseline: SSE SSE2
requested: SSE2
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX
requested: SSE4_1 SSE4_2 AVX FP16 AVX2
SSE4_1 (3 files): + SSE3 SSSE3 SSE4_1
SSE4_2 (1 files): + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (2 files): + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (5 files): + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 AVX

C/C++:
Built as dynamic libs?: YES
C++11: YES
C++ Compiler: C:/software/vs2013/VC/bin/cl.exe (ver 18.0.21005.1)
C++ flags (Release): /DWIN32 /D_WINDOWS /W4 /GR /EHa /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:fast /arch:SSE /arch:SSE2 /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP12 /MD /O2 /Ob2 /DNDEBUG /Zi
C++ flags (Debug): /DWIN32 /D_WINDOWS /W4 /GR /EHa /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:fast /arch:SSE /arch:SSE2 /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP12 /MDd /Zi /Ob0 /Od /RTC1
C Compiler: C:/software/vs2013/VC/bin/cl.exe
C flags (Release): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:fast /arch:SSE /arch:SSE2 /MP12 /MD /O2 /Ob2 /DNDEBUG /Zi
C flags (Debug): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:fast /arch:SSE /arch:SSE2 /MP12 /MDd /Zi /Ob0 /Od /RTC1
Linker flags (Release): /machine:X86 /INCREMENTAL:NO /debug
Linker flags (Debug): /machine:X86 /debug /INCREMENTAL
ccache: NO
Precompiled headers: YES
Extra dependencies:
3rdparty dependencies:

OpenCV modules:
To be built: calib3d core dnn features2d flann highgui imgcodecs imgproc ml objdetect photo python_bindings_generator shape stitching superres ts video videoio videostab
Disabled: js world
Disabled by dependency: -
Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3 viz
Applications: tests perf_tests apps
Documentation: NO
Non-free algorithms: NO

Windows RT support: NO

GUI:
Win32 UI: YES
VTK support: NO

Media I/O:
ZLib: build (ver 1.2.11)
JPEG: build (ver 90)
WEBP: build (ver encoder: 0x020e)
PNG: build (ver 1.6.34)
TIFF: build (ver 42 - 4.0.9)
JPEG 2000: build (ver 1.900.1)
OpenEXR: build (ver 1.7.1)

Video I/O:
Video for Windows: YES
DC1394: NO
FFMPEG: NO
avcodec: NO
avformat: NO
avutil: NO
swscale: NO
avresample: NO
GStreamer: NO
DirectShow: YES

Parallel framework: Concurrency

Trace: YES (with Intel ITT)

Other third-party libraries:
Lapack: NO
Eigen: NO
Custom HAL: NO

NVIDIA CUDA: NO

OpenCL: YES (no extra features)
Include path: E:/opencv-3.4.0/opencv-3.4.0/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python (for build): NO

Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO

Matlab: NO

Install to: E:/opencv-3.4.0/opencv-3.4.0/build/install
-----------------------------------------------------------------

Configuring done

此时可能出现常见问题:

1
2
3
4
5
6
7
8
9
10
11
FFMPEG: Download: opencv_ffmpeg.dll
CMake Warning at cmake/OpenCVDownload.cmake:190 (message):
FFMPEG: Download failed: 28;"Timeout was reached"

FFMPEG: Download: opencv_ffmpeg_64.dll
CMake Warning at cmake/OpenCVDownload.cmake:190 (message):
FFMPEG: Download failed: 28;"Timeout was reached"

IPPICV: Download: ippicv_2017u3_win_ia32_general_20170822.zip
CMake Warning at cmake/OpenCVDownload.cmake:190 (message):
IPPICV: Download failed: 28;"Timeout was reached"

总之就是有文件下载不成功。根据CMake的提示,打开build下的CMakeDownloadLog.txt,上面写着:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use_cache "E:/opencv-3.4.0/opencv-3.4.0/.cache"
do_copy "opencv_ffmpeg.dll" "3ae76b105113d944984b2351c61e21c6" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/66b1fed06cf3510235f367f96aa26da5cb234a15/ffmpeg/opencv_ffmpeg.dll" "E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ffmpeg"
#missing "E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ffmpeg/opencv_ffmpeg.dll"
#check_md5 "E:/opencv-3.4.0/opencv-3.4.0/.cache/ffmpeg/3ae76b105113d944984b2351c61e21c6-opencv_ffmpeg.dll"
#mismatch_md5 "E:/opencv-3.4.0/opencv-3.4.0/.cache/ffmpeg/3ae76b105113d944984b2351c61e21c6-opencv_ffmpeg.dll" "299b46e2c4cf8047b0ef420c9a61c6ef"
#delete "E:/opencv-3.4.0/opencv-3.4.0/.cache/ffmpeg/3ae76b105113d944984b2351c61e21c6-opencv_ffmpeg.dll"
#cmake_download "E:/opencv-3.4.0/opencv-3.4.0/.cache/ffmpeg/3ae76b105113d944984b2351c61e21c6-opencv_ffmpeg.dll" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/66b1fed06cf3510235f367f96aa26da5cb234a15/ffmpeg/opencv_ffmpeg.dll"
do_copy "opencv_ffmpeg_64.dll" "cf3bb5bc9d393b022ea7a42eb63e794d" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/66b1fed06cf3510235f367f96aa26da5cb234a15/ffmpeg/opencv_ffmpeg_64.dll" "E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ffmpeg"
#missing "E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ffmpeg/opencv_ffmpeg_64.dll"
#check_md5 "E:/opencv-3.4.0/opencv-3.4.0/.cache/ffmpeg/cf3bb5bc9d393b022ea7a42eb63e794d-opencv_ffmpeg_64.dll"
#mismatch_md5 "E:/opencv-3.4.0/opencv-3.4.0/.cache/ffmpeg/cf3bb5bc9d393b022ea7a42eb63e794d-opencv_ffmpeg_64.dll" "3ab964d23e60d2b961737a8bb556bd45"
#delete "E:/opencv-3.4.0/opencv-3.4.0/.cache/ffmpeg/cf3bb5bc9d393b022ea7a42eb63e794d-opencv_ffmpeg_64.dll"
#cmake_download "E:/opencv-3.4.0/opencv-3.4.0/.cache/ffmpeg/cf3bb5bc9d393b022ea7a42eb63e794d-opencv_ffmpeg_64.dll" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/66b1fed06cf3510235f367f96aa26da5cb234a15/ffmpeg/opencv_ffmpeg_64.dll"
do_copy "ffmpeg_version.cmake" "ec59008da403fb18ab3c1ed66aed583b" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/66b1fed06cf3510235f367f96aa26da5cb234a15/ffmpeg/ffmpeg_version.cmake" "E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ffmpeg"
#check_md5 "E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ffmpeg/ffmpeg_version.cmake"
#match_md5 "E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ffmpeg/ffmpeg_version.cmake" "ec59008da403fb18ab3c1ed66aed583b"
do_unpack "ippicv_2017u3_win_ia32_general_20170822.zip" "8a7680ae352c192de2e2e34936164bd0" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/dfe3162c237af211e98b8960018b564bc209261d/ippicv/ippicv_2017u3_win_ia32_general_20170822.zip" "E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ippicv"
#check_md5 "E:/opencv-3.4.0/opencv-3.4.0/.cache/ippicv/8a7680ae352c192de2e2e34936164bd0-ippicv_2017u3_win_ia32_general_20170822.zip"
#mismatch_md5 "E:/opencv-3.4.0/opencv-3.4.0/.cache/ippicv/8a7680ae352c192de2e2e34936164bd0-ippicv_2017u3_win_ia32_general_20170822.zip" "c7fcfb626b50daa8bd728b7598328455"
#delete "E:/opencv-3.4.0/opencv-3.4.0/.cache/ippicv/8a7680ae352c192de2e2e34936164bd0-ippicv_2017u3_win_ia32_general_20170822.zip"
#cmake_download "E:/opencv-3.4.0/opencv-3.4.0/.cache/ippicv/8a7680ae352c192de2e2e34936164bd0-ippicv_2017u3_win_ia32_general_20170822.zip" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/dfe3162c237af211e98b8960018b564bc209261d/ippicv/ippicv_2017u3_win_ia32_general_20170822.zip"

可以看到文本中记录了下载链接,于是在浏览器中复制上述链接,自行下载好这几个文件到文本指向的路径中。
比如:我缺的三个文件是:
opencv_ffmpeg.dll,需放到:E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ffmpeg。
下载链接:https://raw.githubusercontent.com/opencv/opencv_3rdparty/66b1fed06cf3510235f367f96aa26da5cb234a15/ffmpeg/opencv_ffmpeg.dll
opencv_ffmpeg_64.dll,需放到:E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ffmpeg。
下载链接:
https://raw.githubusercontent.com/opencv/opencv_3rdparty/66b1fed06cf3510235f367f96aa26da5cb234a15/ffmpeg/opencv_ffmpeg_64.dll
ippicv_2017u3_win_ia32_general_20170822.zip,注意:需在压缩包前加上文件的hash值,即压缩名变为hash值-原名。提供一个在线查看哈希值的网站:http://www.atool.org/file_hash.php ,需要的是MD5 Hash。
需放到:E:/opencv-3.4.0/opencv-3.4.0/build/3rdparty/ippicv。(然而并没有生成ippicv文件夹)
下载链接:
https://raw.githubusercontent.com/opencv/opencv_3rdparty/dfe3162c237af211e98b8960018b564bc209261d/ippicv/ippicv_2017u3_win_ia32_general_20170822.zip
建议科学上网后下载,不然网速感人。
不过还是不匹配。

根据大佬提示,CMake3.10.2没有downloader.cmake,干脆……不要这三个文件了……
修改E:\opencv-3.4.0\opencv-3.4.0\3rdparty\ffmpeg中的ffmpeg.cmake,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Binary branch name: ffmpeg/master_20171009
# Binaries were created for OpenCV: 8ac2c5d620b467d3f22802e96c88ddde6da707af
set(FFMPEG_BINARIES_COMMIT "66b1fed06cf3510235f367f96aa26da5cb234a15")
set(FFMPEG_FILE_HASH_BIN32 "3ae76b105113d944984b2351c61e21c6")
set(FFMPEG_FILE_HASH_BIN64 "cf3bb5bc9d393b022ea7a42eb63e794d")
set(FFMPEG_FILE_HASH_CMAKE "ec59008da403fb18ab3c1ed66aed583b")

function(download_win_ffmpeg script_var)
set(${script_var} "" PARENT_SCOPE)

# set(ids BIN32 BIN64 CMAKE)
# set(name_BIN32 "opencv_ffmpeg.dll")
# set(name_BIN64 "opencv_ffmpeg_64.dll")
# set(name_CMAKE "ffmpeg_version.cmake")

# set(FFMPEG_DOWNLOAD_DIR "${OpenCV_BINARY_DIR}/3rdparty/ffmpeg")

# set(status TRUE)
# foreach(id ${ids})
# ocv_download(FILENAME ${name_${id}}
# HASH ${FFMPEG_FILE_HASH_${id}}
# URL
# "$ENV{OPENCV_FFMPEG_URL}"
# "${OPENCV_FFMPEG_URL}"
# "https://raw.githubusercontent.com/opencv/opencv_3rdparty/${FFMPEG_BINARIES_COMMIT}/ffmpeg/"
# DESTINATION_DIR ${FFMPEG_DOWNLOAD_DIR}
# ID FFMPEG
# RELATIVE_URL
# STATUS res)
# if(NOT res)
# set(status FALSE)
# endif()
# endforeach()
# if(status)
# set(${script_var} "${FFMPEG_DOWNLOAD_DIR}/ffmpeg_version.cmake" PARENT_SCOPE)
# endif()
endfunction()

修改E:\opencv-3.4.0\opencv-3.4.0\3rdparty\ippicv中的ippicv.cmake,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function(download_ippicv root_var)
# set(${root_var} "" PARENT_SCOPE)

# # Commit SHA in the opencv_3rdparty repo
# set(IPPICV_COMMIT "dfe3162c237af211e98b8960018b564bc209261d")
# # Define actual ICV versions
# if(APPLE)
# set(OPENCV_ICV_PLATFORM "macosx")
# set(OPENCV_ICV_PACKAGE_SUBDIR "ippicv_mac")
# if(X86_64)
# set(OPENCV_ICV_NAME "ippicv_2017u3_mac_intel64_general_20170822.tgz")
# set(OPENCV_ICV_HASH "c1ebb5dfa5b7f54b0c44e1917805a463")
# else()
# set(OPENCV_ICV_NAME "ippicv_2017u3_mac_ia32_general_20170822.tgz")
# set(OPENCV_ICV_HASH "49b05a669042753ae75895a445ebd612")
# endif()
# elseif((UNIX AND NOT ANDROID) OR (UNIX AND ANDROID_ABI MATCHES "x86"))
# set(OPENCV_ICV_PLATFORM "linux")
# set(OPENCV_ICV_PACKAGE_SUBDIR "ippicv_lnx")
# if(X86_64)
# set(OPENCV_ICV_NAME "ippicv_2017u3_lnx_intel64_general_20170822.tgz")
# set(OPENCV_ICV_HASH "4e0352ce96473837b1d671ce87f17359")
# else()
# set(OPENCV_ICV_NAME "ippicv_2017u3_lnx_ia32_general_20170822.tgz")
# set(OPENCV_ICV_HASH "dcdb0ba4b123f240596db1840cd59a76")
# endif()
# elseif(WIN32 AND NOT ARM)
# set(OPENCV_ICV_PLATFORM "windows")
# set(OPENCV_ICV_PACKAGE_SUBDIR "ippicv_win")
# if(X86_64)
# set(OPENCV_ICV_NAME "ippicv_2017u3_win_intel64_general_20170822.zip")
# set(OPENCV_ICV_HASH "0421e642bc7ad741a2236d3ec4190bdd")
# else()
# set(OPENCV_ICV_NAME "ippicv_2017u3_win_ia32_general_20170822.zip")
# set(OPENCV_ICV_HASH "8a7680ae352c192de2e2e34936164bd0")
# endif()
# else()
# return()
# endif()

# set(THE_ROOT "${OpenCV_BINARY_DIR}/3rdparty/ippicv")
# ocv_download(FILENAME ${OPENCV_ICV_NAME}
# HASH ${OPENCV_ICV_HASH}
# URL
# "${OPENCV_IPPICV_URL}"
# "$ENV{OPENCV_IPPICV_URL}"
# "https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
# DESTINATION_DIR "${THE_ROOT}"
# ID IPPICV
# STATUS res
# UNPACK RELATIVE_URL)

# if(res)
# set(${root_var} "${THE_ROOT}/${OPENCV_ICV_PACKAGE_SUBDIR}" PARENT_SCOPE)
# endif()
endfunction()

再重新Configure两次,Generate一次,成功。

用VS2013编译

打开OpenCV.sln编译,debug-Win32调试器。

添加到所需工程

根据法一:
添加环境变量:
E:\opencv-3.4.0\opencv-3.4.0\build\install\x86\vc12\bin
VS包含目录:E:\opencv-3.4.0\opencv-3.4.0\build\install\include
E:\opencv-3.4.0\opencv-3.4.0\build\install\include\opencv
E:\opencv-3.4.0\opencv-3.4.0\build\install\include\opencv2
库目录:
E:\opencv-3.4.0\opencv-3.4.0\build\install\x86\vc12\lib
链接器:
opencv_calib3d340d.lib
opencv_core340d.lib
opencv_dnn340d.lib
opencv_features2d340d.lib
opencv_flann340d.lib
opencv_highgui340d.lib
opencv_imgcodecs340d.lib
opencv_imgproc340d.lib
opencv_ml340d.lib
opencv_objdetect340d.lib
opencv_photo340d.lib
opencv_shape340d.lib
opencv_stitching340d.lib
opencv_superres340d.lib
opencv_video340d.lib
opencv_videoio340d.lib
opencv_videostab340d.lib

测试

比较两张图片的不同之处。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cv.h>  
#include <highgui.h>
#include <stdio.h>

int main()
{
IplImage *pSrc = cvLoadImage("1.jpg", 1);
CvSize size = cvSize(600, 376);//区域大小
cvSetImageROI(pSrc, cvRect(0, 0, size.width, size.height));//设置源图像ROI
IplImage* pDest1 = cvCreateImage(size, pSrc->depth, pSrc->nChannels);//创建目标图像
cvCopy(pSrc, pDest1); //复制图像
cvResetImageROI(pDest1);//源图像用完后,清空ROI
cvSaveImage("Image_1.jpg", pDest1);//保存目标图像
cvSetImageROI(pSrc, cvRect(0, 393, size.width, size.height));//设置源图像ROI
IplImage* pDest2 = cvCreateImage(size, pSrc->depth, pSrc->nChannels);//创建目标图像
cvCopy(pSrc, pDest2); //复制图像
cvResetImageROI(pDest2);//源图像用完后,清空ROI
cvSaveImage("Image_2.jpg", pDest2);//保存目标图像
IplImage *dst;
dst = cvCreateImage(cvGetSize(pDest1), pDest1->depth, pDest1->nChannels);
cvAbsDiff(pDest1, pDest2, dst);
cvNamedWindow("Compare_Images", 1);
cvShowImage("Compare_Images", dst);
cvWaitKey();
return 0;

}

以上程序转自http://blog.csdn.net/cooelf/article/details/24663097

可成功编译和运行,到此,基础环境算是磕磕绊绊搭建好了。

评论