grabcut.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python
  2. '''
  3. ===============================================================================
  4. Interactive Image Segmentation using GrabCut algorithm.
  5. This sample shows interactive image segmentation using grabcut algorithm.
  6. USAGE:
  7. python grabcut.py <filename>
  8. README FIRST:
  9. Two windows will show up, one for input and one for output.
  10. At first, in input window, draw a rectangle around the object using
  11. mouse right button. Then press 'n' to segment the object (once or a few times)
  12. For any finer touch-ups, you can press any of the keys below and draw lines on
  13. the areas you want. Then again press 'n' for updating the output.
  14. Key '0' - To select areas of sure background
  15. Key '1' - To select areas of sure foreground
  16. Key '2' - To select areas of probable background
  17. Key '3' - To select areas of probable foreground
  18. Key 'n' - To update the segmentation
  19. Key 'r' - To reset the setup
  20. Key 's' - To save the results
  21. ===============================================================================
  22. '''
  23. # Python 2/3 compatibility
  24. from __future__ import print_function
  25. import numpy as np
  26. import cv2
  27. import sys
  28. BLUE = [255,0,0] # rectangle color
  29. RED = [0,0,255] # PR BG
  30. GREEN = [0,255,0] # PR FG
  31. BLACK = [0,0,0] # sure BG
  32. WHITE = [255,255,255] # sure FG
  33. DRAW_BG = {'color' : BLACK, 'val' : 0}
  34. DRAW_FG = {'color' : WHITE, 'val' : 1}
  35. DRAW_PR_FG = {'color' : GREEN, 'val' : 3}
  36. DRAW_PR_BG = {'color' : RED, 'val' : 2}
  37. # setting up flags
  38. rect = (0,0,1,1)
  39. drawing = False # flag for drawing curves
  40. rectangle = False # flag for drawing rect
  41. rect_over = False # flag to check if rect drawn
  42. rect_or_mask = 100 # flag for selecting rect or mask mode
  43. value = DRAW_FG # drawing initialized to FG
  44. thickness = 3 # brush thickness
  45. def onmouse(event,x,y,flags,param):
  46. global img,img2,drawing,value,mask,rectangle,rect,rect_or_mask,ix,iy,rect_over
  47. # Draw Rectangle
  48. if event == cv2.EVENT_RBUTTONDOWN:
  49. rectangle = True
  50. ix,iy = x,y
  51. elif event == cv2.EVENT_MOUSEMOVE:
  52. if rectangle == True:
  53. img = img2.copy()
  54. cv2.rectangle(img,(ix,iy),(x,y),BLUE,2)
  55. rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
  56. rect_or_mask = 0
  57. elif event == cv2.EVENT_RBUTTONUP:
  58. rectangle = False
  59. rect_over = True
  60. cv2.rectangle(img,(ix,iy),(x,y),BLUE,2)
  61. rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
  62. rect_or_mask = 0
  63. print(" Now press the key 'n' a few times until no further change \n")
  64. # draw touchup curves
  65. if event == cv2.EVENT_LBUTTONDOWN:
  66. if rect_over == False:
  67. print("first draw rectangle \n")
  68. else:
  69. drawing = True
  70. cv2.circle(img,(x,y),thickness,value['color'],-1)
  71. cv2.circle(mask,(x,y),thickness,value['val'],-1)
  72. elif event == cv2.EVENT_MOUSEMOVE:
  73. if drawing == True:
  74. cv2.circle(img,(x,y),thickness,value['color'],-1)
  75. cv2.circle(mask,(x,y),thickness,value['val'],-1)
  76. elif event == cv2.EVENT_LBUTTONUP:
  77. if drawing == True:
  78. drawing = False
  79. cv2.circle(img,(x,y),thickness,value['color'],-1)
  80. cv2.circle(mask,(x,y),thickness,value['val'],-1)
  81. if __name__ == '__main__':
  82. # print documentation
  83. print(__doc__)
  84. # Loading images
  85. if len(sys.argv) == 2:
  86. filename = sys.argv[1] # for drawing purposes
  87. else:
  88. print("No input image given, so loading default image, ../data/lena.jpg \n")
  89. print("Correct Usage: python grabcut.py <filename> \n")
  90. filename = '../data/lena.jpg'
  91. img = cv2.imread(filename)
  92. img2 = img.copy() # a copy of original image
  93. mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG
  94. output = np.zeros(img.shape,np.uint8) # output image to be shown
  95. # input and output windows
  96. cv2.namedWindow('output')
  97. cv2.namedWindow('input')
  98. cv2.setMouseCallback('input',onmouse)
  99. cv2.moveWindow('input',img.shape[1]+10,90)
  100. print(" Instructions: \n")
  101. print(" Draw a rectangle around the object using right mouse button \n")
  102. while(1):
  103. cv2.imshow('output',output)
  104. cv2.imshow('input',img)
  105. k = 0xFF & cv2.waitKey(1)
  106. # key bindings
  107. if k == 27: # esc to exit
  108. break
  109. elif k == ord('0'): # BG drawing
  110. print(" mark background regions with left mouse button \n")
  111. value = DRAW_BG
  112. elif k == ord('1'): # FG drawing
  113. print(" mark foreground regions with left mouse button \n")
  114. value = DRAW_FG
  115. elif k == ord('2'): # PR_BG drawing
  116. value = DRAW_PR_BG
  117. elif k == ord('3'): # PR_FG drawing
  118. value = DRAW_PR_FG
  119. elif k == ord('s'): # save image
  120. bar = np.zeros((img.shape[0],5,3),np.uint8)
  121. res = np.hstack((img2,bar,img,bar,output))
  122. cv2.imwrite('grabcut_output.png',res)
  123. print(" Result saved as image \n")
  124. elif k == ord('r'): # reset everything
  125. print("resetting \n")
  126. rect = (0,0,1,1)
  127. drawing = False
  128. rectangle = False
  129. rect_or_mask = 100
  130. rect_over = False
  131. value = DRAW_FG
  132. img = img2.copy()
  133. mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG
  134. output = np.zeros(img.shape,np.uint8) # output image to be shown
  135. elif k == ord('n'): # segment the image
  136. print(""" For finer touchups, mark foreground and background after pressing keys 0-3
  137. and again press 'n' \n""")
  138. if (rect_or_mask == 0): # grabcut with rect
  139. bgdmodel = np.zeros((1,65),np.float64)
  140. fgdmodel = np.zeros((1,65),np.float64)
  141. cv2.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv2.GC_INIT_WITH_RECT)
  142. rect_or_mask = 1
  143. elif rect_or_mask == 1: # grabcut with mask
  144. bgdmodel = np.zeros((1,65),np.float64)
  145. fgdmodel = np.zeros((1,65),np.float64)
  146. cv2.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv2.GC_INIT_WITH_MASK)
  147. mask2 = np.where((mask==1) + (mask==3),255,0).astype('uint8')
  148. output = cv2.bitwise_and(img2,img2,mask=mask2)
  149. cv2.destroyAllWindows()