c++ - OpenCV 错误 : Assertion failed using calcHist

标签 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_stromalIIDstd::vector<double>从外部传来的。 cv::Mat stromalHistogram填写正确,因为当我打印它时,一切都如我所料(1 行,1203 列)。但是当程序运行时 cv::calcHist ,我收到以下错误:

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

我尝试调试 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。

我做错了什么?

最佳答案

将 Mat 大小更改为源图像。

关于c++ - OpenCV 错误 : Assertion failed using calcHist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738649/

相关文章:

python - 在imshow中用黑光束分开两张图片

c++ - 如何从 vector<Rect> 中删除重复矩形?

c++ - HoughCircles()圆检测OpenCV C++的正确阈值

opencv - 查找图像中指向一个方向的所有线

c++ - Visual Studio 2015 : is a set of const pointers allowed?

nuget-package - 如何在 asp.net 5 应用程序中添加项目引用

Xamarin 不会部署

c++ - OpenMp 根据变量设置并行循环的线程数

c++ - 是否允许并接受用删除标记静态方法?

C++指针类