给定一幅灰度图像,使用任意方法将其变成一幅彩色图像,并尽量使得添加的色彩显得较为真实。
2️⃣核心代码 |
---|
这里我们是直接调用了eccv16和siggraph17的模型,运行程序时会自动下载这两个model文件,然后经过对图像的预处理之后,放入模型中进行上色,最后输出上色后的图像。
import argparse
import matplotlib
import matplotlib.pyplot as plt
from colorizers import \*
matplotlib.use('TKAgg')
parser = argparse.ArgumentParser()
parser.add_argument('-i','--img\_path', type=str, default='imgs/ansel\_adams3.jpg')
parser.add_argument('--use\_gpu', action='store\_true', help='whether to use GPU')
parser.add_argument('-o','--save\_prefix', type=str, default='saved', help='will save into this file with {eccv16.png, siggraph17.png} suffixes')
opt = parser.parse_args()
# 加载上色器
colorizer_eccv16 = eccv16(pretrained=True).eval()
colorizer_siggraph17 = siggraph17(pretrained=True).eval()
if(opt.use_gpu):
colorizer_eccv16.cuda()
colorizer_siggraph17.cuda()
# default size to process images is 256x256
# grab L channel in both original ("orig") and resized ("rs") resolutions
img = load_img("imgs/test2.jpg")
(tens_l_orig, tens_l_rs) = preprocess_img(img, HW=(256,256))
if