Professional Documents
Culture Documents
Mixture of Gaussian code is running very very slow. - MATLAB Answers - MATLAB Central
Search:
Answers
Create Account
File Exchange
Answ ers
New sgroup
Link Exchange
Blogs
Trendy
Cody
Contest
Log In
MathWorks.com
Here I am pasting my code. Plz tell me if there is any correction to work it fast.......
% 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 all
% source = aviread('C:\Video\Source\traffic\san_fran_traffic_30sec_QVGA');
%source = aviread('..\test_video\san_fran_traffic_30sec_QVGA_Cinepak');
[FileName, PathName] = uigetfile('*.avi', 'Pick an avi-file');
source= aviread(strcat(PathName, FileName));
% ----------------------- frame size variables ----------------------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.10;
% 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)
%cmap = rand(236, 3);
%Mov1=zeros(length(source));
%preallocating the array for Mov1
%Mov2=zeros(length(source));
%preallocating the array for Mov2
% --------------------- initialize component means and weights ----------pixel_depth = 8;
pixel_range = 2^pixel_depth -1;
% 8-bit resolution
% pixel range (# of possible values)
for i=1:height
for j=1:width
for k=1:C
mean(i,j,k) = rand*pixel_range;
w(i,j,k) = 1/C;
sd(i,j,k) = sd_init;
end
end
end
% read in frame
% convert frame to grayscale
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;
http://www.mathworks.in/matlabcentral/answers/39790-mixture-of-gaussian-code-is-running-very-very-slow
1/3
7/11/2014
Mixture of Gaussian code is running very very slow. - MATLAB Answers - MATLAB Central
end
rank = w(i,j,:)./sd(i,j,:);
rank_ind = [1:1:C];
end
end
end
% calculate foreground
match = 0;
k=1;
fg(i,j) = 0;
while ((match == 0)&&(k<=m))
end
end
end
figure(1),subplot(3,1,1),imshow(fr)
subplot(3,1,2),imshow(uint8(bg_bw))
subplot(3,1,3),imshow(uint8(fg))
Mov1(n) = im2frame(uint8(fg),gray);
Mov2(n) = im2frame(uint8(bg_bw),gray);
end
movie2avi(Mov1,'mixture_of_gaussians_output','fps',50);
movie2avi(Mov2,'mixture_of_gaussians_background','fps',50);
4 Comments
Show 1 older comment
Walter Roberson on 30 May 2012
Link
0 votes
Tags
preallocating mov1
http://www.mathworks.in/matlabcentral/answers/39790-mixture-of-gaussian-code-is-running-very-very-slow
2/3
7/11/2014
Mixture of Gaussian code is running very very slow. - MATLAB Answers - MATLAB Central
Products
MATLAB
0 Answers
Contact us
Site Help
Featured MathWorks.com Topics:
Patents
Trademarks
Privacy Policy
New Products
Support
Documentation
http://www.mathworks.in/matlabcentral/answers/39790-mixture-of-gaussian-code-is-running-very-very-slow
Preventing Piracy
Training
Terms of Use
Webinars
Newsletters
MATLAB Trials
Careers
3/3