现在人工智能大热,如果想了解人工智能或者自诩为专家,那不得不学习opencv。OpenCV 是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可实现人工智能领域内很多核心的图像处理和计算机视觉方面的通用算法。值得自豪的是,目前全球OpenCV开发总部已经迁移到中国,里面也凝聚了不少国人的心血。

在Linux下安装opencv是一件很简单的事情,本以为在win10下也不复杂,结果还是碰到了一些坑,这跟微软的生态不无关系。先是下载免费的Visual Studio 2015 社区版,占用十几G的空间,结果还是浪费了不少时间才通过编译opencv,这是后话暂且不提。本文只是讲讲得如何在Win10下面,利用MINGW编译通过opencv-4.2.0版本。

MINGW是Windows下面的一个类Linux编译器,按照官网介绍,就是“MinGW,是Minimalist GNUfor Windows的缩写。它是一个可自由使用和自由发布的Windows特定头文件和使用GNU工具集导入库的集合,允许你在GNU/Linux和Windows平台生成本地的Windows程序而不需要第三方C运行时(C Runtime)库。”而且简单易操作。


先做些准备工作,这个步骤挺重要,否则会走很多弯路:

1.下载最新版的CMAKE,安装好后运行命令看版本:

> cmake --version

cmake version 3.15.5

CMake suite maintained and supported by Kitware (kitware.com/cmake).

2.测试opencl库,看GPU是否可用:

> clinfo

Number of platforms: 1

Platform Profile: FULL_PROFILE

Platform Version: OpenCL 2.1 AMD-APP (2841.5)

Platform Name: AMD Accelerated Parallel Processing

Platform Vendor: Advanced Micro Devices, Inc.

Platform Extensions: cl_khr_icd cl_khr_d3d10_sharing cl_khr_d3d11_sharing cl_khr_dx9_media_sharing cl_amd_event_callback cl_amd_offline_devices

Platform Name: AMD Accelerated Parallel Processing

...

Version: OpenCL 2.0 AMD-APP (2841.5)

...

3.下载安装MingW64,将MingW64安装到c:\mingw下,安装好后运行命令看版本:

> g --version

g (rev5, Built by MinGW-W64 project) 4.8.1

Copyright (C) 2013 Free Software Foundation, Inc.

This is free software; see the source for copying conditions. There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

> mingw32-make --version

GNU Make 3.99.90

Built for Windows32

Copyright (C) 1988-2012 Free Software Foundation, Inc.

License GPLv3 : GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

4. 添加CMAKE和MINGW的环境变量

5. 下载最新版的Opencv-4.2.0.zip,解压到c:\opencv-4.2.0目录下

6. 在c:\opencv-4.2.0目录下创建新目录c:\opencv-4.2.0\build-mingw, 用于编译


启动cmake-gui,出现图形界面,设置 c:\opencv-4.2.0 为source, c:\opencv-4.2.0\build-mingw 为binary。点击 configure, 选择“MingW makefile", 然后开始:


荣耀magicbook锐龙版装win7(华为MagicBook锐龙版双系统折腾记七)(1)


正常情况,cmake会开始配置编译环境:


荣耀magicbook锐龙版装win7(华为MagicBook锐龙版双系统折腾记七)(2)

第一次configure完成后,会出现红色选项,需要做取舍,增加 build example 和 build test 两个选项。如果网速慢导致出现ffmpeg(用来操作视频的一个著名开源库,国内几乎所有的视频软件背后都能看到它的影子)的两个动态库dll不能下载,出现错误,两个方法解决,一个是不选ffmpeg,二是用浏览器直接下载,再拷贝到 c:\opencv-4.2.0\build-mingw\3rdparty\ffmpeg中,下载网址是:

https://raw.githubusercontent.com/opencv/opencv_3rdparty/a66a24e9f410ae05da4baeeb8b451912664ce49c/ffmpeg/opencv_videoio_ffmpeg.dll

https://raw.githubusercontent.com/opencv/opencv_3rdparty/a66a24e9f410ae05da4baeeb8b451912664ce49c/ffmpeg/opencv_videoio_ffmpeg_64.dll

然后再次点击configure,如果有opencl,则选取opencl。另外如在编译过程中,不想要太多的warning信息,在C flags (Release)中增加两个参数: -Wno-unused-parameter -Wno-unknown-pragmas:


荣耀magicbook锐龙版装win7(华为MagicBook锐龙版双系统折腾记七)(3)

如果一切正常,则配置完成,然后点击generate,生成makefile编译文件

打开终端命令行cmd,输入下面命令:

>cd c:\opencv-4.2.0\build-mingw

>mingw32-make -j4

加”-j4“参数,是充分利用CPU的多核优势,加快编译速度。然后会正常编译:


荣耀magicbook锐龙版装win7(华为MagicBook锐龙版双系统折腾记七)(4)

耐心等待,编译完成后,然后输入命令安装,默认是安装到c:\opencv-4.2.0\build-mingw\install目录下,所有生成的动态库是放在C:\opencv-4.2.0\build-mingw\install\x64\mingw\bin下,链接库放在C:\opencv-4.2.0\build-mingw\install\x64\mingw\lib。将动态库的目录C:\opencv-4.2.0\build-mingw\install\x64\mingw\bin放到环境变量中,则大功告成。


测试一下。运行opencv_version_w32.exe,显示结果:

>opencv_version_win32.exe

General configuration for OpenCV 4.2.0 =====================================

Version control: unknown

Platform:

Timestamp: 2020-02-06T10:14:31Z

Host: Windows 10.0.18363 AMD64

CMake: 3.15.5

CMake generator: MinGW Makefiles

