Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import os
- from glob import glob
- os.chdir('C:/Python27/Projects/VidComp/asd')
- cap = cv2.VideoCapture('C:/Python27/Projects/VidComp/asd/test_video.mov')
- cap.isOpened()
- count = 0
- success = True
- while success:
- success,image = cap.read()
- if count % 91 == 0:
- cv2.imwrite("image%d.jpg" % count, image)
- count += 1
- cv2.namedWindow('image', cv2.WINDOW_NORMAL)
- #Load the Image
- for fn in glob('*.jpg'):
- img0 = cv2.imread(fn)
- height, width = imgo.shape[:2]
- #Create a mask holder
- mask = np.zeros(imgo.shape[:2],np.uint8)
- #Grab Cut the object
- bgdModel = np.zeros((1,65),np.float64)
- fgdModel = np.zeros((1,65),np.float64)
- #Hard Coding the Rect The object must lie within this rect.
- rect = (750,500,450,950)
- cv2.grabCut(imgo,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
- mask = np.where((mask==2)|(mask==0),0,1).astype('uint8')
- img1 = imgo*mask[:,:,np.newaxis]
- #Get the background
- background = imgo - img1
- #Change all pixels in the background that are not black to white
- background[np.where((background > [0,0,0]).all(axis = 2))] = [255,255,255]
- #Add the background and the image
- final = background + img1
- #To be done - Smoothening the edges
- #cv2.imshow('image', img1 )
- cv2.imwrite('*.jpg',img1 )
- cv2.waitKey(0)
- k = cv.waitKey(0)
- if k==27:
- cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment