Ubuntu配置Opencv
Posted Q渡劫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu配置Opencv相关的知识,希望对你有一定的参考价值。
1、Cmake 的编译及安装
OPENCV的编译依赖Cmake,所以首先需要安装Cmake
通过命令行的方式安装(前提是需要Ubuntu联网)
ml@ml-virtual-machine:~$ sudo apt-get install cmake
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成
cmake 已经是最新版 (3.22.1-1ubuntu1.22.04.1)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 300 个软件包未被升级。
版本信息
ml@ml-virtual-machine:~$ cmake --version
cmake version 3.22.1
2、安装依赖库
2.1 安装 opencv 依赖库
执行命令
ml@ml-virtual-machine:~$ sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libtiff5-dev libswscale-dev pkg-config
【补充】:
显示图像 imshow(),正常显示几秒后报错:
Gtk-Message: 11:37:50.847: Failed to load module “canberra-gtk-module”
缺少库,执行以下命令安装:
ml@ml-virtual-machine:~$ sudo apt-get install libcanberra-gtk-module
2.2 安装 opencv_contrib 依赖库
执行命令
ml@ml-virtual-machine:~$sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev git
安装时若是没有产生报错,其他提示均可忽略
3 、Opencv 的编译及安装
3.1、下载 opencv并解压到 opencv-4.2.0目录
下载官网:https://github.com/opencv/opencv
3.2、进入opencv-4.2.0目录,进行如下操作
ml@ml-virtual-machine:~/opencv-4.2.0$ mkdir build && cd build
进入已经创建的文件目录build
3.3、配置编译选项
ml@ml-virtual-machine:~/opencv-4.2.0/build$ cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 ../
成功会产生如下信息
– Build files have been written to: /home/ml/opencv-4.2.0/build
3.4、编译
ml@ml-virtual-machine:~/opencv-4.2.0/build$ make
时间较长,需要耐心等待,直到进度条达到 100%
3.5、 安装
Ubuntu编译配置Opencv
一、
1.下载opencv源代码
2.安装编译依赖库
2.1 sudo apt-get update
2.2
sudo apt-get install cmake
sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config
sudo apt-get install build-essential
sudo apt-get install libavcodec-dev
sudo apt-get install libavformat-dev
sudo apt-get install libswscale-dev
3.进入到OpenCV的文件夹中,创建一个build目录,进行编译
3.1.cd opencv-3.4.10
3.2.mkdir build
3.3.cd build
3.4.cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
3.5.make
3.6.sudo make install
4.编译完成使用
4.1 一种是配置路径
1.opencv的头文件:/usr/local /include
2.lib 文件,在编译build文件下 :opencv-3.4.10/build/lib
二、
.pro添加opencv库文件
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += main.cpp mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target unix:!macx: LIBS += -L$$PWD/thirdpart/lib/ -lopencv_calib3d -lopencv_core -lopencv_dnn -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videoio -lopencv_videostab INCLUDEPATH += $$PWD/thirdpart/include DEPENDPATH += $$PWD/thirdpart/include
#include "mainwindow.h" #include <QApplication> #include "opencv2/opencv.hpp" int main(int argc, char *argv[]) { QApplication a(argc, argv); // MainWindow w; // w.show(); // read an image cv::Mat image = cv::imread("/home/likewei/Project/OpencvDemo/color3.jpg", 1); // create image window named "My Image" cv::namedWindow("My Image"); // show the image on window cv::imshow("My Image", image); return a.exec(); }
以上是关于Ubuntu配置Opencv的主要内容,如果未能解决你的问题,请参考以下文章