Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. from matplotlib import pyplot as plt
  4.  
  5. img = cv2.imread('AB.jpg')
  6. mask = np.zeros(img.shape[:2] , np.uint8)
  7.  
  8. bgdModel = np.zeros((1,65), np.float64)
  9. fgdModel = np.zeros((1,65), np.float64)
  10.  
  11. rect = (300 , 120 , 470 , 350)
  12.  
  13. #this modifies mask
  14. cv2.grabCut(img,mask,rect,bgdModel, fgdModel , 5 , cv2.GC_INIT_WITH_RECT)
  15.  
  16. #If mask==2 or mask==1 , mask2 get 0, otherwise it gets 1 as 'uint8' type
  17. mask2 = np.where((mask==2) | (mask==0),0,1).astype('uint8')
  18.  
  19. #adding additional dimension for rgb to the mask, by default it gets 1
  20. #multiply with input image to get the segmented image
  21. img_cut = img*mask2[: , : , np.newaxis]
  22.  
  23. plt.subplot(211),plt.imshow(img)
  24. plt.title('Input Image') , plt.xticks([]),plt.yticks([])
  25. plt.subplot(212),plt.imshow(img_cut)
  26. plt.title('Grab cut'), plt.xticks([]),plt.yticks([])
  27. plt.show()
  28.  
  29.  
  30. on compiling i get this error :
  31. python img.py interstellar.jpg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement