openCV抓取GC_INIT_WITH_MASK第一次迭代

时间:2014-09-25 15:03:44

标签: c++ opencv image-segmentation

他, 如何用面具初始化第一个Grabcut?在我的情况下每次我都有这个错误:( - 215)!bgdSamples.empty()&& !函数initGMMs中的fgdSamples.empty()。 是否可以使用掩码运行第一个Grabcut迭代?当我使用第一个Grabcut Iteration with Rect时,它已经可以工作了。这是我的带掩码的init代码(第二次迭代):

mask = Mat::ones(image_.size(), CV_8UC1) * GC_PR_BGD; //GC_PR_BGD
// GrabCut segmentation
    grabCut(GrabCut::image,    // input image
            GrabCut::mask,   // segmentation result
            Rect(),// rectangle containing foreground
            GrabCut::bgModel,GrabCut::fgModel, // models
            iteration,       // number of iterations
            GC_INIT_WITH_MASK ); // use mask

FYI 当mode设置为GC_INIT_WITH_RECT时,该函数会初始化掩码。 面具有4个“层”:

GC_BGD defines an obvious background pixels.
GC_FGD defines an obvious foreground (object) pixel.
GC_PR_BGD defines a possible background pixel.
GC_PR_FGD defines a possible foreground pixel.

http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=grabcut#cv2.grabCut

感谢

1 个答案:

答案 0 :(得分:0)

它并不完美,但结果很干净,图像边缘没有矩形。

  1. 第一个Grabcut init with rect
  2. 使用GC_PR_FGD / BGD作为颜色在图像周围绘制一个更大的矩形

    int xyCord = 1;
    int size = 2*xyCord;
        rectangle(GrabCut::mask, Point(xyCord,xyCord),Point(GrabCut::image.cols-size,GrabCut::image.rows-size),Scalar(GC_PR_FGD),30);
        rectangle(GrabCut::mask, Point(xyCord,xyCord), Point(GrabCut::image.cols-size,GrabCut::image.rows-size),Scalar(GC_PR_BGD),30);
    
  3. 带掩码的第二个Grabcut init