Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV Error: Assertion failed using calcHist

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_64F);
    double * ref_ptr = stromalHistogram.ptr<double>(0);
    std::copy(i_stromalIID.begin(), i_stromalIID.end(), stdext::checked_array_iterator<double *>(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<double> 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

What am I doing wrong? Any help is appreciated! Thanks!

OpenCV Error: Assertion failed using calcHist

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_64F);
    double CV_32F);
    float * ref_ptr = stromalHistogram.ptr<double>(0);
stromalHistogram.ptr<float>(0);
    std::copy(i_stromalIID.begin(), i_stromalIID.end(), stdext::checked_array_iterator<double 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<double> 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? Any help is appreciated! Thanks!wrong?