Index in position 1 is invalid. Array indices must be positive integers or logical values. i am getting this error for averaging filter
3 views (last 30 days)
Show older comments
Rishith Reddy
on 18 Oct 2022
Commented: Rishith Reddy
on 18 Oct 2022
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)
0 Comments
Accepted Answer
Kevin Holly
on 18 Oct 2022
Edited: Kevin Holly
on 18 Oct 2022
i=imread('peppers.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
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
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!