views:

605

answers:

1

Hi, 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.

+1  A: 

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

Andres
Thanks a lot. Your suggestion gives me some more ideas, how to deal with this.Thank you!
Michelle
You are welcome, and just to help me improve my score, can you vote in this answer :-)
Andres