MATLAB: Estimating Value of Z

digital image processingdisparityimage processing

[EDIT: 20110519 14:00 CDT – reformat – WDR]
Hi all ,
I am trying to estimate the value of Z for each pixel value of Disp image . When I am executing it , It stuck in infinite loop . Here Z= (focallength*baseline)/Disp so that in each iteration i can estimate a value of Z for the corresponding Disparity pixel value .
focallength = 1.0 ;
baseline = 10.0 ;
Disp=imread('D1_000000.png');
Disp=(double(Disp)/.256);
[height width] = size(Disp);
%X = zeros (height,width);
%Y = zeros (height,width);
Z = zeros (height,width);
for j=1:height
for i=1:width
if (Disp(j,i)==0)
Disp(j,i)=NaN;
X(j,i)=NaN;
Y(j,i)=NaN;
Z(j,i)=NaN;
else
Z = (focallength * baseline )./ Disp(j,i)
end
end
end

Best Answer

Each iteration of the loop, if the disp is non-zero, you replace all of Z by a single scalar value.
Perhaps you want to be assigning to Z(j,i)
It is not clear to me why you would NaN out X and Y if Disp is 0 there? Especially since X an Y are not otherwise created?
It appears to me the entire loop could be replaced by
Z = (focallength * baseline) ./ Disp;
Division by 0 does not create an error message, it creates a NaN, which is exactly what you would store otherwise.