Hi guys, I run my codes but got this divide by zero error... The
expected result should be 3 sets of mu and sigmas value. I don't
understand why I would have got this error...
Please HELP
Below are the codes:
stframe = 1; endframe = 5;
noframes = endframe - stframe + 1;
ImgHeight = 480; ImgWidth = 720;
J = zeros(480,720,noframes);
cnt = 1;
for i = stframe:endframe
fname = sprintf('%.9d.bmp',i);
I = imread(fname,'bmp');
Igray = rgb2gray(I);
J,:,cnt) = Igray;
cnt = cnt + 1;
end
M = 3;
bg_mu = zeros(ImgHeight,ImgWidth,M);
bg_sigma = zeros(ImgHeight,ImgWidth,M);
for row = 1:ImgHeight
for col = 1:ImgWidth
data = J(row,col,;
data = data);
data1 = [data data];
[h, mu, sigmas, P] = em(M, data1);
mu = mu,1); sigmas = sigmas,1);
bg_mu(row,col, = mu);
bg_sigma(row,col, = sigmas);
end
end
function [h, mu, sigmas, P] = em(M, Y);
maxEpoch = 10; [T,D] = size(Y);
h = ones(T,M)/M; P = ones(M,1)/M;
for j=1:M,
k = 0;
while (k < 1) | (k > T)
k = round(rand(1)*T);
end
mu(j,1) = Y(k,1);
sigmas(j,1) = ones(1,D);
end
for epoch=1:maxEpoch,
for t=1:T,
for k=1:M,
g(k)=Gaussian(Y(t,,mu(k,,sigmas(k,);
sigmas
end
s0 = sum(g);
for j=1:M,
h(t,j) = g(j)/s0;
end
end
for j=1:M,
for i=1,
s1=0; s2=0; s3=0;
for t=1:T,
s1 = s1 + h(t,j)*Y(t,i);
s2 = s2 + h(t,j);
end
mu(j,i) = s1/s2;
P(j) = s2/T;
s3=0;
for t=1:T,
s3 = s3 + h(t,j)*(Y(t,i)-mu(j,i))^2;
end
sigmas(j,i) = s3/s2;
end
end
end
function y = Gaussian(x, mu, Sigma2);
r2 = ((double(x(1)) - mu(1))^2)/(Sigma2(1)) + ((double(x(2)) -
mu(2))^2)/(Sigma2(2));
y2 = exp(- r2)/(2. * 3.1416 * sqrt(Sigma2(1) * Sigma2(2)));
y = y2;
expected result should be 3 sets of mu and sigmas value. I don't
understand why I would have got this error...
Please HELP
Below are the codes:
stframe = 1; endframe = 5;
noframes = endframe - stframe + 1;
ImgHeight = 480; ImgWidth = 720;
J = zeros(480,720,noframes);
cnt = 1;
for i = stframe:endframe
fname = sprintf('%.9d.bmp',i);
I = imread(fname,'bmp');
Igray = rgb2gray(I);
J,:,cnt) = Igray;
cnt = cnt + 1;
end
M = 3;
bg_mu = zeros(ImgHeight,ImgWidth,M);
bg_sigma = zeros(ImgHeight,ImgWidth,M);
for row = 1:ImgHeight
for col = 1:ImgWidth
data = J(row,col,;
data = data);
data1 = [data data];
[h, mu, sigmas, P] = em(M, data1);
mu = mu,1); sigmas = sigmas,1);
bg_mu(row,col, = mu);
bg_sigma(row,col, = sigmas);
end
end
function [h, mu, sigmas, P] = em(M, Y);
maxEpoch = 10; [T,D] = size(Y);
h = ones(T,M)/M; P = ones(M,1)/M;
for j=1:M,
k = 0;
while (k < 1) | (k > T)
k = round(rand(1)*T);
end
mu(j,1) = Y(k,1);
sigmas(j,1) = ones(1,D);
end
for epoch=1:maxEpoch,
for t=1:T,
for k=1:M,
g(k)=Gaussian(Y(t,,mu(k,,sigmas(k,);
sigmas
end
s0 = sum(g);
for j=1:M,
h(t,j) = g(j)/s0;
end
end
for j=1:M,
for i=1,
s1=0; s2=0; s3=0;
for t=1:T,
s1 = s1 + h(t,j)*Y(t,i);
s2 = s2 + h(t,j);
end
mu(j,i) = s1/s2;
P(j) = s2/T;
s3=0;
for t=1:T,
s3 = s3 + h(t,j)*(Y(t,i)-mu(j,i))^2;
end
sigmas(j,i) = s3/s2;
end
end
end
function y = Gaussian(x, mu, Sigma2);
r2 = ((double(x(1)) - mu(1))^2)/(Sigma2(1)) + ((double(x(2)) -
mu(2))^2)/(Sigma2(2));
y2 = exp(- r2)/(2. * 3.1416 * sqrt(Sigma2(1) * Sigma2(2)));
y = y2;