-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_variance_analysis.m
More file actions
85 lines (63 loc) · 2.64 KB
/
image_variance_analysis.m
File metadata and controls
85 lines (63 loc) · 2.64 KB
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
% image_variance_analysis
% Find regions of image data in which the time-variance of brightness is
% high enough to indicate significant movement
% EJR 2017
% License: CC-BY%
%
% Method
% 1. Read in video data
% 2. Get user-defined time range for analysis
% 3. Compute time-variance of image data
% 4. Threshold the time-variance data to identify regions of movement
% 0.0 CONTROL PARAMETERS FOR THIS SCRIPT
filenameMP4 = 'Nov_07_Exp_5_H130D45.MP4'; % Name of file to process
% 0.1 PARTICLE TRACK ANALYSIS - SET TIME RANGE OF INTEREST
tInit = 50; % Timestamp in MP4 data (seconds) for first frame to analyse
tStep = 2; % Time step to get next frame to evaluate particle position
nSteps = 6; % Number of steps to consider
% 0.1.1 PARTICLE TRACK ANALYSIS - GET USER CONFIRMATION OF TIME RANGE
prompt = {'time start (s)','time increment (s)','number of time steps'};
dlg_title = 'Please confirm time range for analysis';
num_lines = 1;
defaultans = { num2str(tInit),num2str(tStep),num2str(nSteps) };
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
tInit = str2num(answer{1});
tStep = str2num(answer{2});
nSteps = str2num(answer{3});
% 1. INPUT
% Create video reader object:
v = VideoReader(filenameMP4);
vid_number_frames = v.Duration.*v.FrameRate;
vid_width = v.Width;
vid_height = v.Height;
% 2. ANALYSIS
% 2.1 Pre-allocate memory for image analysis
imDatGraySum = zeros(vid_height,vid_width);
imDatGraySumSquares = zeros(vid_height,vid_width);
imDatGraySumMBG = zeros(vid_height,vid_width);
imDatGraySumSquaresMBG = zeros(vid_height,vid_width);
for lpImDat =1:nSteps % For the required number of frames of MP4 data
v.CurrentTime = tInit + (lpImDat-1)*tStep; % Find desired timepoint
imDat = readFrame(v); % Read this frame
imDatGray = mean(imDat, 3);
imDatGraySum = imDatGraySum + imDatGray;
imDatGraySumSquares = imDatGraySumSquares + imDatGray.^2;
end
imDatGrayMean = imDatGraySum / vid_number_frames;
for lpImDat =1:nSteps % For the required number of frames of MP4 data
v.CurrentTime = tInit + (lpImDat-1)*tStep; % Find desired timepoint
imDat = readFrame(v); % Read this frame
imDatGray = mean(imDat, 3) - imDatGrayMean;
imDatGrayFrac = imDatGray ./ imDatGrayMean;
imDatGraySumMBG = imDatGraySumMBG + imDatGrayFrac;
imDatGraySumSquaresMBG = imDatGraySumSquaresMBG + imDatGrayFrac.^2;
end
%
% imDat_time_variance = imDatGraySumSquares/vid_number_frames - (imDatGraySum/vid_number_frames ).^2;
%
% imDat_time_variance_n = imDat_time_variance ./ imDatGraySum;
figure(1)
imagesc(imDatGraySumSquaresMBG)
colorbar
foo = min (imDatGraySumSquaresMBG(:))
caxis([foo, foo*1.05])