diff --git a/heartrate/imageextract.asv b/heartrate/imageextract.asv new file mode 100644 index 0000000..8fe7a78 --- /dev/null +++ b/heartrate/imageextract.asv @@ -0,0 +1,20 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% 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/imageextract.m b/heartrate/imageextract.m index e5eb446..df83be6 100644 --- a/heartrate/imageextract.m +++ b/heartrate/imageextract.m @@ -1,4 +1,14 @@ -v = VideoReader('code.avi'); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Image extraction code +% +% Author:LY Pechvattana, MINH Meng Hour +% Date: 19/02/2023 +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +function v = imageextract(vid) +vid = 'code.avi' +v = VideoReader(vid); n = 1; while hasFrame(v) diff --git a/heartrate/main.asv b/heartrate/main.asv new file mode 100644 index 0000000..c7beb9a --- /dev/null +++ b/heartrate/main.asv @@ -0,0 +1,97 @@ +%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 552509a..71a413b 100644 --- a/heartrate/main.m +++ b/heartrate/main.m @@ -57,8 +57,8 @@ P1 = fftshift(P1); plot(f,P1); title("Power Spectrum") -xlabel("f (Hz)") -ylabel("Power Spectrum (f)") +xlabel("frequency (Hz)") +ylabel("Power Spectrum") %peak - compute the freq vector [m n]=size(P1); @@ -78,3 +78,20 @@ 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,'r'); +title('Red Channel'); + +subplot(2,2,2); +plot(greenChannel,'g'); +title('Green Channel'); + +subplot(2,2,3); +plot(blueChannel,'b'); +title('Blue Channel'); + + diff --git a/heartrate/power_spectrum.png b/heartrate/power_spectrum.png new file mode 100644 index 0000000..914c977 Binary files /dev/null and b/heartrate/power_spectrum.png differ diff --git a/heartrate/rgb_plot.png b/heartrate/rgb_plot.png new file mode 100644 index 0000000..7090606 Binary files /dev/null and b/heartrate/rgb_plot.png differ diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 9b9307d..0000000 --- a/main.cpp +++ /dev/null @@ -1,2 +0,0 @@ -int main() -return; \ No newline at end of file