当前位置:   article > 正文

Ubuntu20.04.2.0下安装opencv4.5.2+opencv_contrib_ubuntu20.04安装opencv

ubuntu20.04安装opencv

1.进入系统下载opencv+opencv_contrib

下载地址:Releases - OpenCV

选择sources下载相应版本

解压文件,一版放到home/(用户名)/下

GitHub - opencv/opencv_contrib: Repository for OpenCV's extra modules:opencv_contrib

解压放在opencv文件夹

2.安装Ubuntu下的opencv需要的依赖项

 安装最新的CMake,输入命令

  1. sudo apt-get update
  2. sudo apt-get upgrade
  3. sudo apt install cmake

 安装opencv依赖项

sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config

  1. sudo apt-get install libv4l-dev
  2. sudo apt-get install v4l2ucp
  3. sudo apt-get install v4l-utils

3.编译和安装opencv

进入到opencv文件夹,输入命令

  1. mkdir build
  2. cd build

编译,安装

  1. cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D WITH_LIBV4L=ON ..
  2. sudo make -j4
  3. sudo make install

4.环境配置

打开/etc/ld.so.conf

sudo gedit /etc/ld.so.conf

在文件中加上一行

include /usr/local/lib
 sudo ldconfig

打开 /etc/bash.bashrc 文件

sudo gedit /etc/bash.bashrc 

末尾加上

  1. PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
  2. export PKG_CONFIG_PATH

再输入命令

source /etc/bash.bashrc

5.检验

输入命令

pkg-config opencv --modversion

如果最后一步报错为:
Package opencv was not found in the pkg-config search path.

  1. cd /usr/local/lib
  2. sudo mkdir pkgconfig
  3. cd pkgconfig
  4. sudo touch opencv.pc
  5. sudo vim opencv.pc

如果出现

sudo: vim:找不到命令

输入下命令安装vim

    sudo apt install vim

 vim模式下 a 插入代码   esc 退出到上一层  :wq  保存退出

首先建一个新的文件夹:pkgconfig
然后cd到这个文件夹下,新建opencv.pc文件;
再打开opencv.pc文件,文件内输入

  1. prefix=/usr/local
  2. exec_prefix=${prefix}
  3. includedir=${prefix}/include
  4. libdir=${exec_prefix}/lib
  5. Name: opencv
  6. Description: The opencv library
  7. Version:4.5.2
  8. Cflags: -I${includedir}/opencv4
  9. Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

其中version是你的opencv版本。
最后:wq保存退出。

转到 opencv-4.5.2/sample/cpp/example_cmake 目录下,打开终端

  1. cmake .
  2. make
  3. ./opencv_example

如果摄像头打开,左上角出现 Hello OpenCV 则证明安装成功

ubuntu下Qt配置opencv

1.新建一个mainwindow项目,在.pro项目文件中添加

  1. INCLUDEPATH += /usr/local/include/opencv4/opencv2 \
  2. /usr/local/include/opencv4
  3. LIBS += /usr/local/lib/libopencv_*

2.mian.cpp文件改为

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. #include "opencv2/opencv.hpp"
  4. using namespace cv;
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication a(argc, argv);
  8. MainWindow w;
  9. w.show();
  10. Mat image = imread("/home/tuya/img_data/barbara.bmp");
  11. imshow("test",image);
  12. waitKey(0);
  13. return a.exec();
  14. }

编译运行,如果出现 “cannot open shared object file: No such file or directory”,这是因为编译器找不到安装后lib的路径,在终端输入命令

gedit /etc/ld.so.conf.d/opencv.conf

 创建一个opencv.conf文件,在文件中添加lib路径

/usr/local/lib

保存,再输入下命令,即可解决

sudo ldconfig -v

重新编译运行项目

ubuntu opencv卸载

在ubuntu终端命令行输入以下命令进入安装opencv的build目录并进行卸载操作。

sudo make uninstall

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/119880?site
推荐阅读
相关标签
  

闽ICP备14008679号