opencv 仿照 张靓颖 《Body First》 制作艺术视频

原文链接: opencv 仿照 张靓颖 《Body First》 制作艺术视频

上一篇: opencv 二值化的素描

下一篇: numpy tile 和 opencv repeat 堆叠操作 点阵

https://space.bilibili.com/21808504

思想

将转换为素描的图片和背景图片进行与操作,找到两张图片的交点

将交点部分色素调暗即可

背景

eea7960869226cf30c06e5bf969e1e66d36.jpg

加入内容

c1cd0fa5809a1154f88485be054ea7a7528.jpg

完整代码

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

WHITE = 255
DEEP = 155
LIGHT = 180


# rgb 转 gray 转素描格式 0-255
def parse(frame):
    img_gray = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
    img_blurred = cv.GaussianBlur(img_gray, (5, 5), 0)
    img_threshold1 = cv.adaptiveThreshold(img_blurred, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 5, 2)
    img_threshold1_blurred = cv.GaussianBlur(img_threshold1, (5, 5), 0)
    _, img_threshold2 = cv.threshold(img_threshold1_blurred, 200, 255, cv.THRESH_BINARY)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
    img_opening = cv.bitwise_not(cv.morphologyEx(cv.bitwise_not(img_threshold2), cv.MORPH_OPEN, kernel))
    img_opening_blurred = cv.GaussianBlur(img_opening, (3, 3), 0)
    return img_opening_blurred


#  rgb 格式的 img 和 bg
#  返回 合成图,大小和bg一样 返回 灰度图
def merge(img, bg):
    img = parse(img)
    bg = cv.cvtColor(bg, cv.COLOR_RGB2GRAY)
    bg[bg < 220] = LIGHT
    bg[bg > 220] = WHITE
    # plt.hist(bg.flatten(), bins=255, range=(0, 255))
    # plt.show()

    img = cv.resize(img, bg.shape[::-1])

    # 捕获黑线线条
    mask_img = img <= LIGHT
    mask_bg = bg == LIGHT

    mask = np.logical_and(mask_img, mask_bg)
    bg[mask] = DEEP

    return bg


def parse_video(video_path):
    capture = cv.VideoCapture(video_path)
    bg = cv.imread('./bg.jpg')
    fourcc = cv.VideoWriter_fourcc(*"mp4v")
    size = bg.shape[:2][::-1]
    print('size', size)  # (1080, 1920)
    out_file = cv.VideoWriter("./out.mp4", fourcc, 30, (1920, 1080))

    while capture.isOpened():
        ret, frame = capture.read()
        if not ret:
            break
        out = merge(frame, bg)
        out = cv.cvtColor(out, cv.COLOR_GRAY2BGR)
        out_file.write(out)
    out_file.release()


def main():
    parse_video('./video/cxk.mp4')
    # bg = cv.imread('./bg.jpg')
    # img = cv.imread('./img/cxk.jpg')
    #
    # out = merge(img, bg)
    # # plt.imshow(out, cmap='gray')
    # # plt.show()
    # plt.hist(out.flatten(), bins=255, range=(0, 255))
    # plt.show()
    #
    # cv.imshow("out", out)
    # cv.imwrite('./out.jpg', out)
    # cv.waitKey()


if __name__ == '__main__':
    main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值