gpt4 book ai didi

c++ - 遍历 calcHist 返回的 Mat

转载 作者:行者123 更新时间:2023-11-28 07:05:51 25 4
gpt4 key购买 nike

我正在努力学习equalization of histograms , 我当然知道有 histogram equalization in OpenCV .我是iterating over the Mat objectcalcHist 返回而且我不知道这是否是正确的方法......或者还有其他方法。首先,calcHist返回一个由 float 、 double 或整数组成的垫子?我似乎无法在文档中找到它。

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 将返回 Matfloat 值。虽然类型没有很好的记录,但您可以通过查看 the documentation access its values 的方式轻松猜出它是什么。 .

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

要迭代它的所有值,您可以这样做:

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

注意:对于像RGB这样的3 channel 图像,可以按如下方式进行:

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

关于c++ - 遍历 calcHist 返回的 Mat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21784910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com