新建一个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);
}