2014-02-14 40 views
0

我想學習equalization of histograms,當然我知道有histogram equalization in OpenCV。我由calcHist返回,我不知道這是否是正確的方式...或者有其他方法。首先,calcHist是否會返回浮標或雙打或整數的Mat?我似乎無法在文檔中找到它。迭代calcHist返回的Mat

int histSize = 256; 
float range[] = {0, 256} ; 
const float* histRange = { range }; 

Mat histogram; 
calcHist(&image, 1, 0, Mat(), histogram, 1, &histSize, &histRange); 

Mat accumulatedHistogram = histogram.clone(); 
MatIterator_<float> accHistIt, accHistEnd; 
accHistIt=accumulatedHistogram.begin<float>(); 
accHistEnd=accumulatedHistogram.end<float>(); 

bool firstLoop = true; 

for(; accHistIt != accHistEnd; accHistIt++) { 
    if(firstLoop) {      
     firstLoop = false; 
    } else { 
     *accHistIt += *(accHistIt-1); 
    } 
} 

感謝,

+0

(accHistIt -1)肯定會導致第一次迭代中的緩衝區不足。你想達到什麼目的? – berak

+0

我想計算一下:AH = clone(H);循環{AH [i] = AH [i] + AH [i-1]; }其中AH =積分曲線,H =直方圖。正如您在[維基百科中的直方圖均衡的累積分佈函數(cdf)示例]中所看到的(http://en.wikipedia.org/wiki/Histogram_equalization#Small_image)。我希望我已經說清楚了。 – lmiguelmh

回答

1

calcHist將返回floatMat。雖然類型沒有很好的文檔記錄,但您可以通過查看the documentation access its values如何輕鬆猜出它是什麼。

如果image是單通道圖像,calcHist將計算histSize x 1float矩陣,在你的例子histogram。請注意,histSize通常稱爲number of bins

要遍歷所有的值,你可以這樣做:

for (int i=0; i<histSize; i++) 
    cout << histogram.at<float>(i, 0)); 

注:對於像RGB 3通道圖像,你可以做如下:

vector<float> result; 

/// Separate the image in 3 places (B, G and R) 
vector<Mat> bgr_planes; 
split(image, bgr_planes); 

/// Establish the number of bins 
int histSize = 256; 

/// Set the ranges (for B,G,R)) 
float range[] = { 0, 256 } ; //0~255 the upper boundary is exclusive 
const float * histRange = { range }; 
bool uniform = true; 
bool accumulate = false; 
Mat b_hist, g_hist, r_hist; 

/// Compute the histograms: 
calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate); 
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate); 
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate); 

/// stored in result 
for (int i=0; i<histSize; i++) 
    result.push_back(r_hist.at<float>(i, 0)); 
for (int i=0; i<histSize; i++) 
    result.push_back(g_hist.at<float>(i, 0)); 
for (int i=0; i<histSize; i++) 
    result.push_back(b_hist.at<float>(i, 0));