我正在尝试学习equalization of histograms,我当然知道有histogram equalization in OpenCV。我是iterating over the Mat object返回的 calcHist ,我不知道这是正确的方法...还是其他方法。首先, calcHist 是否返回浮点数, double 数或整数的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);
    }
}

谢谢,

最佳答案

calcHist将返回 Mat 值的float。尽管没有很好地记录类型,但是您可以通过查看the documentation access its values的方式来轻松猜出它是什么。

如果image是单通道图像,则calcHist将计算一个histSize x 1 float 矩阵,在您的示例histogram中。请注意,histSize通常称为number of bins

要迭代其所有值,可以执行以下操作:

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

注意:对于3通道图像,例如RGB,您可以执行以下操作:
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));

08-04 03:48