/
motion_vector_file_utils.h
118 lines (95 loc) · 2.5 KB
/
motion_vector_file_utils.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
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
108
109
110
111
112
113
114
115
116
117
118
#include <string>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <vector>
using namespace std;
#ifndef __READ_MOTION_VECTOR_FILE_H__
#define __READ_MOTION_VECTOR_FILE_H__
const int NO_MV = -10000;
struct MotionVector
{
int X,Y;
float Dx,Dy;
int Mx, My;
char TypeCode, SegmCode;
bool NoMotionVector()
{
return (Dx == NO_MV && Dy == NO_MV) || (Dx == -NO_MV && Dy == -NO_MV);
}
bool IsIntra()
{
return TypeCode == 'P' || TypeCode == 'A' || TypeCode == 'i' || TypeCode == 'I';
}
};
typedef pair<int, vector<MotionVector> > FlowPoints;
struct MotionVectorFileWriter
{
FILE* motionVectorsFile;
MotionVectorFileWriter(string path = "")
{
if(path != "")
Open(path);
}
void Open(string motionVectorsPath)
{
motionVectorsFile = fopen(motionVectorsPath.c_str(), "w");
fputs("FrameIndex\tX\tY\tDx\tDy\tMacroBlockTopLeftCornerX\tMacroBlockTopLeftCornerY\tTypeSegm\n", motionVectorsFile);
}
void Write(int index, int sx, int sy, double dx, double dy, int mx = -1, int my = -1, char typeCode = '_', char segmCode = '_')
{
fprintf(motionVectorsFile, "%d\t%d\t%d\t%.2f\t%.2f\t%d\t%d\t%c%c\n", index, sx, sy, dx, dy, mx, my, typeCode, segmCode);
}
~MotionVectorFileWriter()
{
fclose(motionVectorsFile);
}
};
struct MotionVectorFileReader2
{
FILE* motionVectorsFile;
char curLine[200];
void Open(string motionVectorsPath)
{
motionVectorsFile = fopen(motionVectorsPath.c_str(), "r");
fgets(curLine,100,motionVectorsFile);
fgets(curLine,100,motionVectorsFile);
}
MotionVectorFileReader2(string motionVectorsPath = "")
{
motionVectorsFile = NULL;
if(motionVectorsPath != "")
Open(motionVectorsPath);
}
FlowPoints ReadFlowPoints()
{
vector<MotionVector> pts;
if(strcmp(curLine,"") == 0)
return make_pair(-1, pts);
int curFrameIndex, frameIndex;
MotionVector mv;
sscanf(curLine, "%d %d %d %f %f %d %d %c%c", &curFrameIndex, &mv.X, &mv.Y, &mv.Dx, &mv.Dy, &mv.Mx, &mv.My, &mv.TypeCode, &mv.SegmCode);
pts.push_back(mv);
while(true)
{
strcpy(curLine, "");
if(fgets(curLine, 100, motionVectorsFile) == NULL)
break;
if(strcmp(curLine, "") == 0)
break;
sscanf(curLine, "%d %d %d %f %f %d %d %c%c", &frameIndex, &mv.X, &mv.Y, &mv.Dx, &mv.Dy, &mv.Mx, &mv.My, &mv.TypeCode, &mv.SegmCode);
if(frameIndex != curFrameIndex)
break;
pts.push_back(mv);
}
return make_pair(curFrameIndex, pts);
}
~MotionVectorFileReader2()
{
if(motionVectorsFile != NULL)
fclose(motionVectorsFile);
}
};
#endif