Vishal
Vishal

Reputation: 3296

cvGetHistValue_1D is deprecated. What is to be used instead?

According to latest OpenCV (OpenCV 2.4.5) documentation, cvGetHistValue_1D has been deprecated from imgproc module, and is now part of the legacy module.

I would like to know what should be used instead of cvGetHistValue_1D if I do not plan to use the legacy module.

My previous code is as under, which needs to be rewritten without the use of 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;
    }
}

Upvotes: 1

Views: 518

Answers (1)

Aurelius
Aurelius

Reputation: 11329

I am assuming you are interested in using the C++ API. Matrix element access is easily accomplished using cv::Mat::at().

Your code might then look like this:

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

As a side note, it's a good idea not to use new unless it is necessary.

Upvotes: 1

Related Questions