使用python+OpenCV实现抖音特效“蓝线挑战”
使用OpenCV實現抖音“藍線挑戰”特效。原理比較簡單,當藍線在視頻畫面中滑動,然后從滑過的每一幀中截取部分畫面生成一幅靜態圖片,由于上一幀畫面已經定格,那么通過調整下一幀畫面可以生成各種奇怪的場景。
處理過程可分為幾個步驟:
以藍線從左往右為例:
一、藍線從左往右滑動:
from?cv2?import?cv2
#繪制靜態畫面和藍線
def?drawline(frame,img,x,width,height,bluelinespeed):
????bulelinelocation?=?int(x?+?bluelinespeed?+?2)
????if?bulelinelocation?<?width:
????????image_block?=?frame[0:height,int(x):width]
????????tempimg[0:height,int(x):width]?=?image_block
????????cv2.line(img,(bulelinelocation,0),(bulelinelocation,height),(255,0,0),2)
videoCapture?=?cv2.VideoCapture('C:/Users/admin/Desktop/test/video/2.avi')
fps?=?videoCapture.get(cv2.CAP_PROP_FPS)
width?=?int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH))
height?=?int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
totalCount?=?videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
size?=?(width,height)
bluelinespeed?=?width /?totalCount?????
videoWriter?=?cv2.VideoWriter('C:/Users/admin/Desktop/test/video/222.avi',cv2.VideoWriter_fourcc('X','V','I','D'),fps,size)
success,frame?=?videoCapture.read()
tempimg?=?frame
img?=?frame
remianing?=?totalCount
x?=?0
while?success?and?remianing?>?0:
????x?=?x?+?bluelinespeed
????drawline(frame,img,x,width,height,bluelinespeed)
????videoWriter.write(img)
????success,frame?=?videoCapture.read()
????remianing?-=?1
效果:
二、藍線從右往左
from?cv2?import?cv2
#繪制靜態畫面和藍線
def?drawline(frame,img,x,width,height,bluelinespeed):
????bulelinelocation?=?int(x?-?bluelinespeed?-?2)
????if?bulelinelocation?>?0:
????????image_block?=?frame[0:height,0:int(x)]
????????tempimg[0:height,0:int(x)]?=?image_block
????????cv2.line(img,(bulelinelocation,0),(bulelinelocation,height),(255,0,0),2)
videoCapture?=?cv2.VideoCapture('C:/Users/admin/Desktop/test/video/2.avi')
fps?=?videoCapture.get(cv2.CAP_PROP_FPS)
width?=?int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH))
height?=?int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
totalCount?=?videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
size?=?(width,height)
bluelinespeed?=?width /?totalCount?????
videoWriter?=?cv2.VideoWriter('C:/Users/admin/Desktop/test/video/222.avi',cv2.VideoWriter_fourcc('X','V','I','D'),fps,size)
success,frame?=?videoCapture.read()
tempimg?=?frame
img?=?frame
remianing?=?totalCount
x?=?width
while?success?and?remianing?>?0:
????x?=?x?-?bluelinespeed
????drawline(frame,img,x,width,height,bluelinespeed)
????videoWriter.write(img)
????success,frame?=?videoCapture.read()
????remianing?-=?1
效果:
三、藍線從上往下
from?cv2?import?cv2
#繪制靜態畫面和藍線
def?drawline(frame,img,x,width,height,bluelinespeed):
????bulelinelocation?=?int(x?+?bluelinespeed?+?2)
????if?bulelinelocation?<?height:
????????image_block?=?frame[int(x):height,0:width]
????????tempimg[int(x):height,0:width]?=?image_block
????????cv2.line(img,(0,bulelinelocation),(width,bulelinelocation),(255,0,0),2)
videoCapture?=?cv2.VideoCapture('C:/Users/admin/Desktop/test/video/2.avi')
fps?=?videoCapture.get(cv2.CAP_PROP_FPS)
width?=?int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH))
height?=?int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
totalCount?=?videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
size?=?(width,height)
bluelinespeed?=?height?/?totalCount?????
videoWriter?=?cv2.VideoWriter('C:/Users/admin/Desktop/test/video/222.avi',cv2.VideoWriter_fourcc('X','V','I','D'),fps,size)
success,frame?=?videoCapture.read()
tempimg?=?frame
img?=?frame
remianing?=?totalCount
x?=?0
while?success?and?remianing?>?0:
????x?=?x?+?bluelinespeed
????drawline(frame,img,x,width,height,bluelinespeed)
????videoWriter.write(img)
????success,frame?=?videoCapture.read()
????remianing?-=?1
效果:
四、藍線從下往上滑動:
from?cv2?import?cv2
#繪制靜態畫面和藍線
def?drawline(frame,img,x,width,height,bluelinespeed):
????bulelinelocation?=?int(x?- bluelinespeed?-?2)
????if?bulelinelocation?<?height:
????????image_block?=?frame[0:int(x),0:width]
????????tempimg[0:int(x),0:width]?=?image_block
????????cv2.line(img,(0,bulelinelocation),(width,bulelinelocation),(255,0,0),2)
videoCapture?=?cv2.VideoCapture('C:/Users/admin/Desktop/test/video/1.avi')
fps?=?videoCapture.get(cv2.CAP_PROP_FPS)
width?=?int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH))
height?=?int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
totalCount?=?videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
size?=?(width,height)
bluelinespeed?=?height?/?totalCount?????
videoWriter?=?cv2.VideoWriter('C:/Users/admin/Desktop/test/video/11.avi',cv2.VideoWriter_fourcc('X','V','I','D'),fps,size)
success,frame?=?videoCapture.read()
tempimg?=?frame
img?=?frame
remianing?=?totalCount
x?=?height
while?success?and?remianing?>?0:
????x?=?x?-?bluelinespeed
????drawline(frame,img,x,width,height,bluelinespeed)
????videoWriter.write(img)
????success,frame?=?videoCapture.read()
????remianing?-=?1
效果:
?
總結
以上是生活随笔為你收集整理的使用python+OpenCV实现抖音特效“蓝线挑战”的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数独游戏的算法实现
- 下一篇: python使用线性回归实现房价预测