how to slid filter window of 3*3 over the pixels of image.

2 views (last 30 days)
can anyone plz tell me how to slid a filter window of 3*3 over the pixels of an image one by one.Suppose i have and image with pixels
A= 44 25 22 55 21 11 10 23 ; 35 43 2 23 45 66 86 97 ; 12 22 19 12 76 58 56 23 ; 21 23 43 12 65 75 55 75 ;
I want to slide a filter mask/window of 3*3 over these pixels of image .let the mask be
Mask= 1 1 1 ; 1 1 1 ; 1 1 1 ; plz tell me the coding how to slid this mask over pixels one by one. I have also attached PDF version if anyone know how to do the coading then plz help.

Accepted Answer

Mohammad Abouali
Mohammad Abouali on 10 Oct 2014
use imfilter() it gives you more option than conv2.
For the border pixels instead of using zeros (as in conv2 case) you can define symmetric, replicate, or even circular boundary condition which gives you better results.
As image Analyst said, Another reason that imfilter is suggested in your case is that conv flips the mask. in your case it doesn't matter because you are using symmetric kernel. if you don't want to flip your kernel you should use cross correlation.
Check here and the graph to see how these two, i.e. convolution and cross-correlation, will produce different output.

More Answers (1)

Geoff Hayes
Geoff Hayes on 10 Oct 2014
Ravneet - try using conv2. Using your example from above, we would do
A = [ 44 25 22 55 21 11 10 23
35 43 2 23 45 66 86 97
12 22 19 12 76 58 56 23
21 23 43 12 65 75 55 75];
Mask = [ 1 1 1
1 1 1
1 1 1];
C = conv2(A,Mask,'same');
where C would be
C =
147 171 170 168 221 239 293 216
181 224 223 275 367 429 430 295
156 220 199 297 432 582 591 392
78 140 131 227 298 385 342 209
The same shape parameter ensures that the output matrix is the same size as A. Note that if the window falls outside of the matrix A, then zeros are used in place (so A is zero-padded).
  1 Comment
Image Analyst
Image Analyst on 10 Oct 2014
imfilter() in the Image Processing Toolbox does the same thing and gives you the option of flipping the kernel or not. For this case (all 1's) it doesn't matter but if you ever change the mask Ravneet to something that's not symmetrical, and wonder why the numbers don't match up, then brush up on the definition of convolution, or flip your mask or use imfilter.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!