OpenCV错误:使用calcHist断言失败

时间:2016-04-20 08:56:54

标签: c++ opencv visual-studio-2015

我试图计算仅由一行组成的矩阵的直方图。我使用OpenCV3.1在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是从外部传递的std::vector<double>cv::Mat stromalHistogram被正确填充,因为当我打印它时,一切都如我所料(1行,1203列)。但是当程序运行cv::calcHist时,我收到以下错误:

  

OpenCV错误:断言失败(d == 2&amp;&amp;(sizes [0] == 1 || sizes [1]   == 1 || size [0] * sizes [1] == 0))在cv :: _ OutputArray :: create中,文件D:\ Development \ lib \ OpenCV3.1 \ opencv-master \ modules \ core \ src \ matrix.cpp,   第2363行

我尝试调试OpenCV代码,错误在cv::calcHist时尝试执行:

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

然后调用matrix.cpp内部:

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

这个断言失败了,因为在我的情况下d等于1。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

将Mat尺寸更改为源图像。

答案 1 :(得分:0)

您传递的昏暗值为1,而断言它为2失败。