mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
6.3 KiB
180 lines
6.3 KiB
12 years ago
|
#!/usr/bin/env python
|
||
|
'''
|
||
|
===============================================================================
|
||
|
Interactive Image Segmentation using GrabCut algorithm.
|
||
|
|
||
|
This sample shows interactive image segmentation using grabcut algorithm.
|
||
|
|
||
11 years ago
|
USAGE:
|
||
12 years ago
|
python grabcut.py <filename>
|
||
|
|
||
11 years ago
|
README FIRST:
|
||
12 years ago
|
Two windows will show up, one for input and one for output.
|
||
11 years ago
|
|
||
|
At first, in input window, draw a rectangle around the object using
|
||
12 years ago
|
mouse right button. Then press 'n' to segment the object (once or a few times)
|
||
11 years ago
|
For any finer touch-ups, you can press any of the keys below and draw lines on
|
||
12 years ago
|
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
|
||
|
===============================================================================
|
||
|
'''
|
||
|
|
||
9 years ago
|
# Python 2/3 compatibility
|
||
|
from __future__ import print_function
|
||
|
|
||
12 years ago
|
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
|
||
11 years ago
|
|
||
12 years ago
|
# 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)
|
||
11 years ago
|
rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
|
||
12 years ago
|
rect_or_mask = 0
|
||
|
|
||
|
elif event == cv2.EVENT_RBUTTONUP:
|
||
|
rectangle = False
|
||
|
rect_over = True
|
||
|
cv2.rectangle(img,(ix,iy),(x,y),BLUE,2)
|
||
11 years ago
|
rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
|
||
12 years ago
|
rect_or_mask = 0
|
||
9 years ago
|
print(" Now press the key 'n' a few times until no further change \n")
|
||
11 years ago
|
|
||
12 years ago
|
# draw touchup curves
|
||
11 years ago
|
|
||
12 years ago
|
if event == cv2.EVENT_LBUTTONDOWN:
|
||
|
if rect_over == False:
|
||
9 years ago
|
print("first draw rectangle \n")
|
||
12 years ago
|
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)
|
||
11 years ago
|
|
||
9 years ago
|
if __name__ == '__main__':
|
||
|
|
||
|
# print documentation
|
||
9 years ago
|
print(__doc__)
|
||
9 years ago
|
|
||
|
# Loading images
|
||
|
if len(sys.argv) == 2:
|
||
|
filename = sys.argv[1] # for drawing purposes
|
||
|
else:
|
||
9 years ago
|
print("No input image given, so loading default image, ../data/lena.jpg \n")
|
||
|
print("Correct Usage: python grabcut.py <filename> \n")
|
||
9 years ago
|
filename = '../data/lena.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)
|
||
|
|
||
9 years ago
|
print(" Instructions: \n")
|
||
|
print(" Draw a rectangle around the object using right mouse button \n")
|
||
9 years ago
|
|
||
|
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
|
||
9 years ago
|
print(" mark background regions with left mouse button \n")
|
||
9 years ago
|
value = DRAW_BG
|
||
|
elif k == ord('1'): # FG drawing
|
||
9 years ago
|
print(" mark foreground regions with left mouse button \n")
|
||
9 years ago
|
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',res)
|
||
9 years ago
|
print(" Result saved as image \n")
|
||
9 years ago
|
elif k == ord('r'): # reset everything
|
||
9 years ago
|
print("resetting \n")
|
||
9 years ago
|
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
|
||
9 years ago
|
print(""" For finer touchups, mark foreground and background after pressing keys 0-3
|
||
|
and again press 'n' \n""")
|
||
9 years ago
|
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()
|