影象分割經典演算法--《圖割》(Graph Cut、Grab Cut-----python實現)
1. 演算法介紹
Graph Cut(圖形切割)應用於計算機視覺領域用來有效的解決各種低階計算機視覺問題,例如影象平滑(image smoothing)、立體應對問題(stereo correspondence problem)、影象分割(image segmentation)等等。此類方法把影象分割問題與圖的最小割(min cut)問題相關聯,在計算機視覺的很多類似的問題中,最小能量(minimum energy)方案對應解決方案的最大後驗估計(maximum posterior estimate)。 友情連結:影象分割經典演算法(最小割最大流) 使用Graph Cut的方法可以精確的解決”二進位制問題“(binary problem),例如對二進位制影象進行去噪。可以用兩個以上不同的標籤(例如立體對應(stereo correspondence)或灰度影象的去噪)標記畫素的問題不能精確解決,但產生的解決方案通常能夠接近全域性最優的效果。
2. GrapCut
GraphCut利用最小割最大流演算法進行影象的分割,可以將影象分割為前景和背景。使用該演算法時需要在前景和背景處各畫幾筆作為輸入,演算法將建立各個畫素點與前景背景相似度的賦權圖,並通過求解最小切割區分前景和背景。演算法效果圖如下: 原始圖片 標註圖片 分割後的圖片 相關論文及python實現程式碼:
3.GrabCut
GrabCut GrabCut是對其的改進版,是迭代的Graph Cut。OpenCV中的GrabCut演算法是依據《“GrabCut” - Interactive Foreground Extraction using Iterated Graph Cuts》這篇文章來實現的。該演算法利用了影象中的紋理(顏色)資訊和邊界(反差)資訊,只要少量的使用者互動操作即可得到比較好的分割結果。
GrabCut優點 (1)只需要在目標外面畫一個框,把目標框住,它就可以實現良好的分割效果; (2)增加額外的使用者互動(由使用者指定一些畫素屬於目標),對實現的效果進行優化以得到更好的效果; (3)它的Border Matting技術會使目標分割邊界更加自然和完美。 GrabCut同時存在這一些缺點:1.如果背景比較複雜或者背景和目標相似度很大,那分割的效果不太好;2.由於時迭代的GraphCut,所以速度較慢。
GrabCut和GraphCut的不同點 (1)GraphCut的目標和背景的模型是灰度直方圖,GrabCut取代為RGB三通道的混合高斯模型GMM; (2)GraphCut的能量最小化(分割)是一次達到的,而GrabCut取代為一個不斷進行分割估計和模型引數學習的互動迭代過程; (3)GraphCut需要使用者指定目標和背景的一些種子點,但是GrabCut只需要提供背景區域的畫素集就可以了。也就是說你只需要框選目標,那麼在方框外的畫素全部當成背景,這時候就可以對GMM進行建模和完成良好的分割了。即GrabCut允許不完全的標註(incomplete labelling)。
Python實現 程式碼:
#!/usr/bin/env python
'''
===============================================================================
Interactive Image Segmentation using GrabCut algorithm.
This application shows interactive image segmentation using grabcut algorithm.
USAGE :
python grabcut.py <filename>
README FIRST:
Two windows will show up, one for input and one for output.
At first, in input window, draw a rectangle around the object using
mouse right button. Then press 'n' to segment the object (once or a few times)
For any finer touch-ups, you can press any of the keys below and draw lines on
the areas you want. Then again press 'n' for updating the output.
Key '0' - To select areas of sure background
Key '1' - To select areas of sure foreground
Key '2' - To select areas of probable background
Key '3' - To select areas of probable foreground
Key 'n' - To update the segmentation
Key 'r' - To reset the setup
Key 's' - To save the results
===============================================================================
'''
import numpy as np
import cv2
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):
global img,img2,drawing,value,mask,rectangle,rect,rect_or_mask,ix,iy,rect_over
# Draw Rectangle
if event == cv2.EVENT_RBUTTONDOWN:
rectangle = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if rectangle == True:
img = img2.copy()
cv2.rectangle(img,(ix,iy),(x,y),BLUE,2)
rect = (ix,iy,abs(ix-x),abs(iy-y))
rect_or_mask = 0
elif event == cv2.EVENT_RBUTTONUP:
rectangle = False
rect_over = True
cv2.rectangle(img,(ix,iy),(x,y),BLUE,2)
rect = (ix,iy,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 == cv2.EVENT_LBUTTONDOWN:
if rect_over == False:
print "first draw rectangle \n"
else:
drawing = True
cv2.circle(img,(x,y),thickness,value['color'],-1)
cv2.circle(mask,(x,y),thickness,value['val'],-1)
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
cv2.circle(img,(x,y),thickness,value['color'],-1)
cv2.circle(mask,(x,y),thickness,value['val'],-1)
elif event == cv2.EVENT_LBUTTONUP:
if drawing == True:
drawing = False
cv2.circle(img,(x,y),thickness,value['color'],-1)
cv2.circle(mask,(x,y),thickness,value['val'],-1)
# 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, far.jpg \n"
print "Correct Usage : python grabcut.py <filename> \n"
filename = 'far.jpg'
img = cv2.imread(filename)
img2 = img.copy() # 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
cv2.namedWindow('output')
cv2.namedWindow('input')
cv2.setMouseCallback('input',onmouse)
cv2.moveWindow('input',img.shape[1]+10,90)
print " Instructions : \n"
print " Draw a rectangle around the object using right mouse button \n"
while(1):
cv2.imshow('output',output)
cv2.imshow('input',img)
k = 0xFF & cv2.waitKey(1)
# key bindings
if k == 27: # esc to exit
break
elif k == ord('0'): # BG drawing
print " 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))
cv2.imwrite('grabcut_output.png',output)
cv2.imwrite('grabcut_output_combined.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 = img2.copy()
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)
cv2.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv2.GC_INIT_WITH_RECT)
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)
cv2.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv2.GC_INIT_WITH_MASK)
mask2 = np.where((mask==1) + (mask==3),255,0).astype('uint8')
output = cv2.bitwise_and(img2,img2,mask=mask2)
cv2.destroyAllWindows()
實現效果: 原始圖片、處理過程、處理後的照片分別如下: GitHub程式碼: