Ask Your Question
0

OpenCV Error: Assertion failed using calcHist

asked 2016-04-19 16:40:47 -0600

pollausen85 gravatar image

updated 2016-04-20 05:14:20 -0600

I'm trying to calculate the histogram of a matrix composed by only one row. I wrote this code in VS2015:

//Create matrix stromal pop
    cv::Mat stromalHistogram(1, i_stromalIID.size(), CV_32F);
    float * ref_ptr = stromalHistogram.ptr<float>(0);
    std::copy(i_stromalIID.begin(), i_stromalIID.end(), stdext::checked_array_iterator<float *>(ref_ptr, stromalHistogram.cols));

#ifdef _DEBUG
    std::cout << "Matrix =" << std::endl << stromalHistogram << std::endl;
#endif

    // calculate histogram for stromal pop
    auto itRangesRef = std::minmax_element(i_stromalIID.begin(), i_stromalIID.end());
    float stromalRanges[] = {*itRangesRef.first, *itRangesRef.second};
    const float * rangesRef[] = { stromalRanges };
    const int channel = 0;
    const int histSize = 256;
    std::vector<float> hist_out;
    cv::calcHist(&stromalHistogram, 1, &channel, cv::Mat(), hist_out, 1, &histSize, rangesRef);

i_stromalIID is a std::vector<double> passed from the extern. The cv::Mat stromalHistogram is filled correctly because when I print it, everything is as I expected (1 row, 1204 columns). But when the program runs the cv::calcHist, I receive the following error:

OpenCV Error: Assertion failed (d == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0)) in cv::_OutputArray::create, file D:\Development\lib\OpenCV3.1\opencv-master\modules\core\src\matrix.cpp, line 2363

I tried to debug the OpenCV code, and the error is in cv::calcHist when it tries to do:

void cv::calcHist( const Mat* images, int nimages, const int* channels,
                   InputArray _mask, OutputArray _hist, int dims, const int* histSize,
                   const float** ranges, bool uniform, bool accumulate )
{
      .....
      .....

      _hist.create(dims, histSize, CV_32F);
}

Then inside matrix.cpp is called:

void _OutputArray::create(int d, const int* sizes, int mtype, int i,
                          bool allowTransposed, int fixedDepthMask) const
{
      .....
      .....

      if( k == STD_VECTOR || k == STD_VECTOR_VECTOR )
      {
        CV_Assert( d == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0) );
        .....
        .....
      }
}

And this assert fails because d in my case is equal to 1.

What am I doing wrong?

edit retag flag offensive close merge delete

Comments

1

try cv::Mat hist_out; instead of std::vector<float> hist_out;

sturkmen gravatar imagesturkmen ( 2016-04-20 06:18:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-04-19 17:48:10 -0600

take a look at the documentation of calcHist

it says :

Parameters
    images  Source arrays. They all should have the same depth, CV_8U or CV_32F , and the same size. Each of them can have an arbitrary number of channels.

so your stromalHistogram is not suitable, maybe you could convert it to CV_32F

edit flag offensive delete link more

Comments

Thanks! I did what you suggested, now my code is:

//Create matrix stromal pop
    cv::Mat stromalHistogram(1, i_stromalIID.size(), CV_32F);
    float* ref_ptr = stromalHistogram.ptr<float>(0);
    std::copy(i_stromalIID.begin(), i_stromalIID.end(), stdext::checked_array_iterator<float*>(ref_ptr, stromalHistogram.cols));

and std::vector<double> hist_out now is std::vector<float> hist_out.

Unfortunately nothing changed. Any other suggestions?

pollausen85 gravatar imagepollausen85 ( 2016-04-20 03:47:50 -0600 )edit

try

cv::Mat stromalHistogram;
cv::Mat tmp( 1, i_stromalIID.size(), i_stromalIID, CV_64F);
tmp.convertTo(stromalHistogram, CV_32F);
sturkmen gravatar imagesturkmen ( 2016-04-20 04:37:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-19 16:40:47 -0600

Seen: 3,585 times

Last updated: Apr 20 '16