|
|
@ -32,148 +32,154 @@ from __future__ import print_function |
|
|
|
|
|
|
|
|
|
|
|
import numpy as np |
|
|
|
import numpy as np |
|
|
|
import cv2 as cv |
|
|
|
import cv2 as cv |
|
|
|
|
|
|
|
|
|
|
|
import sys |
|
|
|
import sys |
|
|
|
|
|
|
|
|
|
|
|
BLUE = [255,0,0] # rectangle color |
|
|
|
class App(): |
|
|
|
RED = [0,0,255] # PR BG |
|
|
|
BLUE = [255,0,0] # rectangle color |
|
|
|
GREEN = [0,255,0] # PR FG |
|
|
|
RED = [0,0,255] # PR BG |
|
|
|
BLACK = [0,0,0] # sure BG |
|
|
|
GREEN = [0,255,0] # PR FG |
|
|
|
WHITE = [255,255,255] # sure 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_BG = {'color' : BLACK, 'val' : 0} |
|
|
|
DRAW_PR_FG = {'color' : GREEN, 'val' : 3} |
|
|
|
DRAW_FG = {'color' : WHITE, 'val' : 1} |
|
|
|
DRAW_PR_BG = {'color' : RED, 'val' : 2} |
|
|
|
DRAW_PR_FG = {'color' : GREEN, 'val' : 3} |
|
|
|
|
|
|
|
DRAW_PR_BG = {'color' : RED, 'val' : 2} |
|
|
|
# setting up flags |
|
|
|
|
|
|
|
rect = (0,0,1,1) |
|
|
|
# setting up flags |
|
|
|
drawing = False # flag for drawing curves |
|
|
|
rect = (0,0,1,1) |
|
|
|
rectangle = False # flag for drawing rect |
|
|
|
drawing = False # flag for drawing curves |
|
|
|
rect_over = False # flag to check if rect drawn |
|
|
|
rectangle = False # flag for drawing rect |
|
|
|
rect_or_mask = 100 # flag for selecting rect or mask mode |
|
|
|
rect_over = False # flag to check if rect drawn |
|
|
|
value = DRAW_FG # drawing initialized to FG |
|
|
|
rect_or_mask = 100 # flag for selecting rect or mask mode |
|
|
|
thickness = 3 # brush thickness |
|
|
|
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 |
|
|
|
def onmouse(self, event, x, y, flags, param): |
|
|
|
|
|
|
|
# Draw Rectangle |
|
|
|
# Draw Rectangle |
|
|
|
if event == cv.EVENT_RBUTTONDOWN: |
|
|
|
if event == cv.EVENT_RBUTTONDOWN: |
|
|
|
self.rectangle = True |
|
|
|
rectangle = True |
|
|
|
self.ix, self.iy = x,y |
|
|
|
ix,iy = x,y |
|
|
|
|
|
|
|
|
|
|
|
elif event == cv.EVENT_MOUSEMOVE: |
|
|
|
elif event == cv.EVENT_MOUSEMOVE: |
|
|
|
if self.rectangle == True: |
|
|
|
if rectangle == True: |
|
|
|
self.img = self.img2.copy() |
|
|
|
img = img2.copy() |
|
|
|
cv.rectangle(self.img, (self.ix, self.iy), (x, y), self.BLUE, 2) |
|
|
|
cv.rectangle(img,(ix,iy),(x,y),BLUE,2) |
|
|
|
self.rect = (min(self.ix, x), min(self.iy, y), abs(self.ix - x), abs(self.iy - y)) |
|
|
|
rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y)) |
|
|
|
self.rect_or_mask = 0 |
|
|
|
rect_or_mask = 0 |
|
|
|
|
|
|
|
|
|
|
|
elif event == cv.EVENT_RBUTTONUP: |
|
|
|
elif event == cv.EVENT_RBUTTONUP: |
|
|
|
self.rectangle = False |
|
|
|
rectangle = False |
|
|
|
self.rect_over = True |
|
|
|
rect_over = True |
|
|
|
cv.rectangle(self.img, (self.ix, self.iy), (x, y), self.BLUE, 2) |
|
|
|
cv.rectangle(img,(ix,iy),(x,y),BLUE,2) |
|
|
|
self.rect = (min(self.ix, x), min(self.iy, y), abs(self.ix - x), abs(self.iy - y)) |
|
|
|
rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y)) |
|
|
|
self.rect_or_mask = 0 |
|
|
|
rect_or_mask = 0 |
|
|
|
print(" Now press the key 'n' a few times until no further change \n") |
|
|
|
print(" Now press the key 'n' a few times until no further change \n") |
|
|
|
|
|
|
|
|
|
|
|
# draw touchup curves |
|
|
|
# draw touchup curves |
|
|
|
|
|
|
|
|
|
|
|
if event == cv.EVENT_LBUTTONDOWN: |
|
|
|
if event == cv.EVENT_LBUTTONDOWN: |
|
|
|
if self.rect_over == False: |
|
|
|
if rect_over == False: |
|
|
|
print("first draw rectangle \n") |
|
|
|
print("first draw rectangle \n") |
|
|
|
else: |
|
|
|
|
|
|
|
self.drawing = True |
|
|
|
|
|
|
|
cv.circle(self.img, (x,y), self.thickness, self.value['color'], -1) |
|
|
|
|
|
|
|
cv.circle(self.mask, (x,y), self.thickness, self.value['val'], -1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif event == cv.EVENT_MOUSEMOVE: |
|
|
|
|
|
|
|
if self.drawing == True: |
|
|
|
|
|
|
|
cv.circle(self.img, (x, y), self.thickness, self.value['color'], -1) |
|
|
|
|
|
|
|
cv.circle(self.mask, (x, y), self.thickness, self.value['val'], -1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif event == cv.EVENT_LBUTTONUP: |
|
|
|
|
|
|
|
if self.drawing == True: |
|
|
|
|
|
|
|
self.drawing = False |
|
|
|
|
|
|
|
cv.circle(self.img, (x, y), self.thickness, self.value['color'], -1) |
|
|
|
|
|
|
|
cv.circle(self.mask, (x, y), self.thickness, self.value['val'], -1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self): |
|
|
|
|
|
|
|
# Loading images |
|
|
|
|
|
|
|
if len(sys.argv) == 2: |
|
|
|
|
|
|
|
filename = sys.argv[1] # for drawing purposes |
|
|
|
else: |
|
|
|
else: |
|
|
|
drawing = True |
|
|
|
print("No input image given, so loading default image, lena.jpg \n") |
|
|
|
cv.circle(img,(x,y),thickness,value['color'],-1) |
|
|
|
print("Correct Usage: python grabcut.py <filename> \n") |
|
|
|
cv.circle(mask,(x,y),thickness,value['val'],-1) |
|
|
|
filename = 'lena.jpg' |
|
|
|
|
|
|
|
|
|
|
|
elif event == cv.EVENT_MOUSEMOVE: |
|
|
|
self.img = cv.imread(cv.samples.findFile(filename)) |
|
|
|
if drawing == True: |
|
|
|
self.img2 = self.img.copy() # a copy of original image |
|
|
|
cv.circle(img,(x,y),thickness,value['color'],-1) |
|
|
|
self.mask = np.zeros(self.img.shape[:2], dtype = np.uint8) # mask initialized to PR_BG |
|
|
|
cv.circle(mask,(x,y),thickness,value['val'],-1) |
|
|
|
self.output = np.zeros(self.img.shape, np.uint8) # output image to be shown |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# input and output windows |
|
|
|
|
|
|
|
cv.namedWindow('output') |
|
|
|
|
|
|
|
cv.namedWindow('input') |
|
|
|
|
|
|
|
cv.setMouseCallback('input', self.onmouse) |
|
|
|
|
|
|
|
cv.moveWindow('input', self.img.shape[1]+10,90) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(" Instructions: \n") |
|
|
|
|
|
|
|
print(" Draw a rectangle around the object using right mouse button \n") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(1): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cv.imshow('output', self.output) |
|
|
|
|
|
|
|
cv.imshow('input', self.img) |
|
|
|
|
|
|
|
k = cv.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") |
|
|
|
|
|
|
|
self.value = self.DRAW_BG |
|
|
|
|
|
|
|
elif k == ord('1'): # FG drawing |
|
|
|
|
|
|
|
print(" mark foreground regions with left mouse button \n") |
|
|
|
|
|
|
|
self.value = self.DRAW_FG |
|
|
|
|
|
|
|
elif k == ord('2'): # PR_BG drawing |
|
|
|
|
|
|
|
self.value = self.DRAW_PR_BG |
|
|
|
|
|
|
|
elif k == ord('3'): # PR_FG drawing |
|
|
|
|
|
|
|
self.value = self.DRAW_PR_FG |
|
|
|
|
|
|
|
elif k == ord('s'): # save image |
|
|
|
|
|
|
|
bar = np.zeros((self.img.shape[0], 5, 3), np.uint8) |
|
|
|
|
|
|
|
res = np.hstack((self.img2, bar, self.img, bar, self.output)) |
|
|
|
|
|
|
|
cv.imwrite('grabcut_output.png', res) |
|
|
|
|
|
|
|
print(" Result saved as image \n") |
|
|
|
|
|
|
|
elif k == ord('r'): # reset everything |
|
|
|
|
|
|
|
print("resetting \n") |
|
|
|
|
|
|
|
self.rect = (0,0,1,1) |
|
|
|
|
|
|
|
self.drawing = False |
|
|
|
|
|
|
|
self.rectangle = False |
|
|
|
|
|
|
|
self.rect_or_mask = 100 |
|
|
|
|
|
|
|
self.rect_over = False |
|
|
|
|
|
|
|
self.value = self.DRAW_FG |
|
|
|
|
|
|
|
self.img = self.img2.copy() |
|
|
|
|
|
|
|
self.mask = np.zeros(self.img.shape[:2], dtype = np.uint8) # mask initialized to PR_BG |
|
|
|
|
|
|
|
self.output = np.zeros(self.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""") |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
if (self.rect_or_mask == 0): # grabcut with rect |
|
|
|
|
|
|
|
bgdmodel = np.zeros((1, 65), np.float64) |
|
|
|
|
|
|
|
fgdmodel = np.zeros((1, 65), np.float64) |
|
|
|
|
|
|
|
cv.grabCut(self.img2, self.mask, self.rect, bgdmodel, fgdmodel, 1, cv.GC_INIT_WITH_RECT) |
|
|
|
|
|
|
|
self.rect_or_mask = 1 |
|
|
|
|
|
|
|
elif self.rect_or_mask == 1: # grabcut with mask |
|
|
|
|
|
|
|
bgdmodel = np.zeros((1, 65), np.float64) |
|
|
|
|
|
|
|
fgdmodel = np.zeros((1, 65), np.float64) |
|
|
|
|
|
|
|
cv.grabCut(self.img2, self.mask, self.rect, bgdmodel, fgdmodel, 1, cv.GC_INIT_WITH_MASK) |
|
|
|
|
|
|
|
except: |
|
|
|
|
|
|
|
import traceback |
|
|
|
|
|
|
|
traceback.print_exc() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mask2 = np.where((self.mask==1) + (self.mask==3), 255, 0).astype('uint8') |
|
|
|
|
|
|
|
self.output = cv.bitwise_and(self.img2, self.img2, mask=mask2) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print('Done') |
|
|
|
|
|
|
|
|
|
|
|
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__': |
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
|
|
|
|
|
|
# print documentation |
|
|
|
|
|
|
|
print(__doc__) |
|
|
|
print(__doc__) |
|
|
|
|
|
|
|
App().run() |
|
|
|
# Loading images |
|
|
|
|
|
|
|
if len(sys.argv) == 2: |
|
|
|
|
|
|
|
filename = sys.argv[1] # for drawing purposes |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
print("No input image given, so loading default image, lena.jpg \n") |
|
|
|
|
|
|
|
print("Correct Usage: python grabcut.py <filename> \n") |
|
|
|
|
|
|
|
filename = 'lena.jpg' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
img = cv.imread(cv.samples.findFile(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 |
|
|
|
|
|
|
|
cv.namedWindow('output') |
|
|
|
|
|
|
|
cv.namedWindow('input') |
|
|
|
|
|
|
|
cv.setMouseCallback('input',onmouse) |
|
|
|
|
|
|
|
cv.moveWindow('input',img.shape[1]+10,90) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
|
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 = 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) |
|
|
|
|
|
|
|
cv.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv.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) |
|
|
|
|
|
|
|
cv.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv.GC_INIT_WITH_MASK) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mask2 = np.where((mask==1) + (mask==3),255,0).astype('uint8') |
|
|
|
|
|
|
|
output = cv.bitwise_and(img2,img2,mask=mask2) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cv.destroyAllWindows() |
|
|
|
cv.destroyAllWindows() |
|
|
|