how to avoid 'index exceeds matrix dimension' error?

filter=[0.25 0.5 0.5 0.5 0.25;0.5 0.75 1 0.75 0.5;0.5 1 2 1 0.5;0.5 0.75 1 0.75 0.5;0.25 0.5 0.5 0.5 0.25];
for i=1:5 for j=1:5 filter(i,j)=filter(i,j)/16; end end
% Performs Smoothing
for v = 3 : m for u = 3 : n sum = 0; for i = -2 : 2 for j = -2 : 2 sum = sum + (I(u+i, v+j) * filter(i+3, j+3)); %here error occurs end end IDx(u,v) = sum; end end

回答(1 个)

filter=[0.25 0.5 0.5 0.5 0.25; ...
...
0.25 0.5 0.5 0.5 0.25]/16;
% Performs Smoothing
for v= 3:m
for u=3:n
sum = 0;
for i = -2 : 2
for j = -2 : 2
sum=sum+(I(u+i,v+j)*filter(i+3, j+3)); %here error occurs
end
end
IDx(u,v) = sum;
end
end
It's not in the filter subscripting so you've blown it w/ I
Having not provided any information for it or m, n, a precise answer isn't possible.
I'd suggest
doc filter2

3 个评论

m & n are basically size of the input image 'I'.
So then
u+i>size(I,1) for i>0
and similarly for v+j
is this smoothing can be achieved by conv2 function? if yes then how?

请先登录,再进行评论。

提问:

2014-5-12

评论:

2014-5-12

Community Treasure Hunt

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

Start Hunting!

Translated by