我正在尝试计算仅由一行组成的矩阵的直方图。我使用 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 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/