2014-02-14 39 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));