I have a MxN image matrix in, and need to get out matrix out size MxN as follows:

Each point out[i,j] is the average of all in[i,j] inside a window size WxW (if boundary allows). For example if W=10

out[10,10] is average of in[5:15,5:15]

out[1,1] is average of in[1:5,1:5]

This can be implemented as a correlation with ones(1:W,1:W)/W^2, but at the boundary the result is not correct. How should I apply the convolution? I tried pad zeros, reflect but it's not correct yet

  • 1
    $\begingroup$ This is the same issue you would have with linear convolution. I don't think there's a "right" answer. Padding with zeros seems sensible to me. You could also pad with an average edge value. Which approach is preferred I think depends on the application. $\endgroup$ – Dave C Feb 21 '13 at 19:23
  • 1
    $\begingroup$ Your example uses windows of size $11\times 11$, not $10\times 10$ as you claim, giving an average of $121$ pixel values, not $100$ pixel values. Also, the problem with zero padding is that out[1,1] will add $36$ image values and $121-36=85$ zeroes from the padding, and then divide by $121 (or $100$ if you prefer) so that the average is not quite what you want. There will also be edge effects in addition to corner effects. So, you could program in the correct divisors for the edges and corners (actually whole borders) and go that way. $\endgroup$ – Dilip Sarwate Feb 21 '13 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.