2009-11-25 94 views
3

我想平滑直方圖。如何平滑直方圖?

因此我試圖平滑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 */ 
} 

我試圖掩飾這樣的矩陣:

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

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

不幸的是,這是行不通的,因爲cvSmooth需要CvMat作爲輸入,而不是一個CvMatND。我無法將CvMatND轉換爲CvMat(在我的情況下,CvMatND是2-dim)。

有沒有人可以幫助我?謝謝。

+1

什麼是CvMatND,CvMat?爲什麼cvSmoot需要CvMat?更換cvSmooth。 – 2009-11-25 17:26:47

回答

9

您可以使用與平均過濾器相同的基本算法,只是計算平均值。

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

您也可以選擇使用稍微更靈活的算法,讓您可以輕鬆更改窗口大小。

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; 
} 

但請記住,這只是一個簡單的技術。

如果你真的想使用OpenCV的工具來做到這一點,我建議您訪問的OpenCV論壇:http://tech.groups.yahoo.com/group/OpenCV/join

+0

非常感謝。 你的建議給了我更多的想法,如何處理這個問題。 謝謝! – Michelle 2009-11-27 08:35:32

+0

不客氣,只是爲了幫助我提高自己的分數,你能否在這個答案中投票:-) – Andres 2009-11-27 10:14:53

0

可以大大改變你使用槽的數量改變直方圖的「平滑度」。一個好的經驗法則是,如果你有n個數據點,就有sqrt(n)個分箱。您可以嘗試將此啓發式應用於您的直方圖,並查看您是否獲得了更好的結果。

+0

有趣。這個經驗法則有一些數學基礎嗎? – 2014-11-24 09:11:31