如何通过opencv生成RGB直方图

时间:2015-06-09 01:16:07

标签: python c++ opencv

我想在RGB颜色空间中从图像到3D直方图进行特征提取。我有以下python代码,我想在c ++下完成它:

hist = cv2.calcHist([image], [0, 1, 2], None, self.bins, [0, 256, 0, 256, 0, 256])        
hist = cv2.normalize(hist)
return hist.flatten()

我做了类似的事情:

float range[] = { 0, 255 } ;   
const float* histRange = { range };
int channels[] = {0};
cv::calcHist( &image, 1, channels, cv::Mat(), r_hist, 1, &sizeVector[0], &histRange);
channels[0] = 1;
cv::calcHist( &image, 1, channels, cv::Mat(), g_hist, 1, &sizeVector[0], &histRange);
channels[0] = 2;
cv::calcHist( &image, 1, channels, cv::Mat(), b_hist, 1, &sizeVector[0], &histRange);

但后来我发现它们不一样,因为在python中直方图是基于3D计算的,如何将代码转换为c ++?

1 个答案:

答案 0 :(得分:1)

以下是OpenCv文档中的代码示例:

http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html#code

这里的方法是先拆分三个通道,然后计算每个通道的直方图。

对于3D直方图,请参阅以下答案:

How to access 3D Histogram values in C++ using OpenCV?