MATLAB: What’s the problem in the loop

for loopimage processing

A=imread('cameraman.tif');
B=zeros(255,255);
for j=0:255
for k=0:255
B(j,k)=A(j,k+1)-A(j,k-1);
end
end
figure,imshow(B);
error: Attempted to access A(0,1); index must be a positive integer or logical.
Error in tryfebin (line 6)
B(j,k)=A(j,k+1)-A(j,k-1);

Best Answer

Your code should go like this:
A=imread('cameraman.tif');
B=zeros( size(A) );
for j=1:size(A,1)
for k=2:(size(A)-1) %edge pixels of B matrix has to be treated differently
B(j,k)=A(j,k+1)-A(j,k-1); %you can not apply this algorithm to edge pixels
end
end
figure,imshow(B);
Related Question