我正在尝试从mp4文件中两行之间的位移中提取频率,但是看起来出了点问题。
首先,我将视频转换为帧,并找到每帧两行之间的距离,然后对其应用FFT,最后找到幅度。
由于输出未接近实际值,似乎我做错了事。
clear all
clc
reader = VideoReader('2.mp4');
nof = reader.NumberOfFrames;
refp = zeros(nof,2);
oscp = zeros(nof,2);
reader = VideoReader('2.mp4');
ii = 1;
while hasFrame(reader)
frame = readFrame(reader);
redc = frame(:,:,1);
redc = redc > 150;
redc = imopen(redc, ones(5));
props = regionprops(redc,'FilledArea','Centroid');
nob = length(props);
if (nob > 1)
blobs = zeros(nob,1);
for jj = 1 : nob
blobs(jj) = props(jj).FilledArea;
end
blobs = sort(blobs);
for jj = 1 : nob
if (props(jj).FilledArea == blobs(1))
ind1 = jj;
end
if (props(jj).FilledArea == blobs(2))
ind2 = jj;
end
end
if (props(ind1).Centroid < props(ind2).Centroid)
refp(ii,:) = props(ind1).Centroid;
oscp(ii,:) = props(ind2).Centroid;
else
refp(ii,:) = props(ind2).Centroid;
oscp(ii,:) = props(ind1).Centroid;
end
else
if (ii > 1)
refp(ii,:) = refp(ii-1,:);
oscp(ii,:) = oscp(ii-1,:);
end
end
ii = ii + 1;
% subplot(1,2,1)
% imshow(frame,[]);
% title('RGB Frame')
% subplot(1,2,2)
% imshow(redc,[]);
% title('Thresholded Red Channel')
% pause(0.05);
end
dist = sqrt((oscp(:,1) - refp(:,1)).^2 + (oscp(:,2) - refp(:,2)).^2);
t = 1:length(dist);
t = t / reader.FrameRate;
figure
plot(t,dist,'r','LineWidth',1.5)
grid minor
title('Oscillations');
% xlim([160,210]);
% ylim([5,inf])
xlabel('Frame Number');
ylabel('Distance (Pixels)');
fdist = fftshift(fft(dist));
fs = reader.FrameRate;
f = fs * (0:length(fdist)-1) / length(fdist);
[m,ind] = max(abs(fdist));
figure
subplot(2,1,1)
plot(f,abs(fdist),'m','LineWidth',1.5);
grid minor
title('FFT (Magnitude)')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
text(f(ind) + 1, abs(fdist(ind)), sprintf('Frequency = %f ',f(ind)));
text(f(ind) + 1, abs(fdist(ind))-30000, sprintf('Amplitude = %f ',abs(fdist(ind))));
subplot(2,1,2)
plot(f,angle(fdist),'Color',[1,0.5,0],'LineWidth',1.5);
grid minor
title('FFT (Phase)');
xlabel('Frequency (Hz)');