Index in position 1 is invalid. Array indices must be positive integers or logical values. i am getting this error for averaging filter

3 次查看(过去 30 天)
i=imread('Lena512.png');
[m n]=size(i)
mask=ones(3,3)/9;
in=zeros([m n]);
for k=1:1:m-1
for l=1:1:n-1
t=i(k-1,l-1)*mask(1,1)+i(k-1,l)*mask(1,2)+i(k-1,l+1)*mask(1,3)+i(k,l-1)*mask(2,1)+i(k,l)*mask(2,2)+i(k,l+1)*mask(2,3)+i(k+1,l-1)*mask(3,1)+i(k+1,l)*mask(3,2)+i(k+1,l+1)*mask(3,3)
in(k,l)=t
im2double(in)
end
end
imshow(k)
imshow(in)

采纳的回答

Kevin Holly
Kevin Holly 2022-10-18
编辑:Kevin Holly 2022-10-18
i=imread('peppers.png');
[m n]=size(i)
m = 384
n = 1536
mask=ones(3,3)/9;
in=zeros([m n]);
for k=1:1:m-1
for l=1:1:n-1
t=i(k-1,l-1)*mask(1,1)+i(k-1,l)*mask(1,2)+i(k-1,l+1)*mask(1,3)+i(k,l-1)*mask(2,1)+i(k,l)*mask(2,2)+i(k,l+1)*mask(2,3)+i(k+1,l-1)*mask(3,1)+i(k+1,l)*mask(3,2)+i(k+1,l+1)*mask(3,3)
in(k,l)=t
im2double(in)
end
end
Index in position 1 is invalid. Array indices must be positive integers or logical values.
The problem above occurs when
k = 1
and
l = 1
When looking at a specific index in the matrix i,
i(k-1,l-1)
you get
i(0,0)
which does not exist.
You could try:
for k=2:1:m-1
for l=2:1:n-1
t=i(k-1,l-1)*mask(1,1)+i(k-1,l)*mask(1,2)+i(k-1,l+1)*mask(1,3)+i(k,l-1)*mask(2,1)+i(k,l)*mask(2,2)+i(k,l+1)*mask(2,3)+i(k+1,l-1)*mask(3,1)+i(k+1,l)*mask(3,2)+i(k+1,l+1)*mask(3,3)
in(k,l)=t
im2double(in)
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by