虚拟机ubuntu下qt中opencv配置

虚拟机ubuntu下qt中opencv配置

  • 1. 下载编译opencv
    • 1.1 测试demo.cpp
  • 2. 下载qt及添加opencv配置
    • 2.1 qt下载及路径添加
    • 2.2 opencv配置
    • 2.3 qtcreator中测试demo

1. 下载编译opencv

1.https://docs.opencv.org/master/df/d65/tutorial_table_of_content_introduction.html
2.进入Install in Linux
3.选择Build with opencv_contrib对应的linux命令,如下

// Install minimal prerequisites (Ubuntu 18.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip
// Download and unpack sources
wget -O opencv.zip https://github.com/opencv/opencv/archive/master.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/master.zip
unzip opencv.zip
unzip opencv_contrib.zip
// Create build directory and switch into it
mkdir -p build && cd build
// Configure
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules ../opencv-master
// Build
cmake --build .

1.1 测试demo.cpp

#include
 using namespace cv;
 int main()
 {
     // Mat src = imread("test.jpg");
     //imshow("Test",src);
	 //waitKey(0);
  	VideoCapture capture(0);
	while (true)
   	{
       	Mat frame;
      	capture >> frame;
      	imshow("读取视频",frame);
      	 waitKey(30);
     		}
 	return 0;
 }

CMakeLists.txt内容如下

cmake_minimum_required(VERSION 2.8)
project( demo )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( demo demo.cpp )
target_link_libraries( demo ${OpenCV_LIBS} )

虚拟机中,需要在设置中可移动设备里,加入摄像头。

2. 下载qt及添加opencv配置

2.1 qt下载及路径添加

#下载
sudo apt-get install qt5-default qtcreator
#配置路径
sudo vim /etc/bash.bashrc
#在文件中追加下面三行,添加路径
export QTDIR=/opt/Qt5.12.0/5.12.0/gcc_64
export PATH=$QTDIR/bin:$PATH
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH5_:
# 执行bash.bashrc
source /etc/bash.bashrc

2.2 opencv配置

//opencv配置
sudo vim /etc/ld.so.conf.d/opencv.conf 
//加入       /usr/local/lib
sudo ldconfig
//bash配置
sudo vim /etc/bash.bashrc 
//vim中添加如下两行
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig 
export PKG_CONFIG_PATH
//使配置生效
sudo ldconfig
//将#includ文件复制出来,便于包含
sudo cp /usr/local/include/opencv4/opencv2 /usr/local/include -r

2.3 qtcreator中测试demo

.pro项目文件设置中添加

INCLUDEPATH += /usr/local/include \
                /usr/local/include/opencv
                /usr/local/include/opencv2

LIBS += /usr/local/lib/libopencv_* \

测试main.cpp

#include 
#include

using namespace std;
using namespace cv;

int main()
{
    //cout << "Hello World!" << endl;
    VideoCapture capture (0);
        Mat src;
        while(1)
        {

            capture >> src;
            imshow("capture",src);
            char(key)=(char)waitKey(1);
            if(key==' ')
                break;
        }
    return 0;
}

你可能感兴趣的:(opencv,ubuntu,qt)