OpenCV GrabCut算法:前景分割和提取

目录

一、OpenCv Grabcut算法:前景提取与分割(Foreground segmentation and extraction)

(一)算法工作原理

(二)opencv函数cv2.grabCut

(三)实现opencv的带有边框的GrabCut初始化算法

(四)输出结果显示

(五)小结


一、OpenCv Grabcut算法:前景提取与分割(Foreground segmentation and extraction)

        利用Mast R-CNN或U-Net生成的掩膜不完美,可以使用GrabCut来清理这些分割掩膜。

本文参考并大致参考了光头哥adrian的博客,原文中用了边界框初始化和掩码初始化两种方法,本文仅复现边界框初始化方法,原文链接为https://www.pyimagesearch.com/2020/07/27/opencv-grabcut-foreground-segmentation-and-extraction/

 

(一)算法工作原理

1、接收输入图像,参数一:想分割的图像中对象的位置的边界框;参数二:一种精确分割图像前景和背景的方法

2、 迭代下列操作

  1. 使用高斯混合模型(gaussian mixture model)估计(estimate)前景和背景颜色分布
  2. 在像素标签上(pixels labels)构造一个马尔可夫随机场(Markov random field)(如前景与背景)。
  3. 应用图切割优化(Graph cut optimization)来达到最终的分割

 

(二)opencv函数cv2.grabCut

grabCut(img, mask, rect, bgdModel, fgdModel, iterCount, mode) ->  mask, bgdModel, fgdModel

参数说明

  • img:输入图像,GrabCut假设它是一个8位的3通道图像(即, BGR信道顺序中的无符号8位整数)。
  • mask:输入/输出掩膜。假设该掩膜是一个单通道图像,数据类型为无符号8位整数。如果你使用边界框初始化(第七个参数mode设为cv2.GC_INIT_WITH_RECT),这个掩膜会自动初始化。否则,GrabCut假设正在执行掩码初始化(第七个参数设为cv2.GC_INIT_WITH_MASK)。
  • rect:包含我们要分割的区域的边框矩形。此参数仅在将模式设置为cv2.GC_INIT_WITH_MASK时使用)。
  • bgdModel: GrabCut内部建模背景时使用的临时数组。
  • fgdModel: GrabCut在建模前景时使用的临时数组。
  • iterCount:GrabCut在对前景和背景建模时执行的迭代次数。迭代次数越多,GrabCut运行的时间越长,理想情况下,结果会更好。
  • model:要么cv2.GC_INIT_WITH_RECT或cv2.GC_INIT_WITH_MASK,这分别取决于你是用一个边框还是一个掩码初始化GrabCut。
  • OpenCV的GrabCut实现返回一个3元组:
  • mask:应用GrabCut后的输出掩模
  • fgdMode:用于建模背景的临时数组(可以忽略此值)
  • fgdModel:用于建模前景的临时数组(同样可以忽略此值)

 

(三)实现opencv的带有边框的GrabCut初始化算法

       边界框的实现方法:将指定在图像中分割的对象的边界框,只要算法生成一个边界框,就可以将它与GrabCut结合使用。边界框的实现方法如下:

  • 手动检查图像并标记边框的(x,y)坐标
  • 利用Haar级联
  • 利用HOG+线性SVM检测目标
  • 利用基于深度学习的对象检测器,如Faster R-CNN,SSDs,YOLO等

代码如下:

  • 初始化GrabCut的边界框初始化方法

       构造参数解析器并解析参数,该脚本处理两个命令行参数

  1. --image:输入图像。在"D:\image目录中使用guangtou.png图像。

  2. --iter:GrabCut迭代要执行的次数,较小的值会导致更快的总体时间,较大的值会导致较慢的运行时间(但理想情况下更好的分割结果)

# import the necessary packages
import numpy as np
import argparse
import time
import cv2
import os
# 构造参数解析器并解析参数
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str,
  default=os.path.sep.join([r"D:\image", "guangtou.png"]),
  help="path to input image that we'll apply GrabCut to")
ap.add_argument("-c", "--iter", type=int, default=10,
  help="# of GrabCut iterations (larger value => slower runtime)")
args = vars(ap.parse_args())
  • 加载输入图像,并为相等大小的掩膜分配空间
