利用matplotlib,实现少帅下飞机动态效果

在video_path中更改自己视频的位置即可

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import cv2

video_path = 'D:/python/Practice/Leader.mp4'  #存放视频的路径
commander = cv2.VideoCapture(video_path)

speed_factor = 2  # 视频播放速度的倍数
fig, ax = plt.subplots(figsize=(10, 6))

def update(frame):
    for _ in range(speed_factor):
        ret = commander.grab()
    # 读取视频帧
    ret, frame = commander.read()
    if not ret:
        return

    # 将图像转换为灰度图像
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # 应用 Canny 边缘检测
    edges = cv2.Canny(gray_frame, 100, 200)

    # 进行膨胀操作扩大边缘
    kernel = np.ones((10,10), np.uint8)  # 膨胀的核大小可以调整
    edges_dilated = cv2.dilate(edges, kernel)

    output_image = np.ones_like(frame) * 255  # 创建一个白色的图像,BGR格式

    # 将边缘位置的像素设置为蓝色
    output_image[edges_dilated != 0] = [0, 0, 255]  # BGR格式,蓝色

    # 清空当前轴并绘制新图像
    ax.clear()
    ax.imshow(output_image)

# 创建动画
ani = animation.FuncAnimation(fig, update, frames=20, interval=50)
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值