Local variance estimation for image filtering

19 次查看(过去 30 天)
Hi,
while implementing median filter, medfilt2(), why does medfilt2(I.^2) and medfilt2(I).^2 produce same result?
I am interested in calculating local variance , but using this methodology it is exactly 0.
How do I implement the formula correctly?

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2022-6-21
编辑:Bjorn Gustavsson 2022-6-21
You get that result because medfilt2 will select the median value of the pixel-intensities of your region, and completely discard all the other values. Therefore when you square that median value you get medfilt2(I).^2. Since x^2 is a monotnous function the element that is median of x, will also be the median element of x.^2, and that's why you always get zero's. Simple illustrating example:
x = [1 3 4 412 -3]
x2 = x.^2
Q0 = median(x)
Q1 = median(x).^2
Q2 = median(x2)
Have a look at the code of wiener2 to see how it is done with local averaging filtering. If you want a different variance-type operation you could try mad instead of std (you have to figure out how you want to handle the conversion from mad similar to the conversion between std and var). Or you could try something like:
localMedian = medfilt2(x,nhood,'symmetric');
localMEDVar = medfilt2((x-localMedian).^2,nhood,'symmetric');
% or:
localMADVar = (filter2(ones(nhood), abs(x-localMedian) ) / prod(nhood)).^2;
You have so many options to calculate some measur of the local intensity variations. Which one suits your needs best is difficult to judge. At least you now know why you get the all-zeros result from your first-stab.
HTH
  7 个评论
Albert Tagtum
Albert Tagtum 2022-6-24
Thank you Bjorn for very detailed answers and proposals of solutions!
Much appreciated.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2022-6-21
Try stdfilt if you have the Image Processing Toolbox. It gives the standard deviation in a local window.

Community Treasure Hunt

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

Start Hunting!

Translated by