Moving object segmentation

Explanation

Predominantly frame difference , background subtraction implementation and analysis of the extracted moving object.

Content Experiments

  1. Using MATLAB basic commands and read image imread basic commands or reads video aviread mmreader;
  2. Invoke various moving object detection function for moving object segmentation;
  3. MATLAB using morphological operators to adjust segmentation result;
  4. Learn call format morphological filtering element strel command structure, by changing the shape and size of the structure element, comparing the effect of moving object detection.

Experimental Procedure

The Frame

tic;
clear all
mov = VideoReader('raw.avi');  %读取视频
x = read(mov,2);                  %把第二帧赋给x
fr_size = size(x);               %帧大小
width = fr_size(2);
height = fr_size(1);
c =zeros(height, width);        %生成height*width大小的0矩阵
for k=1:mov.NumberOfFrames-2   %遍历每一帧
    x = read(mov,k);
    y = read(mov,k+2);         %n帧差分。空洞:运动太慢。重影:运动太快。
    m = rgb2gray(x);
    n = rgb2gray(y);
    q=im2double(n);
    w=im2double(m);
    c = q-w ;
    t=50;                          %阈值
    t=t/255;                      %转化为double型数据
    id = c > t;
    cc =zeros(fr_size);
    cc(id) = 1;
    subplot(2,2,1),imshow(m),title('当前帧'),
    subplot(2,2,2),imshow(n),title('后一帧'),
    subplot(2,2,3),imshow(c),title('两帧相减'),
    subplot(2,2,4),imshow(cc),title('阈值后');
    %Mov1(k) = im2frame(uint8(cc),gray); % put frames into movie
    %imwrite(cc,['zcf',sprintf('%04d',mov.NumberOfFrames),'.jpg']);
end
  • Algorithm Analysis
    frame difference method is a method for detecting proposed adjacent frame image having a strong correlation based on the moving image sequence, the corresponding pixels in different frames of subtraction gradation difference absolute value is determined, when the absolute value exceeds a certain threshold when, as a moving target can be determined, in order to achieve target detection.
    If a uniform distribution of gray object, this method will cause certain overlapping portion is formed larger pores, causing severe target segmentation do not communicate, so that the target is not detected. Target motion generated when the hole is too slow, too fast ghosting and motion occurs. Two difference method for the target scenario slower.
    The advantage of this method is that the algorithm is simple and fast, since the environmental adaptability, a scene change is not sensitive to light; disadvantage is that voids occur ghosting phenomenon, the entire region can not extract a moving object, only the contour extraction algorithm relies heavily on the effect of the selected interframe time interval and segmentation threshold.

Background subtraction

Principle background subtraction is the current frame and the background image to obtain a motion difference target region. Background image acquired by the free movement of the frame. BACKGROUND conventional construction methods have time average, a single Gaussian method, mixed Gaussian method or the like. The following is a simple flow chart of background subtraction:
Here Insert Picture Description
less computational background subtraction, but by the light, weather and other external conditions influenced.

Time average method

clear all;
N = 40;                      % N帧用于训练高斯模型,构造背景图像
h = fspecial('gaussian');
imagedata = filter2(h,rgb2gray(imread('rawpic/raw0001.jpg'))); 
mu = imagedata;
[m,n] = size(mu);
pro = zeros(m,n);
for i=1:N                                                     
    filename = sprintf('rawpic/raw%04d.jpg',i);      
    tmp =filter2(h,rgb2gray(imread(filename)));
    mu = mu+tmp;
end;
mu=mu./N;
imwrite(uint8(mu),'sjpj.jpg');
%figure(1),imshow(uint8(mu));
for num = N+1:500                              % 测试
    filename = sprintf('rawpic/raw%04d.jpg',num);
    imagedata = filter2(h,rgb2gray(imread(filename)));
    t=50;                                         % 阈值,可调节
    pro = abs(imagedata-mu)> t;
    imshow(pro),title(sprintf('frame number %d',floor(num)));
    mu = (mu*(num-1) +imagedata)/num; 
    %imwrite(pro,['sjpj',sprintf('%04d',num),'.jpg']);
    %Mov1(num-N) = im2frame(uint8(255*pro),gray); % put frames into movie
end;
  • Algorithm Analysis
    In this experiment, the time-averaged before method 40 used to construct a background video image, divided by the total accumulated number of frames obtained after the background image frame. 40 one by one after the image and the background image to make the frame difference is larger than the threshold value is identified as a moving target. At the same time, then continued treatment to update the frame average. Time averaging algorithm is simple, fast, and on the ambient light changes dynamically changing background sensitive.

Single Gauss

clear all;
N = 40;
h = fspecial('gaussian');
imagedata = filter2(h,rgb2gray(imread('rawpic/raw0001.jpg')));
mu = imagedata;
[m,n] = size(mu);
cov = zeros(m,n);
pro = zeros(m,n);
sav_mu = mu;
a = 0.01;
for i=1:N
    filename = sprintf('rawpic/raw%04d.jpg',i); 
    tmp =filter2(h,rgb2gray(imread(filename)));
    mu = (tmp+(i-1)*sav_mu)./i;
    cov = ((tmp-mu).^2+(i-1)*cov)./(i)+(mu-sav_mu).^2;
    sav_mu = mu;
end;
cov = cov+0.001;                         %防止cov为0
imwrite(uint8(mu),'sjpj.jpg');
 
for num = N+1:500
    filename = sprintf('rawpic/raw%04d.jpg',num);
    imagedata = double(filter2(h,rgb2gray(imread(filename))));
    T=1e-4;                                % 阈值,可调节
    pro = (2*pi)^(-1/2)*exp(-0.5*(imagedata-mu).^2./cov)./sqrt(cov)< T;
%% update covariance and mean
mu = mu +a*(1-pro).*(imagedata-mu); 
cov = cov + a*(1-pro).*((imagedata-mu).^2-cov);
%D1=medfilt2(pro,[5,5]);
%K=filter2(fspecial('average',3),pro)/255;
%se=strel('disk',1); %
%A=imdilate(D1,se); 
%fc=imopen(pro,se);
%B = imfill(fc, 'holes');
imshow(mat2gray(pro)),title(sprintf('frame number %d',floor(num)));
%imwrite(pro,['sjpj',sprintf('%04d',num),'.jpg']);
pause(0.000001);
end;
  • Algorithm analysis
    single Gaussian model is suitable for constant background single occasion, Gaussian mixture model, this is an extension of a single Gaussian model, but the most simple single-Gaussian model, and take parameters in an iterative manner, does not need to model every time.
    Single Gaussian model gradation value of each pixel in the image is regarded as a stochastic process X, and assume that the probability of a certain pixel gray value occurring Gaussian distribution, as expressed in mathematical form:
    Here Insert Picture Description
    wherein, σ² for the code cov, x is a code imagedata, μ is the code mu. - (1/2) and (2 * PI) ^ (-0.5 exp . (ImageData-MU) ^ 2./cov)./sqrt(cov) corresponding to this value is smaller than the threshold value T was moving target. Updating process with variance and the mean frame, wherein a parameter referred to update, in general, is not updated.

Experimental results

  • The Frame
    Here Insert Picture Description
  • Time average method
    Here Insert Picture Description
  • Single Gauss
    Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_42037746/article/details/92389721