[英]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.