-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopdata.cpp
107 lines (90 loc) · 2.21 KB
/
loopdata.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
#include "loopdata.h"
using namespace std;
using namespace cv;
LoopData::LoopData(string filename, QObject *parent) : QObject(parent)
{
this->filename = filename;
this->capture.open(this->filename);
this->width = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
this->height = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
this->setFrames(this->loadData());
}
LoopData::~LoopData()
{
this->capture.release();
}
vector<Mat> LoopData::loadData()
{
vector<Mat> frames;
if (capture.isOpened())
{
cv::Mat frame;
capture.set(CV_CAP_PROP_POS_FRAMES, 0);
while (capture.read(frame))
{
if (frame.channels() == 3)
{
cvtColor(frame, frame, CV_BGR2RGB);
}
frames.push_back(frame.clone());
}
}
return frames;
}
double LoopData::getFPS()
{
return this->capture.get(CV_CAP_PROP_FPS);
}
int LoopData::getWidth()
{
return this->width;
}
int LoopData::getHeight()
{
return this->height;
}
void LoopData::setCurrentFrameIndex(unsigned long index)
{
this->currentFrameIndex = index;
}
cv::Mat LoopData::nextFrame()
{
this->currentFrameIndex++;
if (this->currentFrameIndex >= this->frameCount)
{
this->currentFrameIndex = 0;
}
return this->frames[this->currentFrameIndex];
}
vector<Mat> LoopData::getFrames()
{
return this->frames;
}
void LoopData::setFrames(vector<Mat> newFrames)
{
this->frames = newFrames;
this->frameCount = newFrames.size();
}
void LoopData::setWidth(int width)
{
this->width = width;
}
void LoopData::setHeight(int height)
{
this->height = height;
}
void LoopData::writeFile(vector<Mat> frames, string filepath)
{
auto fourcc = static_cast<int>(this->capture.get(CV_CAP_PROP_FOURCC));
auto width = static_cast<int>(this->capture.get(CV_CAP_PROP_FRAME_WIDTH));
auto height = static_cast<int>(this->capture.get(CV_CAP_PROP_FRAME_HEIGHT));
auto fps = this->getFPS();
auto size = Size(width, height);
cv::VideoWriter writer(filepath, fourcc, fps, size);
for (auto frame : frames)
{
cvtColor(frame, frame, CV_RGB2BGR);
writer.write(frame);
}
writer.release();
}