Asked by Royi Avital
on 3 Oct 2014

Hello,

I'm looking for an efficient way to average overlapping pixels which are result of sliding window operation.

To explain my self, imagine we have a 2D matrix (Image) - I. We operate a sliding window operation of window size of 5x5. Each window undergoes a transform, operation and then back.

Now, a certain pixel, I(i, j) appears in many windows (Actually 25).

I want to average all those 25 pixels (Same pixel just the result using different window).

This is for example a step in the BM3D algorithm.

Does anyone knows about efficient way to this in MATLAB?

The structure should be something like:

- Create the patches of the image (im2col can do that).
- Apply the operation in the patches.
- Average the same pixels from all patches it was involved in.

Thank You.

Answer by Matt J
on 3 Oct 2014

Edited by Matt J
on 3 Oct 2014

Accepted Answer

Royi Avital
on 3 Oct 2014

Hi,

Assume we have an Image I where [M, N] = size(I).

Moreover, the blocks of size [MM, MM] are result of blockMatrix = im2col(I, [MM, MM], 'sliding').

Does it answer your question?

Thank You.

Matt J
on 3 Oct 2014

indices=reshape(1:M*N,[M,N]);

subs = im2col(indices, [MM, MM], 'sliding');

vals = im2col(I, [MM, MM], 'sliding');

result=accumarray(subs(:),vals(:))./accumarray(subs(:),1);

result=reshape(result,M,N);

Royi Avital
on 4 Oct 2014

I will check it and report back.

You seem to be the vectorization prince in this forum :-).

Thank You.

Sign in to comment.

Answer by Image Analyst
on 3 Oct 2014

Image Analyst
on 3 Oct 2014

That's a different explanation - now you're saying to average all 25 pixels in a single window centered around (i,j) and what you said before was to look at 25 different windows that contain pixel (i,j): "pixel I(i, j) appears in many windows (Actually 25)." Those are different things - different numbers of windows.

What you just described in your comment is simply the convolution!

output = conv2(double(grayImage), ones(5)/25, 'same');

Royi Avital
on 3 Oct 2014

No, I guess I wasn't clear.

Again, think of im2col([5, 5], 'sliding'). You get many patches of 5 x 5.

Lets say we do something on each patch. The result is the filtered patch.

Now, do you agree the same pixel in the original image is now filtered in 25 different patches?

Now, I want to aggregate all those and average them.

Image Analyst
on 4 Oct 2014

Sign in to comment.

Answer by Royi Avital
on 27 May 2017

Edited by Royi Avital
on 27 May 2017

Another option would be:

I = reshape(accumarray(mIdx(:), mZ(:), [(M * N), 1], @(x) mean(x)), [M, N]);

Where `mZ` is the result of Image To Column and `mIdx` is the result:

mI = im2col(reshape(1:(M * N), [M, N]), modeOfOperation);

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comments

## Matt J (view profile)

## Direct link to this comment

https://kr.mathworks.com/matlabcentral/answers/157285-averaging-overlapping-pixels-in-sliding-window-operation#comment_240741

Sign in to comment.