Local variance estimation for image filtering

24 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Bjorn Gustavsson
Bjorn Gustavsson il 21 Giu 2022
Modificato: Bjorn Gustavsson il 21 Giu 2022
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 Commenti
Albert Tagtum
Albert Tagtum il 24 Giu 2022
Thank you Bjorn for very detailed answers and proposals of solutions!
Much appreciated.
Bjorn Gustavsson
Bjorn Gustavsson il 24 Giu 2022
My pleasure, happy that it helped.

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 21 Giu 2022
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