Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mean and variance of image in single pass

am trying to calculate mean and variance using 3X3 window over image(hXw) in opencv...here is my code...is there any accuracy issues with this??or is there any other efficient method to do it in one pass.?

int pi,a,b;

for(i=1;i<h-1;i++)
{
    for(j=1;j<w-1;j++)
    {   int sq=0,sum=0;
        double mean=0;
        double var=0;
        for(a=-1;a<=1;a++)
        {
            for(b=-1;b<=1;b++)
            {
                pi=data[(i+a)*step+(j+b)];
                sq=pi*pi;
                sum=sum+sq;
                mean=mean+pi;
            }
        }
        mean=mean/9;
        double soa=mean*mean;//square of average
        double aos=sum/9;//mean of squares
        double var=aos-soa;//variance
    }
}
like image 367
Dark Knight Avatar asked Dec 07 '25 06:12

Dark Knight


1 Answers

With respect to computational efficiency I would recommend doing this in the Fourier domain instead of the time (image) domain using convolutions. Remember, a convolution is a simple multiplication in the Fourier domain. Just like in time series where the spectral density function is the variance decomposed as a function of frequency, one can extend this into two dimensions for an image. Should be much better than nested for-loops.

I don't have the code on me at the moment. but this technique has been used in algorithms like "fast template matching" for object detection or image registration.

like image 107
B. Whitcher Avatar answered Dec 08 '25 19:12

B. Whitcher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!