使用python+opencv快速实现画风迁移效果

jupiter
2021-11-19 / 0 评论 / 685 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年11月25日,已超过883天没有更新,若内容或图片失效,请留言反馈。

风格类型

模型风格(翻译可能有误)
candy糖果
composition_vii康丁斯基的抽象派绘画风格
feathers羽毛
udnie乌迪妮
la_muse缪斯
mosaic镶嵌
the_wave海浪
starry_night星夜
the_scream爱德华·蒙克创作绘画风格(呐喊)

模型下载脚本

BASE_URL="http://cs.stanford.edu/people/jcjohns/fast-neural-style/models/"

mkdir -p models/instance_norm
cd models/instance_norm
curl -O "$BASE_URL/instance_norm/candy.t7"
curl -O "$BASE_URL/instance_norm/la_muse.t7"
curl -O "$BASE_URL/instance_norm/mosaic.t7"
curl -O "$BASE_URL/instance_norm/feathers.t7"
curl -O "$BASE_URL/instance_norm/the_scream.t7"
curl -O "$BASE_URL/instance_norm/udnie.t7"

mkdir -p ../eccv16
cd ../eccv16
curl -O "$BASE_URL/eccv16/the_wave.t7"
curl -O "$BASE_URL/eccv16/starry_night.t7"
curl -O "$BASE_URL/eccv16/la_muse.t7"
curl -O "$BASE_URL/eccv16/composition_vii.t7"
cd ../../

代码调用

import cv2
import matplotlib.pyplot as plt
import os

# 定义调用模型进行风格迁移的函数
def convert_img(model_path,img):
    # 加载模型
    net = cv2.dnn.readNetFromTorch(model_path)
    net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV) #设置后端

    # 从图像中创建四维连通区域
    w,h,_ = img.shape
    blob = cv2.dnn.blobFromImage(img,1.0,(h,w))

    # 图片流经模型
    net.setInput(blob)
    out = net.forward()

    # 模型输出修正
    out = out.reshape(3,out.shape[2],out.shape[3])
    out = out.transpose(1,2,0)
    out = out/255.0
    
    return out

# 读取图片
img = cv2.imread("./img/img2.jpg")

# 调用模型进行风格迁移
out = convert_img("./model/feathers.t7",img)
    
# 模型效果展示
plt.figure(dpi=200)
plt.subplot(1,2,1)
plt.title("origin")
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.subplot(1,2,2)
plt.title("feathers")
plt.imshow(cv2.cvtColor(out, cv2.COLOR_BGR2RGB))
plt.show()

实现效果

参考资料

  1. OpenCV4学习笔记(69)——dnn模块之基于fast_style模型实现快速图像风格迁移
0

评论 (0)

打卡
取消