栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Ubuntu 16.04 Python3.5.2 下安装 OpenCV 3.4.1

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Ubuntu 16.04 Python3.5.2 下安装 OpenCV 3.4.1

Ubuntu 16.04 Python3.5.2 下安装 OpenCV 3.4.11. 安装工具和依赖项

首先升级系统包和软件库

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 #include 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 #include #include #include #include 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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/220728.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号