-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshowTrackedBall.m
69 lines (55 loc) · 1.83 KB
/
showTrackedBall.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
function showTrackedBall(pathToFile, xPositions, yPositions)
videoObj = VideoReader(pathToFile);
NUM_FRAMES = videoObj.NumberOfFrames;
fig = figure;
baseName = pathToFile(1:find(pathToFile=='.')-1);
% Read coordinates from csv file, if exists
csvFile = strcat(baseName, '.csv');
if exist(csvFile, 'file')
trackedCsv = csvread(csvFile,1,0);
xPositions = trackedCsv(:, 2);
yPositions = trackedCsv(:, 3);
end
numTracked = size(xPositions);
numTracked = numTracked(1);
workingDir = tempname;
mkdir(workingDir)
mkdir(workingDir,'images')
for frameNum = 1 : NUM_FRAMES
% Do not show warnings
MSGID = 'images:initSize:adjustingMag';
warning('off', MSGID);
% Read video frame and convert to image
vidFrame = read(videoObj, frameNum);
imshow(vidFrame);
if frameNum <= numTracked
x = xPositions(frameNum);
y = yPositions(frameNum);
else
x = -1;
y = -1;
end
frameWithMarker = vidFrame;
if x > 0 && y > 0
frameWithMarker = insertShape(uint8(vidFrame),'circle',[x y 6.5],'LineWidth',3,'Color','green');
imshow(frameWithMarker);
drawnow;
end
drawnow;
filename = [sprintf('%03d',frameNum) '.jpg'];
fullname = fullfile(workingDir,'images',filename);
imwrite(frameWithMarker,fullname) % Write out to a JPEG file (img1.jpg, img2.jpg, etc.)
end
% Generate and save the video file with the marker to disk
imageNames = dir(fullfile(workingDir,'images','*.jpg'));
imageNames = {imageNames.name}';
baseName = pathToFile(1:find(pathToFile=='.')-1);
outputVideo = VideoWriter(strcat(baseName,'_tracked'), 'MPEG-4');
outputVideo.FrameRate = videoObj.FrameRate;
open(outputVideo)
for i = 1:length(imageNames)
img = imread(fullfile(workingDir,'images',imageNames{i}));
writeVideo(outputVideo,img)
end
close(outputVideo)
end