Ubuntu 18.04 完美安装Opencv 3.4.3
1.1 下载Opencv 3.4.3
在http://opencv.org/网址中找到下载连接,版本选择:https://github.com/opencv/opencv/releases
下载地址:https://github.com/Itseez/opencv/archive/3.4.3.zip (此处可以使用wget命令)
1.2 解压zip包
unzip opencv-3.4.3.zip
cd opencv-3.4.3
1.3 安装依赖库和cmake
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo vim /etc/apt/sources.list
deb http://us.archive.ubuntu.com/ubuntu/ bionic main restricted
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates main restricted
deb http://us.archive.ubuntu.com/ubuntu/ bionic universe
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates universe
deb http://us.archive.ubuntu.com/ubuntu/ bionic multiverse
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates multiverse
deb http://us.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu bionic-security main restricted
deb http://security.ubuntu.com/ubuntu bionic-security universe
deb http://security.ubuntu.com/ubuntu bionic-security multiverse
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt-get install cmake
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev
sudo apt-get install libgtk2.0-dev and pkg-config
sudo apt install libcanberra-gtk-module libcanberra-gtk3-module
mkdir my_build_dir
cd my_build_dir
2. 下载
ippicv_2019_lnx_intel64_general_20180723.tgz
3.修改 ../../opencv-3.4.3/3rdparty/ippicv/ippicv.cmake
将如下
"${OPENCV_IPPICV_URL}""$ENV{OPENCV_IPPICV_URL}""https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
中的
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
修改为ippicv安装包所在本地路径
"file:///home/student/SFDN/Downloads"
4. 编译opencv
cmake -D OPENCV_ENABLE_NONFREE=ON ..
1.5 执行make命令
sudo make // 时间比较漫长
1.6 执行install命令
sudo make install
这一步执行完毕之后,Opencv的编译过程就结束了,接下来的工作就是配置一些Opencv的编译环境。
2.1 将OpenCV的库添加到路径
首先将OpenCV的库添加到路径,从而可以让系统找到
sudo gedit /etc/ld.so.conf.d/opencv.conf
执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加
/usr/local/lib
2.2 生效配置文件
执行如下命令使得刚才的配置路径生效:
sudo ldconfig
2.3 配置bash
sudo gedit /etc/bash.bashrc
在末尾追加:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
保存,执行如下命令使得配置生效:
source /etc/bash.bashrc
更新:
sudo updatedb
至此,所有配置都已经完成。
3 官方程序测试
找到 opencv-3.4.3/samples/cpp/example_cmake 目录下,官方已经给出了一个cmake的example,我们可以拿来测试下。按顺序执行:
cmake .
make
./opencv_example
即可看到打开了摄像头,在左上角有一个hello opencv ,即表示配置成功。
opencv 经典demo测试测试,新建文件 main.cpp ,写入以下厚绒
#include <opencv2/opencv.hpp>
#include <iostream>using namespace cv;
int main(int argc, char** argv) {Mat src = imread("33.jpg");if (src.empty()) {printf("could not load image...\n");return -1;}namedWindow("test opencv setup", CV_WINDOW_AUTOSIZE);imshow("test opencv setup", src);waitKey(0);return 0;
}
编译 opencv c++ 代码
g++ main.cpp `pkg-config opencv --libs --cflags opencv` -o test2
执行程序
./test2
得到效果
更多多资讯或疑问内容请关注 微信公众号 “让梦飞起来” 或添加小编微信, 后台回复 “Python” ,领取更多资料哦