CMake build tool: C:/mingw/bin/mingw32-make.exe

Configuration: Release

CPU/HW features:

Baseline: SSE SSE2 SSE3

requested: SSE3

Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX

requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX

SSE4_1 (16 files): SSSE3 SSE4_1

SSE4_2 (2 files): SSSE3 SSE4_1 POPCNT SSE4_2

FP16 (1 files): SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX

AVX (5 files): SSSE3 SSE4_1 POPCNT SSE4_2 AVX

C/C :

Built as dynamic libs?: YES

C Compiler: C:/mingw/bin/g .exe (ver 4.8.1)

C flags (Release): -Wno-unused-parameter -Wno-unknown-pragmas -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wsign-promo -Wuninitialized -Winit-self -Wno-delete-non-virtual-dtor -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG

C flags (Debug): -Wno-unused-parameter -Wno-unknown-pragmas -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wsign-promo -Wuninitialized -Winit-self -Wno-delete-non-virtual-dtor -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG

C Compiler: C:/mingw/bin/gcc.exe

C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wuninitialized -Winit-self -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG

C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wuninitialized -Winit-self -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG

Linker flags (Release): -Wl,--gc-sections

Linker flags (Debug): -Wl,--gc-sections

ccache: NO

Precompiled headers: NO

Extra dependencies:

3rdparty dependencies:

OpenCV modules:

To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching ts video videoio

Disabled: world

Disabled by dependency: -

Unavailable: Java js python2

Applications: tests perf_tests examples 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-libjpeg-turbo (ver 2.0.2-62)

WEBP: build (ver encoder: 0x020e)

PNG: build (ver 1.6.37)

TIFF: build (ver 42 - 4.0.10)

JPEG 2000: build (ver 1.900.1)

OpenEXR: build (ver 2.3.0)

HDR: YES

SUNRASTER: YES

PXM: YES

PFM: YES

Video I/O:

FFMPEG: YES (prebuilt binaries)

avcodec: YES (58.54.100)

avformat: YES (58.29.100)

avutil: YES (56.31.100)

swscale: YES (5.5.100)

avresample: YES (4.0.0)

GStreamer: NO

DirectShow: YES

Parallel framework: none

Trace: YES (built-in)

Other third-party libraries:

Lapack: NO

Eigen: NO

Custom HAL: NO

Protobuf: build (3.5.1)

OpenCL: YES (no extra features)

include path: C:/opencv-4.2.0/3rdparty/include/opencl/1.2

Link libraries: Dynamic load

Python 3:

Interpreter: C:/Python37/python.exe (ver 3.7)

Libraries: C:/Python37/python37.dll (ver 3.7.0)

numpy: C:/Python37/lib/site-packages/numpy/core/include (ver 1.18.1)

install path: C:/Python37/Lib/site-packages/cv2/python-3.7

Python (for build): C:/Python37/python.exe

Java:

ant: NO

JNI: NO

Java wrappers: NO

Java tests: NO

Install to: C:/opencv-4.2.0/build-mingw/install

-----------------------------------------------------------------

OpenCL Platforms:

AMD Accelerated Parallel Processing

iGPU: gfx902 (OpenCL 2.0 AMD-APP (2841.5))

Current OpenCL device:

Type = iGPU

Name = gfx902

Version = OpenCL 2.0 AMD-APP (2841.5)

Driver version = 2841.5 (PAL,HSAIL)

Address bits = 64

Compute units = 8

Max work group size = 256

Local memory size = 32 KB

Max memory allocation size = 2 GB 54 MB 37 KB 614 B

Double support = Yes

Host unified memory = Yes

Device extensions:

cl_khr_fp64

cl_amd_fp64

cl_khr_global_int32_base_atomics

cl_khr_global_int32_extended_atomics

cl_khr_local_int32_base_atomics

cl_khr_local_int32_extended_atomics

cl_khr_int64_base_atomics

cl_khr_int64_extended_atomics

cl_khr_3d_image_writes

cl_khr_byte_addressable_store

cl_khr_fp16

cl_khr_gl_sharing

cl_khr_gl_depth_images

cl_amd_device_attribute_query

cl_amd_vec3

cl_amd_printf

cl_amd_media_ops

cl_amd_media_ops2

cl_amd_popcnt

cl_khr_d3d10_sharing

cl_khr_d3d11_sharing

cl_khr_dx9_media_sharing

cl_khr_image2d_from_buffer

cl_khr_spir

cl_khr_subgroups

cl_khr_gl_event

cl_khr_depth_images

cl_khr_mipmap_image

cl_khr_mipmap_image_writes

cl_amd_liquid_flash

cl_amd_planar_yuv

Has AMD Blas = No

Has AMD Fft = No

Preferred vector width char = 4

Preferred vector width short = 2

Preferred vector width int = 1

Preferred vector width long = 1

Preferred vector width float = 1

Preferred vector width double = 1

OpenCV's HW features list:

ID= 1 (MMX) -> ON

ID= 2 (SSE) -> ON

ID= 3 (SSE2) -> ON

ID= 4 (SSE3) -> ON

ID= 5 (SSSE3) -> ON

ID= 6 (SSE4.1) -> ON

ID= 7 (SSE4.2) -> ON

ID= 8 (POPCNT) -> ON

ID= 9 (FP16) -> ON

ID= 10 (AVX) -> ON

ID= 11 (AVX2) -> ON

ID= 12 (FMA3) -> ON

Total available: 12


编译出来的很多example都非常有意义,值得学习。最后运行example_cpp_drawing.exe以作纪念:


荣耀magicbook锐龙版装win7(华为MagicBook锐龙版双系统折腾记七)(5)


,