clc;clear;
len=20;
r=[];
g=[];
b=[];
I=VideoReader('baby.mp4');
fs=I.FrameRate; %读取视频总帧率
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%处理第一帧图像,选定ROI%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
filepath=pwd; %保存当前工作目录
cd('E:\figure') ; %切换工作目录
image=imread('original_frame1.bmp');
cd(filepath); %切回原工作目录
imshow(image);
hold on
%Y=imcrop(image,[676,333,20,20]);
[x,y]=ginput(1); %点击鼠标选点
x0=[x-len,y-len];
ROI = [x0(1)-len, x0(2)-len, 2*len, 2*len];
rectangle ('Position', ROI, 'EdgeColor', 'R', 'LineWidth', 2)
hold off
x0=round(x0);
Y=imcrop(image,[x0(1),x0(2),len,len]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%处理所有帧图片,并将RGB分别储存到r,g,b矩阵中,绘图%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
file_path = 'E:\figure\';% 图像文件夹路径
file_name='original_frame';
suffix='.bmp';
img_path_list = dir([file_path,'*.bmp']);%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
order_num=num2str(j);
total_path=strcat(file_path,file_name,order_num,suffix);
image = imread(total_path);
image=double(image);
Y=imcrop(image,[x0(1),x0(2),len,len]);
%Y=imcrop(image,[676,333,20,20]);
R=Y(:,:,1);
G=Y(:,:,2);
B=Y(:,:,3);
r(j)=mean(mean(R));%红色均值
g(j)=mean(mean(G));%绿色均值
b(j)=mean(mean(B));%蓝色均值
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%绘制三个颜色通道的时间图%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(2)
subplot(3,1,1)
plot(0:1/fs:(225-1)/fs,r);
title('red channel');xlabel('Time/s');ylabel('幅度');
subplot(3,1,2)
plot(0:1/fs:(225-1)/fs,g);
title('green channel');xlabel('Time/s');ylabel('幅度');
subplot(3,1,3)
plot(0:1/fs:(225-1)/fs,b);
title('blue channel');xlabel('Time/s');ylabel('幅度');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%设计带通滤波器,婴儿心跳大约为1.5-2Hz,所以通带应在这个范围%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fp=[1.5,2]; %通带
fst=[0.5,3]; %阻带
wp=2.*fp./fs; %设置通带频率
ws=2.*fst./fs; %设置阻带频率
ap=1; %设置通带波纹系数
as=20; %设置阻带波纹系数
[n,wc]=buttord(wp,ws,ap,as); %巴特沃兹带通滤波器,n为滤波器阶数
[b0,a0]=butter(n,wc);
[h,w]=freqz(b0,a0);
figure(3)
plot(w*fs/2/pi,abs(h)); %画出带通滤波器的频率响应
title('带通滤波器的频率响应'); axis([0 10 0 1.2]);
xlabel('f(Hz)');
ylabel ('幅值');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%滤波%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=length(g);
g1=[g,g];
g11=detrend(g1);
g2=filter(b0,a0,g11); %滤波
g22=g2(N+1:2*N);
figure(4)
plot(0:1/fs:(N-1)/fs,g22); %滤波后时域图像
title ('滤波后时域图像');xlabel ('Time/s');ylabel ('幅值');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%傅里叶变换以及频谱图%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
green=fft(g22,N); %做傅里叶变换
figure(5)
j=0:N-1; f=j*fs/N;
plot(f,abs(green));%画出面部信号的频谱
%axis([0 10 0 1]);
title ('脸部信号频谱');xlabel ('频率/Hz');ylabel ('幅值');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%频谱图%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[pxx,f] = pwelch(g22,128,100,f,fs);
figure(6)
plot(f,pxx);%画出面部信号功率谱
%axis([0 5,0 10e-7]);
title ('脸部信号功率谱');xlabel ('频率/Hz');ylabel ('幅值');
ymax=max(pxx);
X=find(pxx==ymax); %找到最大值点的位置
f(X);
radio=60*f(X) %输出心律
- 1
- 2
前往页