importcv2importnumpyasnpimportargparseparser=argparse.ArgumentParser()parser.add_argument("filename",help="input video file path")parser.add_argument("-o","--output",default="output.avi",help="output file path (default: ouput.avi)")parser.add_argument("-s","--size",type=float,required=True,help="region size in %% of image height")parser.add_argument("-d","--decay",type=float,required=True,help="decay rate in %% of image height")parser.add_argument("-c","--center",type=float,required=True,help="center height in %% of image height")parser.add_argument("-f",'--framedrop',type=int,default=1,help="frame dropping rate")args=vars(parser.parse_args())
importcv2importnumpyasnpimportargparseparser=argparse.ArgumentParser()parser.add_argument("filename",help="input video file path")parser.add_argument("-o","--output",default="output.avi",help="output file path (default: ouput.avi)")parser.add_argument("-s","--size",type=float,required=True,help="region size in %% of image height")parser.add_argument("-d","--decay",type=float,required=True,help="decay rate in %% of image height")parser.add_argument("-c","--center",type=float,required=True,help="center height in %% of image height")parser.add_argument("-f",'--framedrop',type=int,default=1,help="frame dropping rate")args=vars(parser.parse_args())cap=cv2.VideoCapture(args['filename'])_,first_frame=cap.read()M,N=first_frame.shape[:2]_,mesh=np.meshgrid(np.arange(N),np.arange(M))d=args['decay']*M/100l1=(args['center']-args['size']/2)*M/100l2=(args['center']+args['size']/2)*M/100defalpha(x,d,l1,l2):return0.5*(np.tanh((x-l1)/d)-np.tanh((x-l2)/d))mask=alpha(mesh,d,l1,l2)mask=np.atleast_3d(mask)blur=np.ones((5,5))/25deftiltshift(original):filtered=cv2.filter2D(original,-1,blur)blended=mask*original+(1-mask)*filteredblended=blended.astype(np.uint8)returnblendedfourcc=cv2.VideoWriter_fourcc(*'MJPG')out=cv2.VideoWriter(args['output'],fourcc,24.,(N,M))framecount=0whilecap.isOpened():ret,frame=cap.read()ifnotret:breakframecount=framecount+1ifframecount%args['framedrop']>0:continueblended=tiltshift(frame)out.write(blended)