Ubuntu16桌面版编译和安装OpenCV4

2022-01-17  本文已影响0人  程序员欣宸

欢迎访问我的GitHub

https://github.com/zq2599/blog_demos

内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;

本篇概览

  1. 安装必要软件,如cmake
  2. 下载OpenCV源码,包括opencv和opencv_contrib,并且解压、摆好位置
  3. 运行cmake-gui,在图形化页面上配置编译项
  4. 编译、安装
  5. 配置环境
  6. 验证

环境

  1. 操作系统:Ubuntu16.04桌面版
  2. OpenCV:4.1.1

换源

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bk

-修改/etc/apt/sources.list为以下内容:

deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse
sudo apt-get update

安装应用

sudo apt-get install -y unzip build-essential curl cmake cmake-gui git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

下载源码

curl -fL -o opencv-4.1.1.zip https://codeload.github.com/opencv/opencv/zip/4.1.1; \
unzip opencv-4.1.1.zip; \
rm -rf opencv-4.1.1.zip; \
curl -fL -o opencv_contrib-4.1.1.zip https://codeload.github.com/opencv/opencv_contrib/zip/refs/tags/4.1.1; \
unzip opencv_contrib-4.1.1.zip; \
rm -rf opencv_contrib-4.1.1.zip; \
mv opencv_contrib-4.1.1 opencv_contrib; \
mv opencv_contrib opencv-4.1.1/; \
mkdir opencv-4.1.1/build

用cmake-gui配置

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

编译

make -j8
在这里插入图片描述

系统配置

sudo vi /etc/ld.so.conf.d/opencv.conf
/usr/local/lib
sudo ldconfig
sudo vi /etc/bash.bashrc
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH
will@hp:~$ pkg-config --modversion opencv4
4.1.1

验证

在这里插入图片描述
cmake_minimum_required(VERSION 3.20)
project(helloworld)

set(CMAKE_CXX_STANDARD 14)

find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(helloworld main.cpp)
target_link_libraries(helloworld ${OpenCV_LIBS})
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main() {
    Mat mat = imread("/home/will/temp/202110/30/111.png");

    if(!mat.data) {
        cout<<"Image not exists!";
        return -1;
    }

    namedWindow("src", WINDOW_AUTOSIZE);
    imshow("[src]", mat);

    waitKey(0);
    return 0;
}
在这里插入图片描述

你不孤单,欣宸原创一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 数据库+中间件系列
  6. DevOps系列

欢迎关注公众号:程序员欣宸

微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos

上一篇 下一篇

猜你喜欢

热点阅读