(
1
)近似中值背景模型
% Approximate Median Filter background model
% This m-file implements the background subtraction using reference image
% for moving object segmentation.
clear all;
close all;
% Construct a videoreader class to read a avi file, first the ' highwayII_raw.avi ' ,
% then the ‘highwayII'.
videoObj = mmreader(' highwayII_raw.avi');
numFrames =videoObj.NumberOfFrames;
FPS = videoObj.FrameRate; %Get the speed of the AVI movie in frames per second (fps)
% Read the first frame in the video sequence as the initial value
newframe = read(videoObj, 1);
fmed = double(newframe);
% Get the height, width, and number of color components of the frame
[height, width, numColor]=size(newframe);
% Assign a value to the threshold
Threh = 20;
beta = 1.0;
fg = zeros(height, width);
% To avoid consuming too much memories, read only a one frame each time.
for n = 1:numFrames
newframe = read(videoObj, n);
% Calculate the differrence image between the new frame
% and the referrence frame Iref.
Idiff = double(newframe) - fmed;
% Update the median of each pixel value
pixInc = find(Idiff > 0);
fmed(pixInc) = fmed(pixInc) + beta;
pixDec = find(Idiff < 0);
fmed(pixDec) = fmed(pixDec) - beta;
% Motion segment, detection moving object by threholding Idiff
fg = abs(Idiff) >Threh;
if ( numColor == 3) % color image
fg = fg(:, :, 1) | fg(:, :, 2) | fg(:, :, 3);
end
figure(1);
subplot(1,2,1), imshow(newframe);
title(strcat('Current Image, No. ', int2str(n)));
subplot(1,2,2), imshow(fg);
title('Segmented result using Approximate Median Filter');
% put the segmented result frames into a movie structure
[X,map]= gray2ind(fg, 256);
Mov1(n)=im2frame( X,map);