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

2 vues (au cours des 30 derniers jours)
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)

Réponse acceptée

Kevin Holly
Kevin Holly le 18 Oct 2022
Modifié(e) : Kevin Holly le 18 Oct 2022
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

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by