linux下在qt新建ui项目并导入apriltag项目计算二维码的深度

本文介绍如何在Linux环境下使用Qt创建UI项目,并将apriltag库集成进来,用于计算二维码的深度。重点讲述了将opencv_demo移植到Qt项目中以及调整cmakelists.txt的过程。经过努力,最终成功运行并展示了结果。
摘要由CSDN通过智能技术生成
    新建一个qt的ui项目。把github上下载的apriltag项目下所有的东西拷贝到你建的ui项目下。因为apriltag作者是用cmakelists编译的,所以这里的qt项目我们也用cmakelists。
    我新建的项目名是location,location.cpp代码如下:
#include "location.h"
#include "ui_location.h"

Location::Location(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Location)
{
   
    ui->setupUi(this);
    this->resize(800, 600);   //屏幕分辨率(1600,900)
        this->setWindowTitle("界面显示");

    videoLabel = new QLabel(this);                //显示处理后的视频
    videoLabel->setGeometry(150, 20, 640, 480);  //500, 10, 512, 384
    videoLabel->setFrameShadow(QFrame::Sunken);
    videoLabel->setFrameShape(QFrame::WinPanel);
}

Location::~Location()
{
   
    delete ui;
}

void Location::on_pushButton_clicked()   //open camera and detect
{
   
   opencv_demo();
}

apriltag的opencv_demo是在example里的,这里我们把它放到qt项目下,和location.cpp属于同一级。代码如下:

/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
All rights reserved.
This software was developed in the APRIL Robotics Lab under the
direction of Edwin Olson, ebolson@umich.edu. This software may be
available under alternative licensing terms; contact the address above.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the Regents of The University of Michigan.
*/

#include <iostream>
#include "location.h"
#include "opencv2/opencv.hpp"
#include <QDebug>
#include <QMessageBox>

extern "C" {
   
#include "apriltag.h"
#include "tag36h11.h"
#include "tag25h9.h"
#include "tag16h5.h"
#include "tagCircle21h7.h"
#include "tagCircle49h12.h"
#include "tagCustom48h12.h"
#include "tagStandard41h12.h"
#include "tagStandard52h13.h"
#include "common/getopt.h"
#include "apriltag_pose.h"
}

using namespace std;
using namespace cv;


void OpenCapture(VideoCapture& videoCap)
{
   
    if (videoCap.isOpened())
    {
   
        videoCap.release();
    }
    videoCap.open(0);
    if (!videoCap.isOpened())
    {
   
        QMessageBox::information(NULL, "Error", "Open video fail!");
    }
}


 //视频流畅
void Mat2QImage(const Mat &mImg,QImage &img) {
   
    cvtColor(mImg, mImg, CV_BGR2RGB);   //注意调整颜色,在opencv里是BGR要转换成RGB
    img = QImage((const unsigned char*)(mImg.data), mImg.cols, mImg.rows, QImage::Format_RGB888);
    img = img.scaled(img.width(), img.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);   //图片缩放
}
void Location:: GetCapture(VideoCapture& videoCap, QImage& QImg)
{
   
    frame = Mat::zeros(Size(1920, 1080), CV_8UC3);
    videoCap >> frame;

    if (!frame.empty())
    {
   
        if (frame.channels() == 3)
        {
   
            Mat2QImage(frame, QImg);
        }
    }
}

void Location:: opencv_demo(){
   
    OpenCapture(capture);      //打开摄像头
    capture.set(CAP_PROP_FRAME_HEIGHT, 1080);         //1080
    capture.set(CAP_PROP_FRAME_WIDTH, 1920);         //1920
    GetCapture(capture, Qframe);
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(NextFrame()));
    timer->start(33);
}


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值