ubuntu opencv3.4.15 安装

第一步安装必要运行库

 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

第二步下载opencv

wget https://github.com/opencv/opencv/archive/3.4.15.zip
unzip 3.4.15.zip

第三步下载ippicv(如果能正常cmake下载ippicv,就忽略这步)
https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20191018/ippicv
根据这个地址找到您需要版本

image.png

wget https://github.com/opencv/opencv_3rdparty/blob/ippicv/master_20191018/ippicv/ippicv_2020_lnx_intel64_20191018_general.tgz
cd opencv-3.4.15/3rdparty/ippicv/
sudo vi ippicv.cmake 

把地址
“https://github.com/opencv/opencv_3rdparty/blob/ippicv/master_20191018/ippicv/ippicv_2020_lnx_intel64_20191018_general.tgz”
替换成: "file:/你的路径/ippicv_2020_lnx_intel64_20191018_general.tgz"

第四步cmake

cd ~/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

sudo make
sudo make install

第五步配置

sudo vi /etc/ld.so.conf.d/opencv.conf 
//打开后可能是空文件,在文件内容最后添加
/usr/local/lib

第六步更新系统库、配置bash,执行如下命令

sudo ldconfig
sudo vi /etc/bash.bashrc  
//在末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH
source /etc/bash.bashrc
//激活配置然后更新database
sudo updatedb  
//验证安装
pkg-config opencv --modversion 

测试
转到 opencv-3.4.15/sample/cpp/example_cmake 目录下,打开终端

运行

cmake .
make
./opencv_example

如果摄像头打开,左上角出现 Hello OpenCV 则证明安装成功


测试程序编译出现

undefined reference to `cv::imread(cv::String const&, int)'

请使用下面命令行编译

g++ -g main.cpp `pkg-config --libs opencv`

如果出现

 error while loading shared libraries: libopencv_highgui.so.3.4: cannot open shared object file: No such file or directory
locate libopencv_highgui.so.3.4
// 找到正确位置:
//  /www/src/opencv-3.4.15/build/lib/libopencv_highgui.so.3.4
//  /www/src/opencv-3.4.15/build/lib/libopencv_highgui.so.3.4.15
cp /www/src/opencv-3.4.15/build/lib/libopencv_highgui.so.3.4 /usr/local/lib/libopencv_highgui.so.3.4
cp /www/src/opencv-3.4.15/build/lib/libopencv_highgui.so.3.4.15 /usr/local/lib/libopencv_highgui.so.3.4.15
sudo ldconfig //更新一下就可以

-------------------下面是opencv4.5.3 问题-----------

------如果4.5.3需要增加参数

//如果4.5.3需要增加参数
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES  -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.3/modules  ..
命令说明:

-D OPENCV_GENERATE_PKGCONFIG=YES:OpenCV4以上版本默认不使用pkg-config,该编译选项开启生成opencv4.pc文件,支持pkg-config功能。

opencv4.pc 位置:/usr/local/lib/pkgconfig/opencv4.pc

如果安装4.5.3出现

root@iZ94c0eouw5Z:/www/src/opencv-4.5.3/build# pkg-config opencv --modversion 
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found

执行拷贝文件到系统位置:
cp /usr/local/lib/pkgconfig/opencv4.pc /usr/lib/pkgconfig/opencv.pc

默认安装的是opencv4,采用cmake编译的时候,cmake也能找到opencv4,但是编译时候报错:

fatal error: opencv2/opencv.hpp: No such file or directory

include

解决方案

这是因为opencv头文件的路径中多了一个opencv4的文件夹:/usr/include/opencv4/opencv2,

可以将opencv2拷贝到自己的include文件夹
可以将opencv2文件夹链接到include文件夹下

sudo ln -s  /usr/local/include/opencv4/opencv2/ /usr/include/

opencv4.5.3
编译命令指明C++11: -std=c++11

g++ `pkg-config --cflags opencv4` main.cpp `pkg-config --libs opencv4` -o main.io -std=c++11

你可能感兴趣的:(ubuntu opencv3.4.15 安装)