Averaging Overlapping Pixels in Sliding Window Operation

8 ビュー (過去 30 日間)
Royi Avital
Royi Avital 2014 年 10 月 3 日
編集済み: Royi Avital 2017 年 5 月 27 日
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:
  1. Create the patches of the image (im2col can do that).
  2. Apply the operation in the patches.
  3. Average the same pixels from all patches it was involved in.
Thank You.
  1 件のコメント
Matt J
Matt J 2014 年 10 月 3 日
編集済み: Matt J 2014 年 10 月 3 日
Now, a certain pixel, I(i, j) appears in many windows (Actually 25).
I think you really mean a certain pixel (i,j). The notation I(i,j) would refer to a pixel value, not a pixel location.
I want to average all those 25 pixels (Same pixel just the result using different window).
Conversely, here, I think you mean 25 pixel values. If the pixel in question is the same pixel each time, there shouldn't be 25 of them. You're saying, I assume, that the same pixel location receives a different value from each of the 25 blocks that it belongs to.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2014 年 10 月 3 日
編集済み: Matt J 2014 年 10 月 3 日
Use accumarray(subs,val,[],@mean). I can't specify any further how to construct subs and val, because I don't know in what form your block data exists now. But basically "subs" will contain a list of the pixel positions and "val" the value associated with that pixel. You can have multiple occurrences of the same pixel location listed in subs each with a different val inherited from the different blocks.
  3 件のコメント
Matt J
Matt J 2014 年 10 月 3 日
編集済み: Matt J 2014 年 10 月 3 日
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
Royi Avital 2014 年 10 月 4 日
I will check it and report back.
You seem to be the vectorization prince in this forum :-).
Thank You.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2014 年 10 月 3 日
There can be 25 window locations that contain pixel at location (i,j). Each of those windows has one output value and is placed into an output image at different locations corresponding to the center of the window. To average those output values, you'd use imfilter() or conv2() operating on the output image (not the input image).
  4 件のコメント
Royi Avital
Royi Avital 2014 年 10 月 3 日
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
Image Analyst 2014 年 10 月 4 日
I have a hard time visualizing what im2col does(). What is your overall objective? To blur the image?

サインインしてコメントする。


Royi Avital
Royi Avital 2017 年 5 月 27 日
編集済み: Royi Avital 2017 年 5 月 27 日
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);

Community Treasure Hunt

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

Start Hunting!

Translated by