如果你之前安装过OpenCV,必须卸干净:
sudo make uninstall #
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
接下来:
sudo apt-get --purge remove opencv-doc opencv-data python-opencv
可以百度云盘搜索,也可以用git clone的方法:
#获取opencv源码
git clone git@github.com:opencv/opencv.git
#获取opencv_contrib源码,根据自己需要,可以不安装
git clone git@github.com:opencv/opencv_contrib.git
sudo apt-get install build-essential libgtk2.0-dev libvtk7-dev libjpeg-dev libtiff5-dev libjasper-dev libopenexr-dev libtbb-dev
以opencv-4.1.0为例,将下载后的解压,按以下命令操作:
unzip opencv-4.1.0.zip
cd opencv-4.1.0
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DOPENCV_GENERATE_PKGCONFIG=ON -DCMAKE_INSTALL_PREFIX=/usr/local/opencv .. #设置安装目录,注意后面的两个点不要丢,否则找不到cmakelist文件
make # 编译 这一步会花一点时间,大概20分钟到一个小时,如果你的cpu性能好,可以用 make -j8
更全的安装命令:
cmake -D CMAKE_BUILD_TYPE=RELEASE -DOPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_GTK=ON -D WITH_OPENGL=ON -D BUILD_TIFF=ON -D WITH_IPP=ON -D BUILD_EXAMPLES=ON -D WITH_FFMPEG=OFF ..
解决tiff问题:
-D BUILD_TIFF=ON
解决lippicv问题:
-D WITH_IPP=ON
解决“video stream stopped ”问题:
-D WITH_FFMPEG=OFF
注意需要先安装ffmpeg,安装教程:https://blog.csdn.net/sunmingyang1987/article/details/125128525,然后-D WITH_FFMPEG=OFF,如果设置为ON,编译时汇报下面类似错误:
error: ‘CODEC_ID_H264’ was not declared in this scope; did you mean ‘AV_CODEC_ID_H264’?
输入以下命令:
make -j12
sudo make install
至此,安装工作已经完了,接下来进行配置,否则检测不到opencv。
输入以下命令:
cd /etc/ld.so.conf.d/
sudo touch opencv.conf
# 根据安装位置
sudo sh -c 'echo "/usr/local/opencv/lib" > opencv.conf' //注意自己的安装位置
sudo ldconfig
然后,复制.pc文件:
sudo cp -f /usr/local/opencv/lib/pkgconfig/opencv4.pc /usr/lib/pkgconfig/opencv4.pc
测试一下:
pkg-config --modversion opencv4
如果以前安装失败,最好将之前安装残留的文件完全清除,先进入之前opencv的build文件夹下,然后输入以下命令:
sudo make uninstall
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
sudo apt-get --purge remove opencv-doc opencv-data python-opencv
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
安装其他软件要用到OpenCV时,对于OpenCV3,在CMakelist.txt中:
FIND_PACKAGE(OpenCV REQUIRED)
如果用到opencv4,在上面命令前面添加opencv4路径即可:
set(CMAKE_PREFIX_PATH "/usr/local/opencv")
原因使用了其它的tiff编译版本库,cmake的时候加上:
-D BUILD_TIFF=ON -D ENABLE_PRECOMPILED_HEADERS=OFF
not found (try using -rpath or -rpath-link)
【1】在Ubuntu16.04下安装OpenCV环境,详细执行步骤和测试案例,良心填坑帖
【2】linux环境下编译安装opencv
【3】ubuntu安装opencv 解决ippicv下载问题:ippicv_2019_lnx_intel64_general_20180723.tgz