# load the input image from disk and then allocate memory for the
# output mask generated by GrabCut -- this mask should hae the same
# spatial dimensions as the input image
image = cv2.imread(args["image"])
mask = np.zeros(image.shape[:2], dtype="uint8")

       创建的掩膜很快就被GrabCut算法的结果填充。

  • 手动定义guangtou.png中的人脸坐标,用矩形选择该区域,前两个参数为矩形左上角顶点,后两个参数为长和宽。

       关于像素点的确定,可以使用Photoshop或GIMP等免费图形编辑软件。这里人脸rect坐标是手动确定的,任何对象检测器都可以完成这项工作,可以选择Haar、HOG或基于dll的面检测器来查找边界。

# define the bounding box coordinates that approximately define my
# face and neck region (i.e., all visible skin)
rect = (80, 20, 140, 230)
  • 执行GrabCut算法,并对输入进行边界框初始化
# allocate memory for two arrays that the GrabCut algorithm internally
# uses when segmenting the foreground from the background
fgModel = np.zeros((1, 65), dtype="float")  #两个空数组,便于在从背景分割前景时使用(fgModel和bgModel)
bgModel = np.zeros((1, 65), dtype="float")
# apply GrabCut using the the bounding box segmentation method
start = time.time()
#GrabCut返回我们填充的掩码以及两个我们可以忽略的数组
(mask, bgModel, fgModel) = cv2.grabCut(image, mask, rect, bgModel,fgModel, iterCount=args["iter"], mode=cv2.GC_INIT_WITH_RECT)
end = time.time()
print("[INFO] applying GrabCut took {:.2f} seconds".format(end - start))
  • 继续对掩膜进行后期处理
# the output mask has for possible output values, marking each pixel
# in the mask as (1) definite background, (2) definite foreground,
# (3) probable background, and (4) probable foreground
values = (
  ("Definite Background", cv2.GC_BGD),
  ("Probable Background", cv2.GC_PR_BGD),
  ("Definite Foreground", cv2.GC_FGD),
  ("Probable Foreground", cv2.GC_PR_FGD),
)
# loop over the possible GrabCut mask values
for (name, value) in values:
  # construct a mask that for the current value
  print("[INFO] showing mask for '{}'".format(name))
  valueMask = (mask == value).astype("uint8") * 255
  # display the mask so we can visualize it
  cv2.imshow(name, valueMask)
  cv2.waitKey(0)

       定义了输出的GrabCut掩膜中可能的值,包括我们确定的/可能的背景和前景。然后继续对这些值进行循环,以便我们可以可视化每个值。在循环中,我们为当前值构造一个掩码,并显示它,直到按下任何键为止。

  • 在确定的/可能的背景和前景已经显示后,我们的代码将开始生成一个outputMask和一个输出图像(背景被掩盖)
# 找到所有确定的背景或可能的背景像素,并将它们设置为0 ,所有其他像素应该标记为1(前景)
outputMask = np.where((mask == cv2.GC_BGD) | (mask == cv2.GC_PR_BGD), 0, 1)
# scale the mask from the range [0, 1] to [0, 255]
outputMask = (outputMask * 255).astype("uint8")
# apply a bitwise AND to the image using our mask generated by
# GrabCut to generate our final output image
output = cv2.bitwise_and(image, image, mask=outputMask)

此时,我们有:

  1. 为grabCut函数准备的输入,包括输入图像、掩模、矩形坐标以及fgModel和bgModel零数组。注意,rect坐标是手动确定的。

  2. 执行GrabCut算法。

  3. 生成并可视化我们确定的/可能的背景和前景腌膜。

  4. 生成GrabCut输出掩膜(outputMask)和带有背景掩膜的输出图像(output)

 

  • 继续并显示我们的最终结果
# show the input image followed by the mask and output generated by
# GrabCut and bitwise masking
cv2.imshow("Input", image)    #原图
cv2.imshow("GrabCut Mask", outputMask)    #GrabCUt生成的掩膜mask
cv2.imshow("GrabCut Output", output)    #只有前景的原始图像
cv2.waitKey(0)

 

(四)输出结果显示

 

(五)小结

       基于深度学习的分割网络,如Faster R-CNN和U-Net,可以自动生成从背景中分割物体(前景)的掩膜——这并不意味着GrabCut在深度学习时代是没意义的。虽然Faster R-CNN和U-Net是超级强大的方法,但它们会导致生成的掩膜混乱粗糙。我们可以用GrabCut来帮助清理这些掩膜得到更好的结果。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值