|
|
|
@ -95,80 +95,82 @@ def onmouse(event,x,y,flags,param): |
|
|
|
|
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, ../data/lena.jpg \n" |
|
|
|
|
print "Correct Usage: python grabcut.py <filename> \n" |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
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',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() |
|
|
|
|
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 = '../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) |
|
|
|
|
|
|
|
|
|
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',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() |
|
|
|
|