grabcut


Segment the image into foreground and background using segmentation based on iterative graph

Syntax

BW = grabcut(A,L,ROI)
BW = grabcut(A,L,ROI,foremask,backmask)
BW = grabcut(A,L,ROI,foreind,backind)
BW = grabcut(___,Name,Value)

Description

BW = grabcut(A,L,ROI)将图像A分为前景和背景区域。 标签矩阵L指定图像的子区域。 
ROI是指定初始感兴趣区域的逻辑掩码。
BW = grabcut(A,L,ROI,foremask,backmask)分割图像A,其中前遮罩和后遮罩是分别
将图像中的像素指定为前景和背景的遮罩。
BW = grabcut(A,L,ROI,foreind,backind)分割图像A,其中foreind和backind指定
分别标记为前景和背景的图像中像素的线性索引。
BW = grabcut(___,Name,Value)
使用名称/值对对图像进行分割,以控制分割的各个方面。

Examples

Use Grabcut to segment the background and foreground in the image

clear all
close all
clc
RGB = imread('peppers.png');
%生成标签矩阵。
L = superpixels(RGB,500);
%指定感兴趣的区域并创建遮罩图像。
figure
imshow(RGB)
h1 = drawpolygon(gca,'Position',[72,105; 1,231; 0,366; 104,359;...
        394,307; 518,343; 510,39; 149,72]);
roiPoints = h1.Position;
roi = poly2mask(roiPoints(:,1),roiPoints(:,2),size(L,1),size(L,2));
%执行抓取剪切操作,指定原始图像,标签矩阵和ROI。
BW = grabcut(RGB,L,roi);
figure
imshow(BW)
%创建遮罩的图像。
maskedImage = RGB;
maskedImage(repmat(~BW,[1 1 3])) = 0;
figure;
imshow(maskedImage)

Insert picture description here
Insert picture description here
Insert picture description here
Use Grabcut to segment 3-D images

clear all
close all
clc
load mristack
V = mristack;
%为初始前景和背景种子点创建一个二维蒙版。
seedLevel = 10;
fseed = V(:,:,seedLevel) > 75;
bseed = V(:,:,seedLevel) == 0;
%显示前景和背景种子点。
imshow(fseed)
imshow(bseed)
%将种子点放入空的3D蒙版中。
fmask = zeros(size(V));
bmask = fmask;
fmask(:,:,seedLevel) = fseed;
bmask(:,:,seedLevel) = bseed;
%创建感兴趣的初始区域。
roi = false(size(V));
roi(10:end-10,10:end-10,:) = true;
%生成标签矩阵。
L = superpixels3(V,500);
%执行GrabCut。
bw = grabcut(V,L,roi,fmask,bmask);
%显示3D分割图像。
montage(reshape(bw,size(V)))

Insert picture description here
Insert picture description here
Insert picture description here

Input Arguments

L —label matrix
Numerical array
Label matrix, specified as a numeric array.
ROI—Region of Interest
Logical Array
The region of interest, designated as a logical array. All pixels that define the area of ​​interest are equal to true.
foremask—Foreground mask
logical array
Foreground mask, specified as a logical array.
backmask—Background mask
logical array
Background mask, specified as a logical array.
foreind—the index of the pixel in the foreground.
Vector The index of the pixel in the
foreground, specified as a vector of linear indices.
backind — the index of the pixel in the background
vector The index of the pixel in the
background, specified as a vector of linear indices.
Name-value pair parameter'Connectivity'—
connectivity of connected components
4 | 8 | 6 | 18 | 26
Connectivity of connected components, specified as one of the following values. For 2D images, the default connection is 8, and for 3D images, the default connection is 26.
'MaximumIterations'-Maximum number of iterations
5 (default) | positive scalar
The maximum number of iterations performed by this algorithm. The algorithm can converge to a solution before reaching the maximum number of iterations.

Tips

For double and single images, the range of the captured image is assumed to be [0 1]. For uint16, int16 and uint8 images, grabbing will assume the range to be the full range of the given data type.

For grayscale images, L, the size of the front mask and the back mask must match the size of image A. For color and multi-channel images, the size of L, the front mask and the back mask must be a two-dimensional array, and the first two dimensions are the same as the first two dimensions of image A.

Guess you like

Origin blog.csdn.net/qq_34562355/article/details/109221923