GrabCut - bgdModel& fgdModel empty - 声明错误 [英] GrabCut - bgdModel & fgdModel empty - Assertion error

查看:4191
本文介绍了GrabCut - bgdModel& fgdModel empty - 声明错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OpenCV2.1(C ++)中尝试使用GrabCut算法进行图像分割

I am attempting image segmentation using GrabCut algorithm in OpenCV2.1 (C++)

这里我的代码:

Mat rgbWorkImage = imread(argv[1]);
Mat mask;
mask = Scalar(0);
Mat bgdModel, fgdModel;
Rect rect = Rect(Point(0,0), imageSize);
grabCut(rgbWorkImage, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_RECT);
grabCut(rgbWorkImage, mask, rect, bgdModel, fgdModel, 2, GC_EVAL);

不幸的是我得到这个运行时错误:

Unfortunately I am getting this runtime error:

OpenCV Error: Assertion failed (!bgdSamples.empty() && !fgdSamples.empty()) in initGMMs, file /build/buildd/opencv-2.1.0/src/cv/cvgrabcut.cpp, line 368
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.1.0/src/cv/cvgrabcut.cpp:368: error: (-215) !bgdSamples.empty() && !fgdSamples.empty() in function initGMMs

我在这里缺少什么?

感谢

推荐答案

错误可能发生的一种情况是,其宽度或高度(但不适用于两者),因为此错误: https://code.ros.org/trac / opencv / ticket / 691 (似乎在OpenCV 2.1之后是固定的)。

One case where that error could happen is when your image has zero for either its width or height (but not for both) because of this bug: https://code.ros.org/trac/opencv/ticket/691 (which seems to be fixed after OpenCV 2.1).

如果图片尺寸不为零, ROI rect:

If the image dimensions are non zero, you should also check that the ROI rect:


  • 不为空( imageSize 不为零大小)

  • is not empty (imageSize has not a zero size) and
  • doesn't cover the entire image.

GC_INIT_WITH_RECT 将给定rect外部的所有像素标记为背景,并且将rect内的所有像素标记为可能是前景,并且断言期望在前景(或可能是前景)和背景或可能的背景)列表。

GC_INIT_WITH_RECT marks all pixels outside the given rect as "background" and all pixels inside the rect as "probably foreground", and the assert expect that there is pixels in both foreground (or "probably foreground") and background (or "probably background") list.

这篇关于GrabCut - bgdModel& fgdModel empty - 声明错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