diff --git a/heartrate/imageextract.asv b/heartrate/imageextract.asv deleted file mode 100644 index 8fe7a78..0000000 --- a/heartrate/imageextract.asv +++ /dev/null @@ -1,20 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Image extraction code -% -% Author:LY Pechvattana, MINH Meng Hour -% Date: 19/02/2023 -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -function v = imgex[] -video -v = VideoReader('code.avi'); - -n = 1; -while hasFrame(v) - img = readFrame(v); - imwrite(img,strcat('image',num2str(n),'.png')); - n = n+1 -end -n=n-1; - diff --git a/heartrate/main.asv b/heartrate/main.asv deleted file mode 100644 index c7beb9a..0000000 --- a/heartrate/main.asv +++ /dev/null @@ -1,97 +0,0 @@ -%set variable -fps = 15; -windowDuration = 30; -windowShift = 1; -roi = [310 126 114 54]; -imgDirectory = 'images'; - -%global -redChannel = []; -greenChannel = []; -blueChannel = []; - -%list the images -listImg = dir([imgDirectory '/*.png']); - -for l_img=1:windowDuration*fps - % load the current image - image_orginal = imread([imgDirectory '/' listImg(l_img).name]); - - image_face = imcrop(image_orginal,roi); - - %spatial average for r, g, b channels - [r g b]=imsplit(image_face); - - r_mean = mean2(r); - g_mean = mean2(g); - b_mean = mean2(b); - - % store the current "average" pixel in global arrays - redChannel = [redChannel r_mean]; - greenChannel = [greenChannel g_mean]; - blueChannel = [blueChannel b_mean]; - -end - -% estimate temporal average and standard deviation -greenChannel_avg = mean(greenChannel); -greenChannel_std = std(greenChannel); - -%normalization -for i = 1:450 - greenChannel_normal(i) = (greenChannel(i)-greenChannel_avg); -end - -%Fast Fourier transform (fft) of Green Channel -greenChannel_fft = fft(greenChannel_normal); - - -%power spectrum P1 -L = length(greenChannel_normal); -P2 = abs(greenChannel_fft/L); -P1 = P2(1:L/2+1); -P1(2:end-1)=2*P1(2:end-1); -f = 15*(0:(L/2))/L; - -P1 = fftshift(P1); - -plot(f,P1); -title("Power Spectrum") -xlabel("f (Hz)") -ylabel("Power Spectrum (f)") - -%peak - compute the freq vector -[m n]=size(P1); -x = 1:n; -y= 1:m; -[X Y]=meshgrid(x, y); -X = X - floor(n/2)-1; -Y = Y - floor(m/2)-1; -f = sqrt(X.^2 + Y.^2); - -%peak - find the peak in range -indices = (f>=0.75)&(f<=4); -[peaks locations] = findpeaks(P1(indices), 'SortStr','descend'); -location = locations(1); -peak_frequency = f(indices); -peak_frequency = peak_frequency(location); -heartrate = peak_frequency.*60; - -disp(['Heart rate: ', num2str(heartrate), ' bpm']); - -%plotting RGB -figure(2) - -subplot(2,2,1); -plot(redChannel); -title('Red Channel', "-r"); - -subplot(2,2,2); -plot(greenChannel); -title('Green Channel'); - -subplot(2,2,3); -plot(blueChannel); -title('Blue Channel'); - - diff --git a/heartrate/main.m b/heartrate/main.m index 71a413b..8082d7b 100644 --- a/heartrate/main.m +++ b/heartrate/main.m @@ -2,7 +2,7 @@ fps = 15; windowDuration = 30; windowShift = 1; -roi = [310 126 114 54]; +roi = [304 126 120 47]; imgDirectory = 'images'; %global diff --git a/heartrate/roi.png b/heartrate/roi.png new file mode 100644 index 0000000..921ee2a Binary files /dev/null and b/heartrate/roi.png differ