gpt4 book ai didi

c++ - cvCalcHist 和 calcHist 的区别

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:48:54 25 4
gpt4 key购买 nike

我试图了解 cvCalcHist 之间的区别,openCV c 版本calcHist 中计算图像直方图的方法>,openCV c++ version 中计算图像直方图的方法。

openCV c版本代码:

    // load the color image
IplImage* im = cvLoadImage("2.png");

// get the color histogram
IplImage* im32f = cvCreateImage(cvGetSize(im), IPL_DEPTH_32F, 3);
cvConvertScale(im, im32f);

int histSize[] = {32, 32, 32};
float rgbRange[] = {0, 256};
float* ranges[] = {rgbRange, rgbRange, rgbRange};

CvHistogram* hist = cvCreateHist(3, histSize, CV_HIST_ARRAY, ranges);
IplImage* b = cvCreateImage(cvGetSize(im32f), IPL_DEPTH_32F, 1);
IplImage* g = cvCreateImage(cvGetSize(im32f), IPL_DEPTH_32F, 1);
IplImage* r = cvCreateImage(cvGetSize(im32f), IPL_DEPTH_32F, 1);

cvSplit(im32f, b, g, r, NULL);
IplImage* planes[] = {b, g, r};
cvCalcHist(planes, hist);

// find min and max values of histogram bins
float minval, maxval;
cvGetMinMaxHistValue(hist, &minval, &maxval);
cout << "Min : " << minval << " / Max : " << maxval << endl;

//OUTPUT: Min : 0 / Max : 177617

openCV c++版本代码:

    const int channels[] = {0, 1, 2};
const int histSize[] = {32, 32, 32};
const float rgbRange[] = {0, 256};
const float* ranges[] = {rgbRange, rgbRange, rgbRange};
Mat im = imread("2.png",IMREAD_COLOR);
MatND hist;
Mat im32fc3, backpr32f(im.cols, im.rows, CV_32F), backpr8u(im.cols, im.rows, CV_8U), backprBw;
im.convertTo(im32fc3, CV_32F);

// compute histogram, scale it to 0-255 range and backproject
calcHist( &im32fc3, 1, channels, Mat(), hist, 1, histSize, ranges, true, false);
// find min and max values of histogram bins
double minval, maxval;
cv::minMaxIdx(hist, &minval, &maxval);
cout << "Min : " << minval << " / Max : " << maxval << endl;

//OUTPUT: Min : 455 / Max : 476732

正如您在上面代码部分的 //OUTPUT 注释中看到的,两段代码的值不同,我认为我应该得到相同的结果。知道发生了什么事吗?

最佳答案

由于您在 B、G、R 中创建 3D 直方图,您需要告诉 calcHist 正确的维数:3。所以基本上您需要更改:

calcHist( &im32fc3, 1, channels, Mat(), hist, 1, histSize, ranges, true, false);
// ^
// one dimension

为此具有正确的维度数:

calcHist( &im32fc3, 1, channels, Mat(), hist, 3, histSize, ranges, true, false);
// ^
// three dimensions

关于c++ - cvCalcHist 和 calcHist 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32951334/

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