1

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;
    }
}
Marcelo Cantos
  • 167,268
  • 37
  • 309
  • 353
Vishal
  • 2,711
  • 2
  • 25
  • 39
  • Was my answer helpful? If it was, it is polite to [accept](http://stackoverflow.com/faq#howtoask) the answer. If not, please state how it could be improved. – Aurelius May 07 '13 at 18:48
  • Hey. I really appreciate your response. Actually I am yet to try out your code. I will mark it once I have tried the same. Thanks. – Vishal May 08 '13 at 01:42

1 Answers1

1

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.

Community
  • 1
  • 1
Aurelius
  • 10,121
  • 3
  • 44
  • 68
  • Thanks for the info. Actually I am using a code database written for C++, but which still uses the old OpenCV API. Originally this question was intended for finding a function which would work with old APIs. – Vishal May 23 '13 at 06:28
  • If you need compatibility with the C API, `cv::Mat` has conversions [to](http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-operator-iplimage) and [from](http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-mat) the C structures. Just be aware that the C API is deprecated. – Aurelius May 23 '13 at 14:55
  • Yes. I am aware of the depreciation of old API. We plan to stick with the old API for this old code-base... – Vishal May 24 '13 at 05:07