# Filter an image based on subset

I try to filter an image using this equation: $$I_f(x_j, y_k, t)=\sum_{p=-q}^q\sum_{r=-q}^q I(x_{j+p}, x_{k+r}, t)$$

where $I$ is the original image in grayscale, $I_f$ is the filtered image and $d$ is the subset. Here, I chose 3. However, I'm confused what will happen when $j=1$, $k=1$, and $p=-q$, $r=-q$.

Looping in MATLAB will result in error:

I=rand(240, 320);
[m, n]=size(I);
I_f=zeros(m,n);
d=3;
q=(d-1)/2;
for x=q:n
for y=q:m
for r=-q:q
for p=-q:q
I_f(x,y)=I(x+p,y+r);
end
end
end
end


How to do it correclty in matlab? Is it possible to do it without looping? Thanks in advnace!

• The equation, simply applies a summation mask on the image. It seems you've misunderstand the image filtering. Simply use this code , "q=10;p=10;Mask=ones(q,q);I_f=imfilter(I,Mask);" .You need study image spatial operations. Mar 23, 2017 at 7:01
• Thanks for the tip. Sorry I'm quite new in image processing. Anyway, I tweaked the code accordingly. d=3;Mask=ones(d,d)/9;I_f=imfilter(I,Mask, 'replicate'); Mar 23, 2017 at 7:47

q=10;