日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

使用python+OpenCV实现抖音特效“蓝线挑战”

發(fā)布時(shí)間:2024/1/23 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用python+OpenCV实现抖音特效“蓝线挑战” 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用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è)步驟:

  • 讀取原始視頻信息(寬、高、fps和總幀數(shù));
  • 計(jì)算藍(lán)線在視頻中的移動(dòng)速度,橫向移動(dòng):寬÷總幀數(shù),縱向移動(dòng):高÷總幀數(shù);
  • 分解原視頻每一幀畫面,計(jì)算藍(lán)線在當(dāng)前幀中的位置;
  • 繪制當(dāng)前靜態(tài)畫面和藍(lán)線;
  • 將繪制后的畫面作為幀生成視頻文件。
  • 以藍(lán)線從左往右為例:

  • 首先使用OpenCV讀取原始視頻,獲取視頻信息并通過(guò)視頻寬與總幀數(shù)計(jì)算藍(lán)線移動(dòng)速度;
  • 藍(lán)線從左往右移動(dòng),當(dāng)藍(lán)線滑過(guò)第一幀,第一幀畫面上滑過(guò)的部分將作為靜態(tài)畫面保留到下一幀。因此需要定義一個(gè)img來(lái)保留之前幀生成的畫面;
  • 當(dāng)讀取到下一幀畫面frame后,根據(jù)當(dāng)前藍(lán)線所處位置對(duì)當(dāng)前幀frame進(jìn)行裁切,用裁切后的畫面替換掉img上相同位置的畫面。(需要注意的是image_block=frame[0:height,x:width],這里是圖像高在前寬在后);
  • 生成畫面之后再使用OpenCV繪制藍(lán)線cv2.line(img,(int(x + 3),0),(int(x + 3),height),(255,0,0),3),藍(lán)線寬度設(shè)定為3個(gè)像素,最后將處理好的畫面寫入視頻文件。
  • 一、藍(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)題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。