1. 下载opencv和opencv-contrib
https://github.com/opencv/opencv/archive/refs/tags/4.6.0.zip
https://github.com/opencv/opencv_contrib/archive/refs/tags/4.6.0.zip
2. 安装依赖
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libgtk-3-dev libgtkglext1-dev libgstreamer1.0-dev libavresample-dev libgphoto2-dev libopenblas-dev libatlas-base-dev doxygen libgstreamer-ocaml-dev libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-good1.0-dev libgstreamer1.0-dev libgstreamermm-1.0-dev libtesseract-dev liblept5 libleptonica-dev liblapacke-dev libgstreamer-opencv1.0-0 libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-good1.0-dev libgstreamerd-3-dev libgstreamermm-1.0-dev libv4l-dev libprotobuf-dev libhdf5-dev libgflags-dev python3-numpy
3. cmake
cd opencv-4.6.0/
mkdir release
cd release/
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv460 -D OPENCV_EXTRA_MODULES_PATH=/home/zxq/code/c++/opencv_contrib-4.6.0/modules ..
结果如下
异常情况如下:
a.编译过程中会下载IPPICV: Download: ippicv_2019_lnx_intel64_general_20180723.tgz,由于网络问题,经常下载失败
解决方法:
手动下载:https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20180723/ippicv
可放在默认的下载路径 ~/DownLoad/
修改opencv里相关配置文件
或sudo wget https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20180723/ippicv
#保存在一个路径里面,这个路径自定义即可。
vim ~/opencv/3rdparty/ippicv/ippicv.cmake #就是这个文件的路径
# 将47行的
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}ippicv/"
# 改为步骤1中手动下载的文件的本地路径(也就是将网络下载的模式改为本地文件下载的模式):
"file:~/Downloads/" #(仅供参考,根据自己的路径填写)
保存完后,重新cmake
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local …
b. opencv2/core/videoio.h ,opencv constants_c.h: 没有那个文件或目录
直接在代码中注释掉即可
4. make
make -j8
5. 安装
sudo make install
结果如下。
右上可知,opencv被安装在/usr/local/opencv460/
6. 配置opencv的路径
sudo gedit /etc/ld.so.conf.d/opencv.conf
添加内容:
/usr/local/opencv460/lib
再sudo一下使配置路径生效
sudo ldconfig
7. 测试
cd到opencv测试用例目录。
cd samples
cd cpp
cd example_cmake
cmake .
8.运行测试程序
make
./opencv_example
结果如下。
出现这个弹窗,说明安装成功。