为了安全起见,我正在使用MATLAB进行实时运动检测项目并生成警报。我创建了实时视频捕获代码并将其保存到AVI文件中,但我无法在代码运行时读取代码中的实时视频。运动检测无法正常工作。我用两个脚本完成了这个。两个脚本独立工作,但组合时不会读取实时输入。我使用的脚本发布在下面。
Enter code here 1. %code for reading and saving AVI file
function realtime_test()
global movie name vid;
% Define frame rate
NumberFrameDisplayPerSecond=10;
% Open figure
hFigure=figure(1);
% Set-up webcam video input
vid = videoinput('winvideo', 1);
% Set parameters for video
% Acquire only one frame each time
set(vid,'FramesPerTrigger',1);
% Go on forever until stopped
set(vid,'TriggerRepeat',Inf);
% Get a grayscale image
set(vid,'ReturnedColorSpace','rgb')
vidRes = get(vid, 'VideoResolution');
nBands = get(vid, 'NumberOfBands');
hImage = imshow( zeros(vidRes(2), vidRes(1), nBands));
preview(vid,hImage);
triggerconfig(vid, 'Manual');
% Set up timer object
TimerData=timer('TimerFcn', {@FrameRateDisplay,vid},'Period',1/NumberFrameDisplayPerSecond,'ExecutionMode','fixedRate','BusyMode','drop');
name = 'Realtime';
movie=avifile(name,'compression','none');
% Start video and timer object
start(vid);
start(TimerData);
% We go on until the figure is closed
uiwait(hFigure);
% Clean up everything
stop(TimerData);
delete(TimerData);
stop(vid);
delete(vid);
movie = close(movie);
% Clear persistent variables
clear functions;
% This function is called by the timer to display one frame of the figure
function FrameRateDisplay(obj, event,vid)
global movie frame;
frame=uint8(getsnapshot(vid));
movie=addframe(movie,frame);
persistent IM;
persistent handlesRaw;
persistent handlesPlot;
trigger(vid);
IM=getdata(vid,1,'uint8');
if isempty(handlesRaw)
% if first execution, we create the figure objects
subplot(2,1,1);
handlesRaw=imagesc(IM);
title('CurrentImage');
% Plot first value
Values=mean(IM(:));
subplot(2,1,2);
handlesPlot=plot(Values);
title('Average of Frame');
xlabel('Frame number');
ylabel('Average value (au)');
else
% We only update what is needed
set(handlesRaw,'CData',IM);
Value=mean(IM(:));
OldValues=get(handlesPlot,'YData');
set(handlesPlot,'YData',[OldValues Value]);
end
2.%运动检测代码
% This m-file implements the frame difference algorithm for background
% subtraction.
clear all;
close all;
% source = aviread('live.avi');
%source = aviread('test.avi');
%--------------------------------------------------------------------------
[filename, pathname] = uigetfile( ...
{'*.avi;*.mpg;*.mpeg;.*flv','Video Files (*.avi,*.mpg,*.mpeg,.*flv)';
'*.*', 'All Files (*.*)'}, ...
'Select a video file');
mov = mmreader(fullfile(pathname,filename));
implay(filename);
source = aviread(filename);
%--------------------------------------------------------------------------
thresh = 50;
bg = source(1).cdata; % Read in 1st frame as background frame
bg_bw = rgb2gray(bg); % Convert background to greyscale
% ----------------------- Set frame size variables -----------------------
fr_size = size(bg);
width = fr_size(2);
height = fr_size(1);
fg = zeros(height, width);
% --------------------- Process frames -----------------------------------
for i = 2:45
fr = source(i).cdata; % Read in frame
fr_bw = rgb2gray(fr); % Convert frame to grayscale
fr_diff = abs(double(fr_bw) - double(bg_bw)); % Cast operands as double to avoid negative overflow
for j=1:width % If fr_diff > thresh pixel in foreground
for k=1:height
if ((fr_diff(k,j) > thresh))
fg(k,j) = fr_bw(k,j);
disp('motion detected');
%------------------------- Executes alarm ---------------------------------
t = 15;
Fs = 50;
[t,Fs] = wavread('Blip.wav');
player = audioplayer(t,Fs);
play(player);
%--------------------------------------------------------------------------
else
fg(k,j) = 0;
end
end
disp('motion not detected');
end
bg_bw = fr_bw;
%figure(1), subplot(3,1,1), imshow(fr)
subplot(3,1,2),imshow(fr_bw)
subplot(3,1,3),imshow(uint8(fg))
figure,imshow(uint8(fr_diff))
%M(i-1) = im2frame(uint8(fg),gray); % Put frames into movie
end
fps = 15;
%movie2avi(M,'frame_difference_output', 'fps', 30); % Save movie as AVI
答案 0 :(得分:1)
您可以使用vid = videoinput('winvideo',1)直接阅读您的实时网络摄像头,而不是在第二段代码中阅读源代码。我认为它将满足您的要求。试试。
答案 1 :(得分:0)
当您出于安全目的这样做时,我猜近实时应该足够好。 这是一个应该有效的流程。请注意,我提到的实体可以是线程,cpu甚至是计算机,无论你喜欢什么。
根据我提出的时间,您将无法获得实时结果,但延迟应限制为最多6秒。