MATLAB: Problem with the following.

Image Processing Toolbox

This is my MATLAB code for the following:
F(m, n) = w0I0 + w1I1 + w2I2
where
I0 = I2^α0 (m, n)
I1 = I2^α1 (m 1, n) + I2^α1 (m + 1, n) + I2^α1 (m, n 1) + I2^α1 (m, n + 1)
I2 = I2^α2 (m 1, n 1) + I2^α2 (m + 1, n 1) + I2^α2 (m + 1, n 1) + I2^α2 (m + 1, n + 1)
The value of alpha0,alpha1,alpha2,w0,w1,w2 are declared in code. But it shows some error. I dono why?
code:
[file path] = uigetfile ('*.bmp','Select any Image');
if file ~=0
I = imread (strcat(path,file));
[m n] = size (I);
h=1;
alpha0 = 8*h;
alpha1 = h;
alpha2 = h;
w0 = 2;
w1 = -0.125;
w2 = -0.125;
% Compute I0
for j = 1 : n
for i = 1 : m
I0(i,j) = I(i,j)^(2*alpha0);
end
end
% Compute I1
for j = 1 : n
for i = 1 : m
I1(i,j) = I(i-1,j)^(2*alpha1) + I(i+1,j)^(2*alpha1) + I(i,j-1)^(2*alpha1) + I(i,j+1)^(2*alpha1);
end
end
% Compute I2
for j = 1 : n
for i = 1 : m
I2(i,j) = I(i-1,j-1)^(2*alpha2) + I(i+1,j-1)^(2*alpha2) + I(i+1,j-1)^(2*alpha2) + I(i+1,j+1)^(2*alpha2);
end
end
F(m,n) = (w0*I0(m,n)) + (w1*I1(m,n)) + (w2*I2(m,n));
figure, imshow(F);
end

Best Answer

Always provide the complete error message to the forum, when you get one.
I guess at least this fails:
for i = 1 : m
I1(i,j) = I(i-1,j)^(2*alpha1) + I(i+1,j)^(2*alpha1) ...
This fails for i==1 and i==m, because i-1 and i+1 are no valid indices, respectively. You have to run the loop from 2:m-1, such that I1 has a different size.
Btw.:
  • Overwriting the important function path with a local variable is a bad idea. E.g. the debugging might fail.
  • This code:
% Compute I0
for j = 1 : n
for i = 1 : m
I0(i,j) = I(i,j)^(2*alpha0);
end
end
can be abbreviated to:
I0 = I .^ (2 * alpha0);
  • The comparison of strings should not be done by == or ~=, although this works accidently in your example: "if file ~=0". Better use if ischar(file) or if ~isequal(file, 0).