www.pudn.com > matlab-vibe.rar > vibe.m, change:2016-03-02,size:1561b


clear,clc; 
% [filename,pathname] = uigetfile('*.avi','choose the video name:');%选择视频 
%video = mmReader('moving_car.avi'); 
video =mmReader('a.avi'); 
height = video.Height; 
width = video.Width; 
 
%参数 
NumFrames = video.NumberOfFrames; 
cardinality = 2;%基数 
r = 15;%给定半径 
n = 20; 
% 
%初始化 
bg = zeros(height,width,n); 
for  nn = 1 : n 
    imrgb = read(video,nn); 
    imgray = rgb2gray(imrgb); 
    bg(:,:,nn) = imgray; 
end 
bgpad = padarray(bg,[1 1],'replicate'); 
 
%随机跟新 
 
for f = n+1 : NumFrames 
    imageRGB = read(video,f); 
    image = rgb2gray(imageRGB); 
    for i = 1:height 
        for j = 1:width 
            div = abs(bg(i,j,:) - double(image(i,j))); 
            logic = div < r; 
            bignum =  sum(logic); 
            if bignum > cardinality 
%                 随机选择初始化背景中的一个点用新点进行替换 
%                 rnum = rand(n,1); 
%                 maxnum = find(rnum == max(rnum)); 
                randz = randi(n); 
                %bg(i,j,int8(rand(1)*n)) = image(i,j); 
                bgpad(i+1,j+1,randz) = image(i,j); 
%                 随机改变(i,j)邻域 
                randy = randi([-1 1]); 
                randx = randi([-1 1]); 
                bgpad(i+1+randy,j+1+randx,randz) = image(i,j);     
%                 
            end 
        end 
    end 
    randbg = randi(n); 
    out = bgpad(2:height+1,2:width+1,randbg); 
    figure(1),imshow(out,[]); 
    drawnow; 
end 
finalbg = mean(bgpad(2:height+1,2:width+1),3); 
figure(2),imshow(finalbg,[]); 
drawnow;