gpt4 book ai didi

c++ - 多 channel 反投影断言(j < nimages)

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:58 24 4
gpt4 key购买 nike

尝试在三 channel 图像上进行直方图反投影会导致以下错误:

OpenCV Error: Assertion failed (j < nimages) in histPrepareImages, file ../modules/imgproc/src/histogram.cpp, line 148

失败的代码:

cv::Mat _refImage; //contains reference image of type CV_8UC3
cv::Mat output; //contains image data of type CV_8UC3
int histSize[] = {16, 16, 16};
int channels[] = {0, 1, 2};
const float hRange[] = {0.f, 256.f};
const float* ranges[] = {hRange, hRange, hRange};
int nChannels = 3;

cv::Mat hist;
cv::calcHist(&_refImage, 1, channels, cv::noArray(), hist, nChannels, histSize, ranges);

cv::calcBackProject(&output, 1, channels, hist, output, ranges); //This line causes assertion failure

在单 channel 图像上运行几乎相同的代码是可行的。根据文档,还支持多 channel 图像。为什么这段代码不起作用?

最佳答案

简短的回答是 cv::calcBackProject() 不支持就地操作,尽管文档中没有提到这一点。

解释

深入研究 OpenCV 源代码会得到以下片段:

void calcBackProject( const Mat* images, int nimages, const int* channels,
InputArray _hist, OutputArray _backProject,
const float** ranges, double scale, bool uniform )
{
//Some code...

_backProject.create( images[0].size(), images[0].depth() );
Mat backProject = _backProject.getMat();
assert(backProject.type() == CV_8UC1);
histPrepareImages( images, nimages, channels, backProject, dims, hist.size, ranges,
uniform, ptrs, deltas, imsize, uniranges );
//More code...
}

导致问题的行是:

_backProject.create( images[0].size(), images[0].depth() );

如果源和目标相同,则重新分配输入图像数据。 images[0].depth() 的计算结果为 CV_8U,它在数值上等同于类型说明符 CV_8UC1。因此,数据被创建为单 channel 图像。

这是一个问题,因为 histPrepareImages 仍然期望输入图像有 3 个 channel ,并且断言被抛出。

解决方案

幸运的是,解决方法很简单。输出参数必须与输入不同,如下所示:

cv::Mat result;
cv::calcBackProject(&output, 1, channels, hist, result, ranges);

关于c++ - 多 channel 反投影断言(j < nimages),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16840968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com