MATLAB编程混合高斯模型和改进的CV模型相结合的目标跟踪算法源码程序 - matlab算法设计 - 谷速源码
下载频道> 资源分类> matlab源码> 算法设计> MATLAB编程混合高斯模型和改进的CV模型相结合的目标跟踪算法源码程序

标题:MATLAB编程混合高斯模型和改进的CV模型相结合的目标跟踪算法源码程序
分享到:

所属分类: 算法设计 资源类型:程序源码 文件大小: 15.86 MB 上传时间: 2019-12-01 23:03:40 下载次数: 35 资源积分:1分 提 供 者: jiqiren 20191201110453896
内容:
MATLAB编程混合高斯模型和改进的CV模型相结合的目标跟踪算法源码程序,能够准确的跟踪出车辆与行人
% This m-file implements the mixture of Gaussians algorithm for background
% subtraction.  It may be used free of charge for any purpose (commercial
% or otherwise), as long as the author (Seth Benton) is acknowledged.
 
 
clear global;clear variables
close all; warning off
 
 
source = aviread('san_fran_traffic_30sec_QVGA_Cinepak.avi');
 
 
% -----------------------  frame size variables -----------------------
tic
fr = source(1).cdata;           % read in 1st frame as background frame
fr_bw = rgb2gray(fr);     % convert background to greyscale
fr_size = size(fr);             
width = fr_size(2);
height = fr_size(1);
fg = zeros(height, width);
bg_bw = zeros(height, width);
 
% --------------------- mog variables -----------------------------------
 
C = 3;                                  % number of gaussian components (typically 3-5)
M = 3;                                  % number of background components
D = 2.5;                                % positive deviation threshold
alpha = 0.01;                           % learning rate (between 0 and 1) (from paper 0.01)
thresh = 0.25;                          % foreground threshold (0.25 or 0.75 in paper)
sd_init = 6;                            % initial standard deviation (for new components) var = 36 in paper
w = zeros(height,width,C);              % initialize weights array
mean = zeros(height,width,C);           % pixel means
sd = zeros(height,width,C);             % pixel standard deviations
u_diff = zeros(height,width,C);         % difference of each pixel from mean
p = alpha/(1/C);                        % initial p variable (used to update mean and sd)
rank = zeros(1,C);                      % rank of components (w/sd)
 
 
% --------------------- initialize component means and weights -----------
 
