风格类型
模型 | 风格(翻译可能有误) |
---|
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()
实现效果
参考资料
- OpenCV4学习笔记(69)——dnn模块之基于fast_style模型实现快速图像风格迁移
评论 (0)