必须安装的软件包
GCC 4.4.x or later
CMake 2.6 or higher
Git
GTK+2.x or higher, including headers (libgtk2.0-dev)
pkg-config
Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
[optional] libtbb2 libtbb-dev
[optional] libdc1394 2.x
[optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
在终端中使用以下命令进行安装:
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
第三句安装可能找不到 libjasper-dev 包,还有libtiff-dev提示可用libtiff5-dev代替,将命令改为
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev libdc1394-22-dev
下载opencv,这里以opencv3.2.0为例(其他版本可在https://github.com/opencv/opencv找到),在终端执行
git clone https://github.com/opencv/opencv.git
下载太慢请移步: https://pan.baidu.com/s/1sC4wyoWtTuZiWB35Uodnmg 提取码: srf6
然后进入目录,并新建一个目录用来编译
cd opencv
mkdir buildOpencv
cd buildOpencv
对opencv进行配置
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
由于网络问题,在配置过程中可能遇到ippicv_linux_20151201下载失败,
解决办法手动下载该包 ippicv_linux_20151201
然后替换下载的包opencv-3.2.0/3rdparty/ippicv/downloads/linux-*目录下的同名文件。然后重新cmake。
在buildOpencv目录下执行make
make
如果想发挥多核CPU的优势,使用make -j4 其中j4代表利用4核编译
可能在编译过程中出现如下错误
In file included from /usr/local/include/c++/6.2.0/bits/stl_algo.h:59:0,
from /usr/local/include/c++/6.2.0/algorithm:62,
from /home/dmelo/proj2/opencv/modules/core/include/opencv2/core/base.hpp:55,
from /home/dmelo/proj2/opencv/modules/core/include/opencv2/core.hpp:54,
from /home/dmelo/proj2/opencv/modules/highgui/include/opencv2/highgui.hpp:46,
from /home/dmelo/proj2/opencv/build/modules/highgui/precomp.hpp:45:
/usr/local/include/c++/6.2.0/cstdlib:75:25: fatal error: stdlib.h: No such file or directory
#include_next
^
compilation terminated.
这时需要重新配置,配置加入
-DENABLE_PRECOMPILED_HEADERS=OFF
后编译正常
cmake -D CMAKE_BUILD_TYPE=RELEASE -DENABLE_PRECOMPILED_HEADERS=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..
在终端执行下面命令,实际是将opencv库放到系统的库中
sudo make install
在安装结束后再做些后续工作
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
将下面内容粘贴进去.bashc文件中
ONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
打开.bashrc,并使之生效。
vim ~/.bashrc
source ~/.bashrc
查看opencv版本信息:
opencv_version
python:
python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
如果没报错则安装成功
C++:
建立一个cpp文件,vim opencvTest.cpp,然后将下面内容写进去。
#include
using namespace cv;
int main(){
Mat img =imread("./1.jpg");
imshow("image",img);
waitKey(0);
return 0;
}
找一张图片命名为1.jpg放在与opencvTest同目录下。然后在终端执行命令
g++ opencvTest.cpp -o opencvTest `pkg-config --cflags --libs opencv`
./opencvTest
卸载编译目录、安装目录、链接库
cd opencv-3.2.0/buildOpencv
sudo make uninstall
cd ..
sudo rm -r buildOpencv
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
从github上下载
git clone https://github.com/opencv/opencv.git
进入opencv目录并查看opencv版本tag
cd opencv
#查看tag
git tag
git reset --hard 3.3.0
参考:
https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation
https://blog.csdn.net/qq_42517195/article/details/80834655
https://blog.csdn.net/myboyliu2007/article/details/72760503
https://blog.csdn.net/Jonms/article/details/80592299