pixel_depth = 8;                        % 8-bit resolution
pixel_range = 2^pixel_depth -1;         % pixel range (# of possible values)
 
for i=1:height
    for j=1:width
        for k=1:C
            
            mean(i,j,k) = rand*pixel_range;     % means random (0-255)
            w(i,j,k) = 1/C;                     % weights uniformly dist
            sd(i,j,k) = sd_init;                % initialize to sd_init
            
        end
    end
end
 
%--------------------- process frames -----------------------------------
 
%for n =396:400%length(source)
%for n=455:460
for n=55:60
    fr = source(n).cdata;       % read in frame
   
    fr_bw = rgb2gray(fr);       % convert frame to grayscale
    
    % calculate difference of pixel values from mean
    for m=1:C
        u_diff(:,:,m) = abs(double(fr_bw) - double(mean(:,:,m)));
    end
     
    % update gaussian components for each pixel
    for i=1:height
        for j=1:width
            
            match = 0;
            for k=1:C                       
                if (abs(u_diff(i,j,k)) <= D*sd(i,j,k))       % pixel matches component
                    
                    match = 1;                          % variable to signal component match
                    
                    % update weights, mean, sd, p
                    w(i,j,k) = (1-alpha)*w(i,j,k) + alpha;
                    p = alpha/w(i,j,k);                  
                    mean(i,j,k) = (1-p)*mean(i,j,k) + p*double(fr_bw(i,j));
                    sd(i,j,k) =   sqrt((1-p)*(sd(i,j,k)^2) + p*((double(fr_bw(i,j)) - mean(i,j,k)))^2);
                else                                    % pixel doesn't match component
                    w(i,j,k) = (1-alpha)*w(i,j,k);      % weight slighly decreases
                    
                end
            end
            
            w(i,j,:) = w(i,j,:)./sum(w(i,j,:));
            
            bg_bw(i,j)=0;
            for k=1:C
                bg_bw(i,j) = bg_bw(i,j)+ mean(i,j,k)*w(i,j,k);
            end
            
            % if no components match, create new component
            if (match == 0)
                [min_w, min_w_index] = min(w(i,j,:));  
                mean(i,j,min_w_index) = double(fr_bw(i,j));
                sd(i,j,min_w_index) = sd_init;
            end
 
            rank = w(i,j,:)./sd(i,j,:);             % calculate component rank
            rank_ind = [1:1:C];
            
            % sort rank values
            for k=2:C               
                for m=1:(k-1)
                    
                    if (rank(:,:,k) > rank(:,:,m))                     
                        % swap max values
                        rank_temp = rank(:,:,m);  
                        rank(:,:,m) = rank(:,:,k);
                        rank(:,:,k) = rank_temp;
                        
                        % swap max index values
                        rank_ind_temp = rank_ind(m);  
                        rank_ind(m) = rank_ind(k);
                        rank_ind(k) = rank_ind_temp;    
 
                    end
                end
            end
            
            % calculate foreground
            match = 0;
            k=1;
            
            fg(i,j) = 0;
            while ((match == 0)&&(k<=M))
 
                if (w(i,j,rank_ind(k)) >= thresh)
                    if (abs(u_diff(i,j,rank_ind(k))) <= D*sd(i,j,rank_ind(k)))
                        fg(i,j) = 0;
                        match = 1;
                    else
                        fg(i,j) = fr_bw(i,j);     
                    end
                end
                k = k+1;
            end
        end
    end
    
    figure;
    subplot(2,3,1),imshow(fr);
    text(40,260,'(a1)输入图像');text(40,285,'(a1)Input Image');
    subplot(2,3,2),imshow(uint8(bg_bw))
    text(40,260,'(a2)背景模板');text(1,285,'(a2)Background Template');
    subplot(2,3,3),imshow(uint8(fg)) 
    text(40,260,'(a3)混合高斯算法');text(1,285,'(a3)Mixed Gaussian Model');
    t=uint8(fg);
    se=strel('line',3,0);
   f=imclose(t,se);
  
   f=imfill(f,'holes');
 
    f=adpmedian(f,3);
    f=bwmorph(f,'fill',100);
  
   
 
  [Y,area]=falseRemoval(f);
    
    subplot(2,3,4),imshow(Y);
      text(40,275,'(a4)目标检测');text(40,299,'(a4)Target Detection ');
    x=bwlabel(Y,8);
 
    d=regionprops(x);
    
  centroid = d.Centroid;
  
  
  cc = centroid(1)+40;
  cr = centroid(2)-5;
 %cc = centroid(1);
  %cr = centroid(2);
  
 
  
  
  
  
  
  
  
 [MR,MC,Dim] = size(fr);
R=[[0.2845,0.0045]',[0.0045,0.0455]'];
H=[[1,0]',[0,1]',[0,0]',[0,0]'];
Q=0.01*eye(4);
P = 100*eye(4);
dt=3;
A=[[1,0,0,0]',[0,1,0,0]',[dt,0,1,0]',[0,dt,0,1]'];
g = 1; % pixels^2/time step
Bu = [0,0,0,g]';
kfinit=0;
x=zeros(100,4);
if flag==0
    continue
  end
 
  hold on
 
  % Kalman update
  if kfinit==0
    xp = [MC/2,MR/2,0,0]'
  else
    xp=A*x(i-1,:)' + Bu
  end
  kfinit=1;
  PP = A*P*A' + Q
  K = PP*H'*inv(H*PP*H'+R)
  x(i,:) = (xp + K*([cc,cr]' - H*xp))';
 P = (eye(4)-K*H)*PP
  hold on;
  
  
  
  
  
  
% Img=imread('vessel3.bmp'); 
%Img=imnoise(fr,'salt & pepper',0.2);
Img=double(fr(:,:,1));
 
 
 
 
 
 
 
 
% get the size
[nrow,ncol] =size(Img)
ic= cr;
jc= cc;
r=40;
initialLSF = sdf2circle(nrow,ncol,ic,jc,r);
phi=initialLSF;
subplot(2,3,5),contour(phi,[0 0],'b','LineWidth',2);
text(40,275,'(a5)初始化曲线');text(40,295,'(a5)Initialization Curve');
iterNum =50;
lambda1 = 1.0;
lambda2 = 1.0;
nu = 0.004*255*255;
%timestep =10.0;
timestep =10.5;
mu = 1;
epsilon = 1.5;
 
sigma=5.0; 
K=fspecial('gaussian',round(2*sigma)*2+1,sigma); % Gaussian kernel
KI=conv2(Img,K,'same'); 
KONE=conv2(ones(size(Img)),K,'same');
 
sigma=0.5;    % scale parameter in Gaussian kernel for smoothing.
G=fspecial('gaussian',2,sigma);
Img_smooth=conv2(Img,G,'same');  % smooth image by Gaussiin convolution
[Ix,Iy]=gradient(Img);
f=Ix.^2+Iy.^2;
k=0.02.*max(Ix,Iy);
if f<=k
    g=1;
else
    g=1./(1+f-k);
end
 
imagesc(Img,[0 255]);colormap(gray)
hold on;
subplot(2,3,5),contour(phi,[0 0],'b','LineWidth',2);
text(40,275,'(a5)初始化曲线');text(40,295,'(a5)Initialization Curve');
subplot(2,3,6),contour(phi,[0 0],'b','LineWidth',2);
 
 
% start level set evolution
time = cputime;
for n=1:iterNum
   numIter=1;
    %level set evolution.  
    phi=EVOL_LBF(phi,g,Img,K,KI,KONE,nu,timestep,mu,lambda1,lambda2,epsilon,numIter);
     if mod(n,10)==0
        pause(0.001);
        imagesc(Img, [0, 255]);colormap(gray);hold on; axis off;
       subplot(2,3,6), contour(phi,[0 0],'b','LineWidth',2);
       text(40,275,'(a6)目标追踪');text(40,295,'(a6)Target Tracking');
        %iterNum=[num2str(n), ' iterations'];
        %title(iterNum);
        
        hold off;
    end
end
totaltime = cputime - time;
imagesc(Img, [0, 255]);colormap(gray);hold on; axis off;
subplot(2,3,6),contour(phi,[0 0],'b','LineWidth',2);
text(40,275,'(a6)目标追踪');text(40,295,'(a6)Target Tracking');
 
end
toc

文件列表(点击上边下载按钮,如果是垃圾文件请在下面评价差评或者投诉):

EVOL_LBF.m
Main_BG_GM.m
falseRemoval.m
san_fran_traffic_30sec_QVGA_Cinepak.avi
sdf2circle.m

关键词: MATLAB 编程 混合 高斯 模型 改进 CV模型 源码 程序

Top_arrow
回到顶部
联系方式| 版权声明| 招聘信息| 广告服务| 银行汇款| 法律顾问| 兼职技术| 付款方式| 关于我们|
网站客服网站客服 程序员兼职招聘 程序员兼职招聘
沪ICP备19040327号-3
公安备案号:沪公网安备 31011802003874号
库纳格流体控制系统(上海)有限公司 版权所有
Copyright © 1999-2014, GUSUCODE.COM, All Rights Reserved