forked from opentrack/opentrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ar_video_widget.cpp
54 lines (47 loc) · 1.54 KB
/
ar_video_widget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Copyright (c) 2012 Patrick Ruoff
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*/
#include "ar_video_widget.h"
#include <QDebug>
using namespace std;
void ArucoVideoWidget::update_image(const cv::Mat& frame)
{
QMutexLocker foo(&mtx);
_frame = frame.clone();
}
void ArucoVideoWidget::update_and_repaint()
{
QMutexLocker foo(&mtx);
if (_frame.cols*_frame.rows <= 0)
return;
QImage qframe = QImage(_frame.cols, _frame.rows, QImage::Format_RGB888);
uchar* data = qframe.bits();
const int pitch = qframe.bytesPerLine();
for (int y = 0; y < _frame.rows; y++)
{
for (int x = 0; x < _frame.cols; x++)
{
const auto& elt = _frame.at<cv::Vec3b>(y, x);
const cv::Scalar elt2 = static_cast<cv::Scalar>(elt);
data[y * pitch + x * 3 + 0] = elt2.val[2];
data[y * pitch + x * 3 + 1] = elt2.val[1];
data[y * pitch + x * 3 + 2] = elt2.val[0];
}
}
auto qframe2 = qframe.scaled(size(), Qt::IgnoreAspectRatio, Qt::FastTransformation);
texture = qframe2;
update();
}
void ArucoVideoWidget::paintEvent(QPaintEvent* e)
{
QMutexLocker foo(&mtx);
QPainter(this).drawImage(e->rect(), texture);
}
ArucoVideoWidget::ArucoVideoWidget(QWidget* parent): QWidget(parent)
{
connect(&timer, SIGNAL(timeout()), this, SLOT(update_and_repaint()));
timer.start(60);
}