Octave heartrate found

This commit is contained in:
Thomas PÉRIN 2023-02-19 10:51:27 +01:00
parent e0eddd2d1e
commit 28fbddcb67
1 changed files with 18 additions and 32 deletions

View File

@ -2,16 +2,14 @@
%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%
% hr = heartRateEstimation(imgDirectory, fps, windowDuration, windowShift) % hr = heartRateEstimation(imgDirectory, fps, windowDuration, windowShift)
% %
% Task: % Task: Finding the heartrate of person from selcted images
% %
% Inputs: % Inputs:
% -imgDirectory % -imgDirectory
% -fps % -fps
% -windowDuration
% -windowShift
% %
% Output: % Output:
% -hr: % -heartrate:
% %
% Author: Thomas Périn % Author: Thomas Périn
% Date: 17/02/2023 % Date: 17/02/2023
@ -22,11 +20,9 @@
clc clc
clear all clear all
%temp var % temp var
%imgDirectory = 'img';
fps = 15; fps = 15;
windowDuration = 30; windowDuration = 30;
windowShift = 1;
roi = [280 580 600 850]; roi = [280 580 600 850];
% global % global
@ -34,20 +30,16 @@ redChannel = [];
greenChannel = []; greenChannel = [];
blueChannel = []; blueChannel = [];
%list the images %list the images with i the same directory
%listImg = dir([imgDirectory '/*.png']);
listImg = dir(['*.png']); listImg = dir(['*.png']);
% iteration for every images available
for l_img=1:windowDuration*fps for l_img=1:windowDuration*fps
% load the current image % load the current image
%image_original = imread([imgDirectory '/' listImg(l_img).name]);
image_original = imread([listImg(l_img).name]); image_original = imread([listImg(l_img).name]);
%image(image_original);
%imshow([listImg(l_img).name])
% crop the current image around the face % crop the current image around the face
image_face = image_original(roi(1):roi(2), roi(3):roi(4), :); image_face = image_original(roi(1):roi(2), roi(3):roi(4), :);
%image(image_face);
% spatial average for r, g, b channels % spatial average for r, g, b channels
r_mean = mean(mean(image_face(:,:,1))); r_mean = mean(mean(image_face(:,:,1)));
@ -69,15 +61,16 @@ blueChannel_avg = mean(blueChannel);
blueChannel_std = std(blueChannel); blueChannel_std = std(blueChannel);
% normalize your data % normalize your data
%first define length of our data % first define length of our data
L = columns(greenChannel); L = columns(greenChannel);
for i=1:L for i=1:L
greenChannel_normalized(i) = (greenChannel(i) - greenChannel_avg)/greenChannel_std; greenChannel_normalized(i) = (greenChannel(i) - greenChannel_avg)/greenChannel_std;
end end
% select only greenChannel for simplicity (no ICA)
greenChannel_fft = fft(greenChannel_normalized); greenChannel_fft = fft(greenChannel_normalized);
% power spectrum (https://www.mathworks.com/help/matlab/ref/fft.html) % power spectrum using link(https://www.mathworks.com/help/matlab/ref/fft.html)
P2 = abs(greenChannel_fft/L); P2 = abs(greenChannel_fft/L);
P1 = P2(1:L/2+1); P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1); P1(2:end-1) = 2*P1(2:end-1);
@ -85,21 +78,14 @@ f = 15*(0:(L/2))/L;
plot(f,P1) plot(f,P1)
% find the peak in the range ([0.75 4] Hz) % find the peak in the range ([0.75 4] Hz)
% max -> value, index % define range and round to get intergers
low_index = round(0.75*L/15);
low_index = 0.75*L/15+0.5 high_index = round(2*L/15);
high_index = 2*L/15 % find max value and index inn new range
a = max(P1(low_index:high_index)) [a, ia] = max(P1(low_index:high_index));
heart_rate = f(a) % adapt the index for full range
heart_frequency = f(ia+low_index)
% convert the index from Hz to bpm
% determine heart rate % determine heart rate
% convert the index from Hz to bpm
heart_rate = heart_frequency*60