不推荐使用cvGetHistValue_1D。用什么代替?

时间:2013-05-03 08:37:23

标签: c++ opencv

根据最新的OpenCV(OpenCV 2.4.5)文档,{imgproc模块已弃用cvGetHistValue_1D,现在它已成为legacy模块的一部分。

如果我不打算使用cvGetHistValue_1D模块,我想知道应该使用什么代替legacy

我之前的代码如下,需要在不使用cvGetHistValue_1D

的情况下重写
CvHistogram *hist = cvCreateHist(1, &numBins, CV_HIST_ARRAY, ranges, 1);
cvClearHist(hist);

cvCalcHist(&oDepth,hist);
cvNormalizeHist(hist, 1.0f);
float *cumHist = new float[numBins];
cumHist[0] = *cvGetHistValue_1D(hist, 0);
for(int i = 1; i<numBins; i++)
{
    cumHist[i] = cumHist[i-1] + *cvGetHistValue_1D(hist, i);

    if (cumHist[i] > 0.95)
    {
        oMaxDisp = i;
        break;
    }
}

1 个答案:

答案 0 :(得分:1)

我假设您对使用C ++ API感兴趣。使用cv::Mat::at()可以轻松完成矩阵元素访问。

您的代码可能如下所示:

cv::Mat image;    //Already in memory
size_t oMaxDisp = 0;
cv::Mat hist;

//Setup the histogram parameters
const int channels = 0;
const int numBins = 256;
const float rangevals[2] = {0.f, 256.f};
const float* ranges = rangevals;

cv::calcHist(&image, 1, &channels, cv::noArray(), hist, 1, &numBins, &ranges);
cv::normalize(hist, hist,1,0,cv::NORM_L1);

float cumHist[numBins];
float sum = 0.f;
for (size_t i = 0; i < numBins; ++i)
{
    float val = hist.at<float>(i);
    sum += val;
    cumHist[i] = sum;

    if (cumHist[i] > 0.95)
    {
        oMaxDisp = i;
        break;
    }
}

作为旁注,除非有必要,否则不要使用new