1 ubuntu
1.1 准备文件
- opencv-3.4.7.zip
- ippicv_2019_lnx_intel64_general_20180723.tgz
从这里下载,提取码:gfcd
1.2 打开终端,安装以下软件包
sudo apt install ffmpeg
sudo apt install build-essential
sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
1.3 解压
- 把上面准备的文件都存放在 ~/dowload/ 目录下,解压 unzip opencv-3.4.7.zip,即 ~/dowload/opencv-3.4.7;
- 进入 ~/dowload/opencv-3.4.7/ 文件夹,创建目录 build;
1.4 用 cmake 编译
- 修改 3rdparty/ippicv/ippicv.cmake 文件,
- 将第47行:
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/a62e20676a60ee0ad6581e217fe7e4bada3b95db/ippicv/"
改为:"file://~/dowload/"
(这是 ippicv_2019_lnx_intel64_general_20180723.tgz 文件存放的地址) - 如果没有安装cmake先安装:
# 安装命令: sudo apt-get install cmake-qt-gui # 启动命名: cmake-gui
- 打开 CMake 软件,需要填写的两个路径:源码路径、编译存放的路径
~/dowload/opencv-3.4.7/
~/dowload/opencv-3.4.7/build - 点击 configure;
- 选择编译选项;
- 点击 Generate;
1.5 安装
cd ~/dowload/opencv-3.4.7/build
make
sudo make install
1.6 测试
执行:pkg-config --cflags --libs opencv
出现下面的信息表示安装成功:
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_ml -lopencv_superres -lopencv_dnn -lopencv_videostab -lopencv_highgui -lopencv_stitching -lopencv_shape -lopencv_photo -lopencv_video -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
最好还要用opencv自带的测试程序测试一下运行情况,进入目录:.samples/cpp/example_cmake/
,执行一下命令:
~: cmake
~: make
~: ./opencv_example
运行可能会出现以下问题:
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow, file /home/nick/.Apps/opencv/modules/highgui/src/window.cpp, line 516
terminate called after throwing an instance of 'cv::Exception'
what(): /home/nick/.Apps/opencv/modules/highgui/src/window.cpp:516: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvNamedWindow
报错提示安装 libgtk2.0-dev
和 pkg-config
,如果用命令 sudo apt install libgtk2.0-dev
安装提示依赖问题,就用 sudo aptitude install libgtk2.0-dev
命令安装,它会降级依赖的一些库,弹出的第一项一般是默认保持原版本,所以选择n
,之后弹出的如果是要安装的版本就选择y
;
1.7 扩展模块的编译安装
opencv的扩展模块有脸部识别、文本识别、边缘检测、追踪算法等,如果要加入扩展模块需要重新编译opencv,在编译之前,需要准备对应版本的 OpenCVcontrib 源文件;
下载地址:https://github.com/opencv/opencv_contrib/releases/ (选择和 opencv 一样版本的)
下载后解压,可以存放至 opencv 同级目录;
在 cmake 时将会下载几个 *.i 文件,但网络问题下载不了,所以先手动下载,下载文件的目录结构:
├── boostdesc
│ ├── 0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i
│ ├── 0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i
│ ├── 202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i
│ ├── 232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i
│ ├── 324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i
│ ├── 98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i
│ └── e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i
└── vgg
├── 151805e03568c9f490a5e3a872777b75-vgg_generated_120.i
├── 7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i
├── 7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i
└── e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i
将下载这些文件按照目录结构复制到 ~/dowload/opencv-3.4.7/.cache/xfeatures3d/ 里面,文件可以用下面的命令下载:
cd ./cache/xfeatures2d/
cd boostdesc
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i > 0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i > e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i > 98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i > 202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i > 324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i > 232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i > 0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i
cd ../vgg
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i > 151805e03568c9f490a5e3a872777b75-vgg_generated_120.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i > 7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i > e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i
curl https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i > 7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i
cmake 时还会下载一个 face_landmark_model.dat 文件,还是先手动下载至本地(存放路径随意,如:/home/usrname/install/):
下载地址:
https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat
然后修改opencv_contrib-3.4.0/modules/face/CMakeLists.txt文件,把下载路径改成上面的存放路径:
file:///home/usrname/install/"
#https://raw.githubusercontent.com/opencv/opencv_3rdparty/
# usrname记得替换为自己的用户名,路径记得替换为自己文件对应的路径
现在开始用 cmake 编译,
打开 CMake 软件,需要填写的两个路径:源码路径、编译存放的路径
~/dowload/opencv-3.4.7/
~/dowload/opencv-3.4.7/build
点击 configure;
- 在 OPENCV_EXTRA_MODULES_PATH 选项中填扩展包源码路径,如:xxx/dowload/opencv_contrib-3.4.7/modules
- 勾上选项:OPENCV_ENABLE_NONFREE
点击 Generate;
最后进入/dowload/opencv-3.4.7/build ,执行:
make -j 8
sudo make install
2 windows
2.1 准备文件
- opencv-4.0.0.zip
- .cache.zip (这是 opencv-4.0.0 版本的)
从这里下载,提取码:2m73
2.2 解压
- 解压上面两个文件
- 在opencv-4.0.0 文件夹中新建空文件夹build
- 把解压出来的.cache文件夹拷贝到刚解压的opencv目录下
2.3 cmake配置
打开cmake的窗口,指定编译路径(前面那个是opencv源码路径,后面那个是里面的build路径),然后分别点击三个按钮 :
Configure
(会弹出窗口选择编译工程的平台,其中第二栏是选择编译 win32 或 x64 的)Generate
Open Project
最后会启动VS进行编译;
2.4 编译
- 在VS中,选择Debug、x64
- 右键点击 ALL_BUILD(CMakeTargets目录下),点击重新生成解决方案;
- 右键点击 INSTALL(CMakeTargets目录下),选择 Project Only -> Build Only INSTALL;
- (生成的库就会存放在
./build/install/ 中,
include是头文件包含目录,
./build/install/x64/vc14/ 是相关的库;) - 把
./build/install/x64/vc14/bin
文件夹的全路径添加到环境变量中;