-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathCHROM_DEHAAN.m
219 lines (180 loc) · 7.55 KB
/
CHROM_DEHAAN.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
function [BVP, PR, HR_ECG, PR_PPG, SNR] = CHROM_DEHAAN(VideoFile, FS, StartTime, Duration, ECGFile, PPGFile, PlotTF)
% CHROM_DEHAAN The Chrominance Method from: De Haan, G., & Jeanne, V. (2013). Robust pulse rate from chrominance-based rPPG. IEEE Transactions on Biomedical Engineering, 60(10), 2878-2886. DOI: 10.1109/TBME.2013.2266196
%
% Inputs:
% VideoFile = Video file path.
% FS = Video framerate (fps).
% StartTime = Timepoint at which to start process (default = 0 seconds).
% Duration = Duration of the time window to process (default = 60 seconds).
% ECGFile = File path to corresponding ECG data file (.mat) containing: 1) The waveform - ECGData.data, 2) The ECG sampling rate - ECGData.fs, 3) The ECG peak locations (in samples) - ECGData.peaks.
% PPGFile = File path to corresponding PPG data file (.mat) containing: 1) The waveform - PPGData.data, 2) The PPG sampling rate - PPGData.fs, 3) The PPG peak locations (in samples) - PPGData.peaks.
% PlotTF = Boolean to turn plotting results on or off.
%
% Outputs:
% BVP = Processed Blood Volume Pulse (BVP).
% PR = Estimated Pulse Rate (PR) from processed BVP timeseries using peak in periodogram.
% HR_ECG = Gold standard Heart Rate (HR) measured from the ECG timeseries R-waves for the window.
% PR_PPG = Pulse Rate (PR) measured from the PPG timeseries systolic onsets for the window.
% SNR = Blood Volume Pulse Signal-to-Noise Ratio (SNR) calculated based on the ECG HR frequency using a method adapted from the method by G. de Haan, TBME, 2013.
%
% Requires - Signal Processing Toolbox
%
% Daniel McDuff, Ethan Blackford, January 2019
% Copyright (c)
% Licensed under the MIT License and the RAIL AI License.
%% Parameters
SkinSegmentTF=false;
LPF = 0.7; %low cutoff frequency (Hz) - specified as 40 bpm (~0.667 Hz) in reference
HPF = 2.5; %high cutoff frequency (Hz) - specified as 240 bpm (~4.0 Hz) in reference
WinSec=1.6; %(was a 32 frame window with 20 fps camera)
%% Add Backup Functions
if(~license('test', 'image_toolbox')&&SkinSegmentTF)
addpath([cd '\optional\rgb2ycbcr.m']);%GNU GPL rgb2ycbcr.m function
end
%% Plot Control
if(PlotTF)
PlotPRPSD = true;
PlotSNR = true;
else
PlotPRPSD = false;
PlotSNR = false;
end
%% Load Video:
VidObj = VideoReader(VideoFile);
VidObj.CurrentTime = StartTime;
FramesToRead=floor(Duration*VidObj.FrameRate); %video may be encoded at slightly different frame rate
%% Read Video and Spatially Average:
T = zeros(FramesToRead,1);%initialize time vector
RGB = zeros(FramesToRead,3);%initialize color signal
FN = 0;
while hasFrame(VidObj) && (VidObj.CurrentTime <= StartTime+Duration)
FN = FN+1;
T(FN) = VidObj.CurrentTime;
VidFrame = readFrame(VidObj);
%position for optional face detection/tracking - originally specified in reference as using Viola Jones, 2004
VidROI = VidFrame;
if(SkinSegmentTF)%skin segmentation - not specified in reference
YCBCR = rgb2ycbcr(VidROI);
Yth = YCBCR(:,:,1)>80;
CBth = (YCBCR(:,:,2)>77).*(YCBCR(:,:,2)<127);
CRth = (YCBCR(:,:,3)>133).*(YCBCR(:,:,3)<173);
ROISkin = VidROI.*repmat(uint8(Yth.*CBth.*CRth),[1,1,3]);
RGB(FN,:) = squeeze(sum(sum(ROISkin,1),2)./sum(sum(logical(ROISkin),1),2));
else
RGB(FN,:) = sum(sum(VidROI,2)) ./ (size(VidROI,1)*size(VidROI,2));
end
end%endwhile video
if(~license('test', 'image_toolbox')&&SkinSegmentTF)%remove path if added
rmpath([cd '\optional\']);
end
%% CHROM:
NyquistF = 1/2*FS;
[B,A] = butter(3,[LPF/NyquistF HPF/NyquistF]);%Butterworth 3rd order filter - originally specified as an a FIR band-pass filter with cutoff frequencies 40-240 BPM
%Window parameters - overlap, add with 50% overlap
WinL = ceil(WinSec*FS);
if(mod(WinL,2))%force even window size for overlap, add of hanning windowed signals
WinL=WinL+1;
end
NWin = floor((FN-WinL/2)/(WinL/2));
S = zeros(NWin,1);
WinS = 1;%Window Start Index
WinM = WinS+WinL/2;%Window Middle Index
WinE = WinS+WinL-1;%Window End Index
for i = 1:NWin
TWin = T(WinS:WinE,:);
RGBBase = mean(RGB(WinS:WinE,:));
RGBNorm = bsxfun(@times,RGB(WinS:WinE,:),1./RGBBase)-1;
% CHROM
Xs = squeeze(3*RGBNorm(:,1)-2*RGBNorm(:,2));%3Rn-2Gn
Ys = squeeze(1.5*RGBNorm(:,1)+RGBNorm(:,2)-1.5*RGBNorm(:,3));%1.5Rn+Gn-1.5Bn
Xf = filtfilt(B,A,double(Xs));
Yf = filtfilt(B,A,double(Ys));
Alpha = std(Xf)./std(Yf);
SWin = Xf - Alpha.*Yf;
SWin = hann(WinL).*SWin;
%overlap, add Hanning windowed signals
if(i==1)
S = SWin;
TX = TWin;
else
S(WinS:WinM-1) = S(WinS:WinM-1)+SWin(1:WinL/2);%1st half overlap
S(WinM:WinE) = SWin(WinL/2+1:end);%2nd half
TX(WinM:WinE) = TWin(WinL/2+1:end);
end
WinS = WinM;
WinM = WinS+WinL/2;
WinE = WinS+WinL-1;
end
BVP=S;
T=T(1:length(BVP));
% Estimate Pulse Rate from periodogram
PR = prpsd(BVP,FS,40,240,PlotPRPSD);
%% Ground Truth HR:
load(ECGFile);
ECG.time = (1:length(ECG.data))/ECG.fs;
ECGMask = (ECG.time>=StartTime) & (ECG.time<=StartTime+Duration);
ECGPeakMask = ((ECG.peaks./ECG.fs)>=StartTime) & ((ECG.peaks./ECG.fs)<=StartTime+Duration);
HR_ECG = (1/mean(diff(ECG.peaks(ECGPeakMask)./ECG.fs)))*60;
if ~isempty(PPGFile)
load(PPGFile);
PPG.time = (1:length(PPG.data))/PPG.fs;
PPGMask = (PPG.time>=StartTime) & (PPG.time<=StartTime+Duration);
if isfield(PPG,'peaks')
PPGPeakMask = ((PPG.peaks./PPG.fs)>=StartTime) & ((PPG.peaks./PPG.fs)<=StartTime+Duration);
PR_PPG = (1/mean(diff(PPG.peaks(PPGPeakMask)./PPG.fs)))*60;
else
PR_PPG = NaN;
end
else
PR_PPG = NaN;
end
%% SNR
SNR = bvpsnr(BVP,FS,HR_ECG,PlotSNR);
%% Optionally Plot Timeseries
if(PlotTF)
%Plot ECG, PPG, iPPG timeseries
figure
if ~isempty(PPGFile)
%Plot ECG
Ax1=subplot(3,1,1);
plot(ECG.time(ECGMask),ECG.data(ECGMask))
hold on
plot(ECG.peaks(ECGPeakMask)/ECG.fs,ECG.data(ECG.peaks(ECGPeakMask)),'*')
ylabel('ECG (a.u.)')
title('CHROM Method - ECG, PPG, iPPG Timeseries')
%Plot PPG
Ax2=subplot(3,1,2);
plot(PPG.time(PPGMask),PPG.data(PPGMask))
hold on
plot(PPG.peaks(PPGPeakMask)/PPG.fs,PPG.data(PPG.peaks(PPGPeakMask)),'*')
ylabel('PPG (a.u.)')
%Plot iPPG
Ax3=subplot(3,1,3);
plot(T,BVP)
hold on
ylabel('iPPG (a.u.)')
xlabel('Time (s)')
linkaxes([Ax1,Ax2,Ax3],'x')
else
%Plot ECG
Ax1=subplot(2,1,1);
plot(ECG.time(ECGMask),ECG.data(ECGMask))
hold on
plot(ECG.peaks(ECGPeakMask)/ECG.fs,ECG.data(ECG.peaks(ECGPeakMask)),'*')
ylabel('ECG (a.u.)')
title('CHROM Method - ECG, iPPG Timeseries')
%Plot iPPG
Ax2=subplot(2,1,2);
plot(T,BVP)
hold on
ylabel('iPPG (a.u.)')
xlabel('Time (s)')
linkaxes([Ax1,Ax2],'x')
end
xlim([StartTime StartTime+Duration])
end%endif plot
%% Remove Backup Functions
if(~license('test', 'image_toolbox')&&SkinSegmentTF)%remove path if added
rmpath([cd '\optional\']);
end
end%end function