繁体   English   中英

OpenCV错误:使用calcHist断言失败

[英]OpenCV Error: Assertion failed using calcHist

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

OpenCV错误:断言失败(d == 2 &&(sizes [0] == 1 || sizes [1] == 1 || sizes [0] * sizes [1] == 0))在cv :: _ OutputArray中: :创建,文件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。

我究竟做错了什么?

将“垫”大小更改为源图像。

您将dims传递为1,断言它为2失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM