/
viewmodel.cpp
84 lines (79 loc) · 1.39 KB
/
viewmodel.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
#include "viewmodel.h"
ViewModel::ViewModel()
{
sensor = new KinectSensor();
running = true;
recing = false;
}
HRESULT ViewModel::Init()
{
HRESULT hr = sensor->init(FrameSourceTypes_Depth);
if(FAILED(hr))
{
qDebug("Failed to init kinect sensor");
return hr;
}
return hr;
}
void ViewModel::stop()
{
if(recing)
{
Rec();
}
running = false;
if(sensor)
{
sensor->close();
}
}
void ViewModel::run()
{
if(!sensor) return;
while(running)
{
HRESULT hr = sensor->update();
if(FAILED(hr))
{
continue;
}
cv::Mat depthFrame = sensor->getDepthMat();
if(depthFrame.empty())
{
qDebug("Warning: missed frame from Kinect.");
continue;
}
if(recing) torec.push_back(depthFrame);
depthFrame.convertTo(depthFrame, CV_8U, 1.0/10);
cv::cvtColor(depthFrame, depthFrame, CV_GRAY2RGB);
emit SetDepthFrame(512, 424, depthFrame.data);
}
}
void ViewModel::Rec()
{
if(recing)
{
MatStreamHeader header;
header.bytesPerPixel = 2;
header.channels = 1;
header.height = 424;
header.width = 512;
header.type = CV_16U;
MatStream stream;
stream.SetHead(header);
QString fileName =
QFileDialog::getSaveFileName(
nullptr,
QString(),
QString(),
QString("All files(*.*)"));
stream.Open(string(fileName.toLocal8Bit()),
MatStream::Op::out);
for(int i =0;i<torec.size();i++)
{
stream.Write(torec[i]);
}
stream.Close();
}
recing = !recing;
}