-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleVideo.h
74 lines (66 loc) · 1.96 KB
/
SimpleVideo.h
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
#ifndef _SIMPLEVIDEO_H_
#define _SIMPLEVIDEO_H_
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <memory>
#include "SimpleImage.h"
using namespace cv;
using namespace std;
class SimpleVideoReader {
public:
SimpleVideoReader(const string& filename) : delegate_(VideoCapture(filename)) {}
bool IsOpened() { return delegate_.isOpened(); }
vector<SimpleImage> ExtractFrames(int frames) {
vector<SimpleImage> ret;
Mat frame;
for (int i = 0; i < frames; i++) {
delegate_ >> frame;
if (frame.empty()) {
return ret;
}
SimpleImage image;
image.Initialize(frame.cols, frame.rows);
for (int x = 0; x < frame.cols; x++) {
for (int y = 0; y < frame.rows; y++) {
Vec3b pixel = frame.at<Vec3b>(y, x);
uchar blue = pixel.val[0];
uchar green = pixel.val[1];
uchar red = pixel.val[2];
image.setPixel(x, frame.rows - y - 1,
RGBColor(red/255.f, green/255.f, blue/255.f));
}
}
ret.push_back(image);
}
return ret;
}
private:
VideoCapture delegate_;
};
class SimpleVideoWriter {
public:
SimpleVideoWriter(const string& filename, double fps, int width, int height) {
delegate_ = VideoWriter(filename, CV_FOURCC('D', 'I', 'V', 'X'),
fps, Size(width, height), true);
}
void WriteToVideo(const vector<unique_ptr<SimpleImage> >& images) {
for (auto image = images.begin();
image != images.end();
++image) {
Size thisSize = Size((*image)->GetWidth(), (*image)->GetHeight());
Mat frame(Size((*image)->GetWidth(), (*image)->GetHeight()), CV_8UC3);
for (int x = 0; x < (*image)->GetWidth(); x++) {
for (int y = 0; y < (*image)->GetHeight(); y++) {
RGBColor rgb = (**image)(x, (*image)->GetHeight() - y - 1);
Vec3b bgr(rgb.b * 255.f, rgb.g * 255.f, rgb.r * 255.f);
frame.at<Vec3b>(y, x) = bgr;
}
}
delegate_ << frame;
}
}
private:
VideoWriter delegate_;
};
#endif