Guest User

Untitled

a guest
Jul 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4. from glob import glob
  5.  
  6. os.chdir('C:/Python27/Projects/VidComp/asd')
  7. cap = cv2.VideoCapture('C:/Python27/Projects/VidComp/asd/test_video.mov')
  8. cap.isOpened()
  9.  
  10. count = 0
  11. success = True
  12. while success:
  13. success,image = cap.read()
  14. if count % 91 == 0:
  15. cv2.imwrite("image%d.jpg" % count, image)
  16. count += 1
  17.  
  18. cv2.namedWindow('image', cv2.WINDOW_NORMAL)
  19.  
  20. #Load the Image
  21. for fn in glob('*.jpg'):
  22. img0 = cv2.imread(fn)
  23.  
  24. height, width = imgo.shape[:2]
  25.  
  26. #Create a mask holder
  27. mask = np.zeros(imgo.shape[:2],np.uint8)
  28.  
  29. #Grab Cut the object
  30. bgdModel = np.zeros((1,65),np.float64)
  31. fgdModel = np.zeros((1,65),np.float64)
  32.  
  33. #Hard Coding the Rect The object must lie within this rect.
  34. rect = (750,500,450,950)
  35. cv2.grabCut(imgo,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
  36. mask = np.where((mask==2)|(mask==0),0,1).astype('uint8')
  37. img1 = imgo*mask[:,:,np.newaxis]
  38.  
  39. #Get the background
  40. background = imgo - img1
  41.  
  42. #Change all pixels in the background that are not black to white
  43. background[np.where((background > [0,0,0]).all(axis = 2))] = [255,255,255]
  44.  
  45. #Add the background and the image
  46. final = background + img1
  47.  
  48. #To be done - Smoothening the edges
  49.  
  50. #cv2.imshow('image', img1 )
  51. cv2.imwrite('*.jpg',img1 )
  52.  
  53. cv2.waitKey(0)
  54.  
  55. k = cv.waitKey(0)
  56.  
  57. if k==27:
  58. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment