-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocessVideoFile.m
78 lines (61 loc) · 2.33 KB
/
processVideoFile.m
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
% Processes the given file by tracking the moving table tennis ball in the
% video.
% Input:
% videoFilename (char): Relative path to the video file.
% Output:
% xPositions ():
% yPositions ():
% Writes csv file to disk containing coordinates of the ball
function [xPositions, yPositions] = processVideoFile(videoFilename, cam)
fprintf("In function processVideoFile()\n");
videoObj = VideoReader(videoFilename);
xPositions = [];
yPositions = [];
% ================================Constants================================
NUM_FRAMES = videoObj.NumberOfFrames;
%CSV File Particulars
FILENAME = strcat(videoFilename, '.csv');
DELIMITER = ',';
HEADER1 = 'frame';
HEADER2 = 'x';
HEADER3 = "y";
NOT_FOUND = -1.0;
PREV_Y = -2.0;
% ===========================Logic/Implementation==========================
csvFileObj = fopen(FILENAME,'w');
% Write headers to csv file
headersCsvEntry = strcat(HEADER1, DELIMITER, HEADER2, DELIMITER, HEADER3, '\n');
fprintf(csvFileObj,headersCsvEntry);
% Load/generate background of video
backgroundFile = strcat(videoFilename, 'background.png');
if ~exist(backgroundFile, 'file')
videoBackground = getBackgroundImage(videoObj, NUM_FRAMES);
imwrite(uint8(videoBackground), backgroundFile, 'png');
else
fprintf("Loading background from disk\n");
videoBackground = double(imread(backgroundFile));
end
xPositions = zeros(NUM_FRAMES);
yPositions = zeros(NUM_FRAMES);
for frameNum = 1 : 100
% Read video frame and convert to image
vidFrame = double(read(videoObj, frameNum));
% Get moving objects in this frame
movingObjects = abs(vidFrame - videoBackground);
% diff = uint8(movingObjects);
movingObjectsGrayscale = rgb2gray(uint8(movingObjects));
[x, y] = getBallPosition(movingObjectsGrayscale, cam);
xPositions(frameNum) = x;
yPositions(frameNum) = y;
x = sprintf('%4.1f',x);
y = sprintf('%4.1f',y);
if strcmp(x, sprintf('%4.1f',NOT_FOUND)) && strcmp(y, sprintf('%4.1f',NOT_FOUND))
posCsvEntry = strcat(int2str(frameNum-1), DELIMITER, DELIMITER, '\n');
else
posCsvEntry = strcat(int2str(frameNum-1), DELIMITER, x, DELIMITER, y, '\n');
end
fprintf(posCsvEntry);
fprintf(csvFileObj,posCsvEntry);
% TODO: Stop tracking after ball is out of table
end
end