图像前景分割之GrabCut算法
参考:
运⾏环境:
1.python3.x
2.所需库函数:numpy,opencv-python
3.操作步骤:
1) 长按⿏标右键在分割⽬标周围标定矩形框,矩形框外的区域被⾃动认为是背景;
2) 按字母'n'得到初步分割结果;
3) 按数字'0',在input图上通过长按⿏标左键进⾏背景标注,按字母'n'得到初步分割结果。(标注处为背景区域去掉);
4) 按数字'1',在input图上通过长按⿏标左键进⾏前景标注,按字母'n'得到初步分割结果。(标注处为前景区域增加保留)
5) 按数字'2',在input图上通过长按⿏标左键进⾏背景标注,按字母'n'得到初步分割结果。(标注处为可能为背景区域);
6) 按数字'3',在input图上通过长按⿏标左键进⾏前景标注,按字母'n'得到初步分割结果。(标注处为可能为前景区域);
7) 按字母'r',重置;
8) 按字母's',将分割后的图⽚保存到输出⽂件;
9) 按键盘'esc',退出;
在input图⽚中标定含有物体的矩形,矩形外的区域被⾃动认为是背景,对于⽤户标定的矩形区域,可⽤背景中的数据来区别它⾥⾯的前景和背景区域。⽤⾼斯混合模型(GMM)来对背景和前景建模,并将末定义的像素标记为可能的前景或背景。图像中的每⼀个像素都被看作通过虚拟边与周围像素相连接,⽽每⼀条边都有⼀个属于前景或背景的概率,这基于它与周围像素颜⾊上的相似性。每⼀个像素会与⼀个前景或背景节点连接。若节点之间不属于同⼀个终端(就是两个相邻的节点,⼀个节点属于前景,⼀个节点属于背景),则会切断它们之间的边,这就将图像各个部分分割出来了。
# -*- coding:utf-8 -*-
# Python 2/3 compatibility
from __future__ import print_function
import numpy as np
import cv2 as cv
import sys
BLUE = [255,0,0]        # rectangle color
RED = [0,0,255]        # PR BG
GREEN = [0,255,0]      # PR FG
BLACK = [0,0,0]        # sure BG
WHITE = [255,255,255]  # sure FG
DRAW_BG = {'color' : BLACK, 'val' : 0}
DRAW_FG = {'color' : WHITE, 'val' : 1}
DRAW_PR_FG = {'color' : GREEN, 'val' : 3}
DRAW_PR_BG = {'color' : RED, 'val' : 2}
# setting up flags
rect = (0,0,1,1)
drawing = False        # flag for drawing curves
rectangle = False      # flag for drawing rect
rect_over = False      # flag to check if rect drawn
rect_or_mask = 100      # flag for selecting rect or mask mode
value = DRAW_FG        # drawing initialized to FG
thickness = 3          # brush thickness
def onmouse(event,x,y,flags,param):
def onmouse(event,x,y,flags,param):
global img,img2,drawing,value,mask,rectangle,rect,rect_or_mask,ix,iy,rect_over
# Draw Rectangle
if event == cv.EVENT_RBUTTONDOWN:
rectangle = True
ix,iy = x,y
elif event == cv.EVENT_MOUSEMOVE:
if rectangle == True:
img = py()
rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
rect_or_mask = 0
elif event == cv.EVENT_RBUTTONUP:
rectangle = False
rect_over = True
rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
rect_or_mask = 0
print(" Now press the key 'n' a few times until no further change \n")
# draw touchup curves
if event == cv.EVENT_LBUTTONDOWN:
if rect_over == False:
print("first draw rectangle \n")
else:
drawing = True
cv.circle(img,(x,y),thickness,value['color'],-1)
cv.circle(mask,(x,y),thickness,value['val'],-1)
elif event == cv.EVENT_MOUSEMOVE:
if drawing == True:
cv.circle(img,(x,y),thickness,value['color'],-1)
cv.circle(mask,(x,y),thickness,value['val'],-1)
elif event == cv.EVENT_LBUTTONUP:
if drawing == True:
drawing = False
cv.circle(img,(x,y),thickness,value['color'],-1)
cv.circle(mask,(x,y),thickness,value['val'],-1)
if __name__ == '__main__':
# print documentation
print(__doc__)
# Loading images
if len(sys.argv) == 2:
filename = sys.argv[1] # for drawing purposes
else:
print("No input image given, so loading default image, ../data/lena.jpg \n")
print("Correct Usage: python grabcut.py <filename> \n")
filename = 'cows.jpg'
img = cv.imread(filename)
img2 = py()                              # a copy of original image
mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG    output = np.zeros(img.shape,np.uint8)          # output image to be shown
# input and output windows
cv.namedWindow('output')
cv.namedWindow('input')
cv.setMouseCallback('input',onmouse)
cv.setMouseCallback('input',onmouse)
print(" Instructions: \n")
print(" Draw a rectangle around the object using right mouse button \n")
while(1):
cv.imshow('output',output)
cv.imshow('input',img)
k = cv.waitKey(1)
# key bindings
if k == 27:        # esc to exit
break
elif k == ord('0'): # BG drawing,背景
rectangle函数opencvprint(" mark background regions with left mouse button \n")
value = DRAW_BG
elif k == ord('1'): # FG drawing,前景
print(" mark foreground regions with left mouse button \n")
value = DRAW_FG
elif k == ord('2'): # PR_BG drawing,可能的背景
value = DRAW_PR_BG
elif k == ord('3'): # PR_FG drawing,可能的前景
value = DRAW_PR_FG
elif k == ord('s'): # save image
bar = np.zeros((img.shape[0],5,3),np.uint8)
#res = np.hstack((img2,bar,img,bar,output))#原图、框图、提取图均保存
#res = np.hstack((img2, bar))#只存原图
#res = np.hstack((img,bar))#只存矩形框图+原图
res = np.hstack((output,bar))#只存提取图
cv.imwrite('grabcut_output.png',res)
print(" Result saved as image \n")
elif k == ord('r'): # reset everything
print("resetting \n")
rect = (0,0,1,1)
drawing = False
rectangle = False
rect_or_mask = 100
rect_over = False
value = DRAW_FG
img = py()
mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG
output = np.zeros(img.shape,np.uint8)          # output image to be shown
elif k == ord('n'): # segment the image
print(""" For finer touchups, mark foreground and background after pressing keys 0-3            and again press 'n' \n""")
if (rect_or_mask == 0):        # grabcut with rect
bgdmodel = np.zeros((1,65),np.float64)
fgdmodel = np.zeros((1,65),np.float64)
rect_or_mask = 1
elif rect_or_mask == 1:        # grabcut with mask
bgdmodel = np.zeros((1,65),np.float64)
fgdmodel = np.zeros((1,65),np.float64)
mask2 = np.where((mask==1) + (mask==3),255,0).astype('uint8')
output = cv.bitwise_and(img2,img2,mask=mask2)
cv.destroyAllWindows()
注:83⾏修改⾃⼰本地图⽚
实现效果:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。