-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFrame.m
114 lines (84 loc) · 2.46 KB
/
getFrame.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function im = getFrame(vid_path,v,fr_num,imInvert,clrMode,imMean)
% Reads image file from a video.
% v - video structure
% fr_num - frame number desired
% vid_path - path to video (use path in v structure as default)
% imInvert - logical that indicates whether to invert a grayscale image
% clrMode - 'rgb' or 'gray' format for image
% imMean - (optional) mean image of video sequence
%
% Developed by McHenryLab at UC Irvine
%% Parse inputs
% Default for image invert
if nargin < 4
imInvert = 0;
end
% Default color mode
if nargin < 5
clrMode = 'rgb';
end
if nargin < 6
imMean = [];
end
% Check requested frame number
if fr_num>v.UserData.LastFrame
error(['Video sequence does not have a frame ' num2str(fr_num)]);
end
% Frame index
iFrame = fr_num-v.UserData.FirstFrame + 1;
%iFrame = fr_num;
if isfield(v.UserData,'FileInfo');
% Get filename and extension
fName = v.UserData.FileInfo(iFrame).name;
ext = fName(find(fName=='.')+1:end);
fName = fName(1:(find(fName=='.')-1));
% Get frame number
frNum = str2num(fName((find(fName=='_')+1):end));
% Check for match
if frNum~=fr_num
error('file numbering does not match the video info');
end
end
%% Read frame
% If it is an image sequence . . .
if isfield(v.UserData,'FileInfo');
% Read image
im = imread([vid_path filesep v.UserData.FileInfo(iFrame).name]);
% Read frame from a movie file . . .
else
% Adjust items to new 'v'
v = VideoReader(vid_path);
% Set current time to last frame
if fr_num/v.FrameRate==v.Duration
v.CurrentTime = (fr_num-1)./v.FrameRate;
% Set current time to other frame
else
v.CurrentTime = fr_num./v.FrameRate;
end
% Read next available frame
im = readFrame(v);
end
%% Modify image
% Convert to grayscale
if strcmp(clrMode,'gray') && size(im,3)==3
im = rgb2gray(im);
if ~isempty(imMean)
imMean = rgb2gray(imMean);
end
end
% Invert, if requested
if imInvert==1
im = imcomplement(im);
end
% Subtract mean image, if present
if ~isempty(imMean)
%im = imsubtract(im,imMean);
if strcmp(clrMode,'gray') || strcmp(clrMode,'rgb')
% Get the compliment
imMean = imcomplement(imMean);
im = imcomplement(im);
im = uint8(double(im)-double(imMean));
im = imcomplement(im);
end
%im = uint8(imadjust(imcomplement(imsubtract(imMean,im))));
end