現在人工智能大熱,如果想了解人工智能或者自詡為專家,那不得不學習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>
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銳龍版雙系統折騰記七:win10+mingw+opencv](http://p2.ttnews.xyz/loading.gif)
正常情況,cmake會開始配置編譯環境:
![華為MagicBook銳龍版雙系統折騰記七:win10+mingw+opencv](http://p2.ttnews.xyz/loading.gif)
第一次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:
如果一切正常,則配置完成,然後點擊generate,生成makefile編譯文件
打開終端命令行cmd,輸入下面命令:
>cd c:\\opencv-4.2.0\\build-mingw
>mingw32-make -j4
加”-j4“參數,是充分利用CPU的多核優勢,加快編譯速度。然後會正常編譯:
耐心等待,編譯完成後,然後輸入命令安裝,默認是安裝到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以作紀念:
閱讀更多 hunnish 的文章