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

3 Ansichten (letzte 30 Tage)
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)

Akzeptierte Antwort

Kevin Holly
Kevin Holly am 18 Okt. 2022
Bearbeitet: Kevin Holly am 18 Okt. 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

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by