我正在尝试计算仅由一行组成的矩阵的直方图。我使用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代码,尝试执行时错误在
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大小更改为源图像。