使用python+OpenCV实现抖音特效“蓝线挑战”
使用OpenCV實(shí)現(xiàn)抖音“藍(lán)線挑戰(zhàn)”特效。原理比較簡(jiǎn)單,當(dāng)藍(lán)線在視頻畫面中滑動(dòng),然后從滑過(guò)的每一幀中截取部分畫面生成一幅靜態(tài)圖片,由于上一幀畫面已經(jīng)定格,那么通過(guò)調(diào)整下一幀畫面可以生成各種奇怪的場(chǎng)景。
處理過(guò)程可分為幾個(gè)步驟:
以藍(lán)線從左往右為例:
一、藍(lán)線從左往右滑動(dòng):
from?cv2?import?cv2
#繪制靜態(tài)畫面和藍(lán)線
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
效果:
二、藍(lán)線從右往左
from?cv2?import?cv2
#繪制靜態(tài)畫面和藍(lán)線
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
效果:
三、藍(lán)線從上往下
from?cv2?import?cv2
#繪制靜態(tài)畫面和藍(lán)線
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
效果:
四、藍(lán)線從下往上滑動(dòng):
from?cv2?import?cv2
#繪制靜態(tài)畫面和藍(lán)線
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
效果:
?
總結(jié)
以上是生活随笔為你收集整理的使用python+OpenCV实现抖音特效“蓝线挑战”的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 数独游戏的算法实现
- 下一篇: websocket python爬虫_p