用最簡單的差分法實現了一下前景分割。使用的mall數據集。
思路是這樣的:首先設定一個隊列的長度,若讀取的圖片張數少於隊列長度則以當前讀取到的圖片做平均。否則則以隊列中的圖片做平均。
這樣之后和當前圖片做差分,大於差分閾值的就是前景。
%init close all ; path = './mall_dataset/frames/'; numofImages = 30 ; image_cells = cell(numofImages,1) ; E = cell(numofImages,1) ; Em = cell(numofImages,1) ; %param Wh = 10 ;%人高 Ww = 5 ;%人寬 defualtLenOfList = 5 ;%前景差分時默認的隊列長度 dif_t = 60 ;%差分閾值 %kenel gaussian_kenel = fspecial('gaussian',3,0.5) ; %program for i=1:numofImages temp = i ; prefix = 'seq_00' ; while temp < 1000 prefix = strcat(prefix,'0') ; temp = temp*10 ; end prefix = strcat(prefix,num2str(i)) ; I=imread([path,prefix,'.jpg']); %依次讀取每一幅圖像 I = rgb2gray(I); I = imfilter(I,gaussian_kenel) ; %I = uint8(I) ; [Gmag,Gdir] = imgradient(I) ; E{i}.Gmag = Gmag; E{i}.Gdir = Gdir ; image_cells{i} = I ; %todo strip高=wp/W? 不一定好 %差分處理 Gmean = 0 ; if i~=1 if i>defualtLenOfList lenoflist = defualtLenOfList ; else lenoflist = i-1 ; end Lstart = i-lenoflist ; Ltail = i-1 ; for j=Lstart:Ltail Gmean = Gmean + (E{j}.Gmag ./ lenoflist) ; end Lmean = 0 ;if lenoflist>=defualtLenOfList for k=Lstart:Ltail Lmean = Lmean + (E{k}.Gmag./lenoflist) ; end for k=Lstart:Ltail Lvar = Lvar+((E{k}.Gmag-Lmean).^2)./lenoflist ; end end Gmoving = E{i}.Gmag - Gmean ; Gmoving = Gmoving .* (Gmoving>dif_t) ; G = uint8(Gmoving); imshow(G) ; end end