This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.cpp
107 lines (92 loc) · 2.54 KB
/
stream.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "stream.h"
#include <QDebug>
// two constructors:
// To instantiate device camera stream, need to pass in int 0
// To instantiate online/local video stream, need to pass in string url/filepath
stream::stream(QObject *parent, int source)
: QThread{parent}, myVideoCap{source}
{
}
stream::stream(QObject *parent, std::string source)
: QThread{parent}, myVideoCap{source}
{
}
// run thread to continuously read next frame from source and emit signal,
// which will be received by QLabel slot, until video ends
void stream::run()
{
if (myVideoCap.isOpened())
{
while (true)
{
myVideoCap.read(myFrame);
if (!myFrame.empty())
{
myPixmap = cvMatToQPixmap(myFrame);
emit newPixmapCaptured();
cv::waitKey(5);
}
}
}
}
// helper function to convert OpenCV format to qt QImage format
QImage stream::cvMatToQImage(const cv::Mat &inMat)
{
switch (inMat.type())
{
// 8-bit, 4 channel
case CV_8UC4:
{
QImage image(inMat.data,
inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_ARGB32);
return image;
}
// 8-bit, 3 channel
case CV_8UC3:
{
QImage image(inMat.data,
inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_RGB888);
return image.rgbSwapped();
}
// 8-bit, 1 channel
case CV_8UC1:
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
QImage image(inMat.data,
inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_Grayscale8);
#else
static QVector<QRgb> sColorTable;
// only create our color table the first time
if (sColorTable.isEmpty())
{
sColorTable.resize(256);
for (int i = 0; i < 256; ++i)
{
sColorTable[i] = qRgb(i, i, i);
}
}
QImage image(inMat.data,
inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_Indexed8);
image.setColorTable(sColorTable);
#endif
return image;
}
default:
qWarning() << "ASM::cvMatToQImage() - cv::Mat image type not handled in switch:" << inMat.type();
break;
}
return QImage();
}
// helper function to convert QImage to pixmap as QLabel uses latter
QPixmap stream::cvMatToQPixmap(const cv::Mat &inMat)
{
return QPixmap::fromImage(cvMatToQImage(inMat));
}