GrabCut (cv::grabCut). init_mode picks rectangle (a box containing the target) or mask (rough fg/bg marks); iterated energy minimization refines a precise foreground mask. For high-quality portraits prefer the matting node.

Module:OpenCV segmentation · Task type:opencv_grabcut

Ports

Inputs

PortTypeRequired
inImage / cv::Mat✓
maskImage / cv::Mat—

Outputs

PortTypeRequired
outcv::Mat✓

Parameters

参数类型默认值范围 / 选项说明
init_modeenum0RECT / MASK
rect_xint0[0, 16384]
rect_yint0[0, 16384]
rect_widthint0[0, 16384]
rect_heightint0[0, 16384]
iterationsint5[1, 100]number of repetitions

Example graph

A minimal runnable pipeline (read → process → write). Drag it into GraphStudio or open via --open; replace data/test.png with a real path.

{
  "version": "2.0",
  "tasks": [
    {
      "id": "read1",
      "type": "opencv_image_read",
      "params": {
        "file_path": "data/test.png"
      }
    },
    {
      "id": "read2",
      "type": "opencv_image_read",
      "params": {
        "file_path": "data/test.png"
      }
    },
    {
      "id": "node",
      "type": "opencv_grabcut",
      "params": {
        "init_mode": 0,
        "rect_x": 0,
        "rect_y": 0,
        "rect_width": 0,
        "rect_height": 0,
        "iterations": 5
      }
    },
    {
      "id": "save",
      "type": "opencv_image_write",
      "params": {
        "file_path": "out_out.png"
      }
    }
  ],
  "edges": [
    {
      "from": "read1",
      "from_port": "out",
      "to": "node",
      "to_port": "in"
    },
    {
      "from": "read2",
      "from_port": "out",
      "to": "node",
      "to_port": "mask"
    },
    {
      "from": "node",
      "from_port": "out",
      "to": "save",
      "to_port": "in"
    }
  ]
}

Notes

  • RECT mode uses rect_x/y/width/height; MASK mode ignores them.
  • More iterations refine edges and cost time; 5-10 usually suffices.

Part of the GraphStudio node guide series; parameters reflect the latest release.