Ubuntu 16.04 Python3.5.2 下安装 OpenCV 3.4.1
1. 安装工具和依赖项
首先升级系统包和软件库
sudo apt update
sudo apt upgrade
安装一些工具
sudo apt install build-essential cmake pkg-config git wget
安装numpy模块
sudo apt install python3-pip
pip3 install numpy
安装依赖项
sudo apt install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt install libxvidcore-dev libx264-dev
sudo apt install libgtk-3-dev
sudo apt install libatlas-base-dev gfortran
sudo apt install python3.5-dev
2 安装OpenCV 3.4.3
下载解压源码包
wget https://github.com/opencv/opencv/archive/3.4.3.zip
unzip opencv-3.4.3.zip
用cmake配置属性
cd opencv-3.4.3
mkdir -p build
cd build
开始配置
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_EXECUTABLE=/usr/bin/python3 -D PYTHON_INCLUDE_DIR=/usr/include/python3.5 -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON ../
该步成功,之后输入
make -j8
sudo make install
至此在python3下执行
import cv2
不会报错,则安装成功
3 C++调用OpenCV接口
新建test.cpp
gedit test.cpp
#include <iostream>#include <opencv2/opencv.hpp>using namespace std;using namespace cv;int main() { Mat img = imread("test.jpg"); // 最好使用绝对路径 imshow("test",img); waitKey(5000); return 0; }
在控制台下编译,或者写入Makefile
g++ test.cpp -o test `pkg-config opencv --libs`
使用如下代码运行
./test
4 Qt Creator 调用OpenCV接口
新建控制台工程test
在test.pro中加入
INCLUDEPATH += /usr/local/include \ /usr/local/include/opencv \ /usr/local/include/opencv2 LIBS += /usr/local/lib/libopencv_highgui.so \ /usr/local/lib/libopencv_core.so \ /usr/local/lib/libopencv_imgproc.so
使用的main.cpp代码如下:
#include <QCoreApplication>#include <iostream>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>using namespace cv;using namespace std;int main(void){ namedWindow( "test", WINDOW_AUTOSIZE ); Mat img = imread("test.jpg", 1); // 务必使用绝对路径,否则可能报错 imshow("test", img); waitKey(5000); return 0; }
作者:我还在这里
链接:https://www.jianshu.com/p/80f74f47f987
共同学习,写下你的评论
评论加载中...
作者其他优质文章