Click here to Skip to main content
15,881,424 members
Articles / All Topics
Technical Blog

Integral Image for Mean and Variance Computation

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 Jan 2014CPOL1 min read 9.4K   1  
Integral image for mean and variance computation
<link href=""https://googledrive.com/host/0B-pfqaQBbAAtenZiNDItSDFTSTg/common.css"" rel=""stylesheet"" type=""text/css"/" />

Introduction

  • The Integral Image is used as a quick and effective way of calculating the sum of values (pixel values) in a given image – or a rectangular subset of a grid (the given image).
  • In this post, we will assume that concepts of integral image are known and then proceed to see how it can be used to compute the mean and variance of an image patch.
  • Given an integral representation of an image, the sum of value of pixels in the rectangular region R with vertices A,B,C,D is given by \[ I = S(A) +S(D) -S(B) -S(C) \]
  • Dividing this quantity by the number of pixels gives us the mean value of pixels in the region. \[ \mu = \frac{I}{N} \]
  • Let us also consider the squared integral image.To obtain this, all the pixel values in the image are squared then integral image is computed.
  • Consider the variance about a rectangulation regions \[ v= \sum_i (x_i-\mu)^2 v= \sum_i x_i^2 - 2\sum_i x_i \mu + \mu^2 v= \sum_i x_i^2 - \mu^2 \] The summation $x_i^2$ can obtained by the square integral image and $\mu$ can be obtained by integral image computation.
  • This enables us to compute the variance of rectangular patch of image.
  • A similar method can be employed to compute the denominator variance term normalize cross correlation formula.
    C++
    <script class="brush: cpp" type="syntaxhighlighter"> 
    <![CDATA[       #include "integralImage.h" class IntegralImage 
    { 
    public:  Mat _integral;  //integral image Mat _sq_integral; 
    //sq integral image Mat _image;  
    //original image IntegralImage();  
    //function to compute integral image 
    void compute(Mat image); 
    //function to compute mean value of a patch 
    float calcMean(Rect r);  
    //function to compute variance of a patch 
    float calcVariance(Rect r); }; 
    IntegralImage::IntegralImage() { }  
    void IntegralImage::compute(Mat image) 
    { image.copyTo(_image); cv::integral(_image,_integral,_sq_integral); }  
    float IntegralImage::calcMean(Rect r) 
    {            
    int width=_integral.cols;            
    int height=_integral.rows;            
    unsigned int *ii1 =(unsigned int *)_integral.data;            
    int a=r.x+(r.y*width);            
    int b=(r.x+r.width)+(r.y*width);            
    int c=r.x+((r.y+r.height)*width);            
    int d=(r.x+r.width)+(r.y+r.height)*width;            
    float mx=ii1[a]+ii1[d]-ii1[b]-ii1[c];            
    mx=mx/(r.width*r.height);        
    return mx;  }  
    float IntegralImage::calcVariance(Rect r) {         
    int width=_integral.cols;         
    int height=_integral.rows;            
    int a=r.x+(r.y*width);            
    int b=(r.x+r.width)+(r.y*width);            
    int c=r.x+((r.y+r.height)*width);            
    int d=(r.x+r.width)+(r.y+r.height)*width;            
    float mx=calcMean(r);            
    double *ii2 = (double *)_sq_integral.data;            
    float mx2=ii2[a]+ii2[d]-ii2[b]-ii2[c];            
    mx2=mx2/(r.width*r.height);            
    mx2=mx2-(mx*mx);            
    return mx2; }  </script>  
  • The above code can be found in git repo https://github.com/pi19404/OpenVision/tree/master/ImgFeatures/integralImage.cpp and ImgFeatures/integralImage.hpp files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student IIT Bombay
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --