Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to smooth a histogram?

I want to smooth a histogram.

Therefore I tried to smooth the internal matrix of cvHistogram.

typedef struct CvHistogram
{
    int     type;
    CvArr*  bins;
    float   thresh[CV_MAX_DIM][2]; /* for uniform histograms */
    float** thresh2; /* for non-uniform histograms */
    CvMatND mat; /* embedded matrix header for array histograms */
}

I tried to smooth the matrix like this:

cvCalcHist( planes, hist, 0, 0 ); // Compute histogram
(...)

// smooth histogram with Gaussian Filter
cvSmooth( hist->mat, hist_img, CV_GAUSSIAN, 3, 3, 0, 0 );

Unfortunately, this is not working because cvSmooth needs a CvMat as input instead of a CvMatND. I couldn't transform CvMatND into CvMat (CvMatND is 2-dim in my case).

Is there anybody who can help me? Thanks.

like image 263
Michelle Avatar asked Jan 23 '23 22:01

Michelle


1 Answers

You can use the same basic algorithm used for Mean filter, just calculating the average.

for(int i = 1; i < NBins - 1; ++i)
{
    hist[i] = (hist[i - 1] + hist[i] + hist[i + 1]) / 3;
}

Optionally you can use a slightly more flexible algorithm allowing you to easily change the window size.

int winSize = 5;
int winMidSize = winSize / 2;

for(int i = winMidSize; i < NBins - winMidSize; ++i)
{
    float mean = 0;
    for(int j = i - winMidSize; j <= (i + winMidSize); ++j)
    {
         mean += hist[j];
    }

    hist[i] = mean / winSize;
}

But bear in mind that this is just one simple technique.

If you really want to do it using OpenCv tools, I recommend you access the openCv forum: http://tech.groups.yahoo.com/group/OpenCV/join

like image 143
Andres Avatar answered Jan 25 '23 23:01

Andres