Update V2rgb.m

This commit is contained in:
theyatokami 2023-02-19 18:47:10 +01:00
parent c4d546b02d
commit 7b0bb76cab
1 changed files with 38 additions and 14 deletions

52
V2rgb.m
View File

@ -1,5 +1,32 @@
%%%%%%%%%%%%%%%%%
%
%
% Task: Detect heart rate through video
%
%
%
% Authord: Estevan Biau-Loyer & Darren Gallois
% Date: 19/02/2023
%
%%%%%%%%%%%%%%%%%
% Loop through the images selected
for i = 100:1000
% Set the image path
image_path = imread(sprintf('frame2_%d.jpg', i));
[rows, columns, channels] = size(image_path);
%Create the ROI
roi = image_path(90:150, 300:400, :);
% Save the images adjusted to the ROI
imwrite(roi, (sprintf('frames/my_imagev2%d.jpg',i)));
end
% Initialize a matrix to store RGB values
all_rgb = zeros(720, 3, 61, 101);
% Initialize iteration
a=0;
% Loop through 720 images
for i = 100:1000
@ -19,6 +46,8 @@ for i = 100:1000
all_rgb(i, 2, :, :) = green;
all_rgb(i, 3, :, :) = blue;
a=a+1;
% Display iteration
disp(a);
end
@ -35,7 +64,6 @@ num_images = size(mean_rgb, 1);
% Create a vector of indices for each image
indices = 1:num_images;
% Plot the green channel
figure;
plot(indices, mean_rgb(:, 1), 'r');
@ -52,20 +80,22 @@ legend({'Green', 'Red', 'Blue'});
xlabel('Image index');
ylabel('Mean RGB value');
% Get the standard deviation of each color
red_std = std(mean_rgb(:, 1));
green_std = std(mean_rgb(:, 2));
blue_std = std(mean_rgb(:, 3));
% Get the average value of each color
red_avg = mean(mean_rgb(:, 1));
green_avg = mean(mean_rgb(:, 2));
blue_avg = mean(mean_rgb(:, 3));
disp(green_avg);
% Normalize the values
red_normalized = (mean_rgb(:, 1) - red_avg)/red_std
green_normalized = (mean_rgb(:, 2) - green_avg)/green_std
blue_normalized = (mean_rgb(:, 3) - blue_avg)/blue_std
% Create the 3 channels fft
red_fft = fft(red_normalized)
green_fft = fft(green_normalized)
blue_fft = fft(blue_normalized)
@ -87,30 +117,24 @@ xlabel('Frequency (Hz)');
ylabel('Magnitude');
legend('Red', 'Green', 'Blue');
F=60;
P2 = abs(red_fft/N);
P1 = P2(1:N/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = F*(0:(N/2))/N;
figure;
plot(f,P1)
% Find indices in f that correspond to the frequency range of interest
idx_range = find(f >= 0.75 & f <= 4);
% Extract the amplitude values in the range of interest for each channel
red_amp_range = abs(red_fft(idx_range));
green_amp_range = abs(green_fft(idx_range));
blue_amp_range = abs(blue_fft(idx_range));
% Find the index of the maximum amplitude value in the range of interest for each channel
[red_max_amp, red_max_idx] = max(red_amp_range);
[green_max_amp, green_max_idx] = max(green_amp_range);
[blue_max_amp, blue_max_idx] = max(blue_amp_range);
% Convert index of maximum amplitude value to frequency, and then to bpm
red_bpm = f(idx_range(red_max_idx)) * 60;
green_bpm = f(idx_range(green_max_idx)) * 60;
blue_bpm = f(idx_range(blue_max_idx)) * 60;
% Determine heart rate by taking the average of the bpm values for the three channels
heart_rate = mean([red_bpm, green_bpm, blue_bpm]);
disp(heart_rate);