-
Notifications
You must be signed in to change notification settings - Fork 24
/
show_video.m
executable file
·92 lines (77 loc) · 2.85 KB
/
show_video.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function update_visualization_func = show_video(img_files, video_path, resize_image)
%SHOW_VIDEO
% Visualizes a tracker in an interactive figure, given a cell array of
% image file names, their path, and whether to resize the images to
% half size or not.
%
% This function returns an UPDATE_VISUALIZATION function handle, that
% can be called with a frame number and a bounding box [x, y, width,
% height], as soon as the results for a new frame have been calculated.
% This way, your results are shown in real-time, but they are also
% remembered so you can navigate and inspect the video afterwards.
% Press 'Esc' to send a stop signal (returned by UPDATE_VISUALIZATION).
%
% Joao F. Henriques, 2014
% http://www.isr.uc.pt/~henriques/
%store one instance per frame
num_frames = numel(img_files);
boxes = cell(num_frames,1);
affines = cell(num_frames,1);
%create window
[fig_h, axes_h, unused, scroll] = videofig(num_frames, @redraw, [], [], @on_key_press); %#ok, unused outputs
set(fig_h, 'Name', ['Tracker - ' video_path])
axis off;
%image and rectangle handles start empty, they are initialized later
im_h = [];
rect_h = [];
line_h=[];
update_visualization_func = @update_visualization;
stop_tracker = false;
function stop = update_visualization(frame, box,aff)
%store the tracker instance for one frame, and show it. returns
%true if processing should stop (user pressed 'Esc').
boxes{frame} = box;
affines{frame} = aff;
scroll(frame);
stop = stop_tracker;
end
function redraw(frame)
%render main image
im = imread([video_path img_files{frame}]);
% if size(im,3) > 1,
% im = rgb2gray(im);
% end
if resize_image,
im = imresize(im, 0.5);
end
if isempty(im_h), %create image
im_h = imshow(im, 'Border','tight', 'InitialMag',200, 'Parent',axes_h);
else %just update it
set(im_h, 'CData', im)
end
%render target bounding box for this frame
if isempty(rect_h), %create it for the first time
rect_h = rectangle('Position',[0,0,1,1], 'EdgeColor','r', 'Parent',axes_h);
end
if ~isempty(boxes{frame}),
set(rect_h, 'Visible', 'on', 'Position', boxes{frame});
else
set(rect_h, 'Visible', 'off');
end
%render affine box for this frame
if isempty(line_h), %create it for the first time
line_h = line('XData',[0 0 1 1 0],'YData',[0 1 1 0 0], 'Color','g', 'LineWidth',2,'Parent',axes_h);
end
if ~isempty(affines{frame}),
set(line_h, 'Visible', 'on', 'XData',affines{frame}(1,:),'YData', affines{frame}(2,:));
else
set(line_h, 'Visible', 'off');
end
%imwrite(frame2im(getframe(gcf)), ['./pic/' num2str(frame) '.png']);
end
function on_key_press(key)
if strcmp(key, 'escape'), %stop on 'Esc'
stop_tracker = true;
end
end
end