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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

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

使用OpenCV實現抖音“藍線挑戰”特效。原理比較簡單,當藍線在視頻畫面中滑動,然后從滑過的每一幀中截取部分畫面生成一幅靜態圖片,由于上一幀畫面已經定格,那么通過調整下一幀畫面可以生成各種奇怪的場景。

處理過程可分為幾個步驟:

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

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

    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实现抖音特效“蓝线挑战”的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。