程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python grayscale image colorization

編輯:Python
1️⃣ Job requirements

Given a grayscale image , Use any method to turn it into a color image , And try to make the added colors appear more real .

2️⃣ Core code

Here we call directly eccv16 and siggraph17 Model of , These two will be downloaded automatically when the program is run model file , Then after the image preprocessing , Put it into the model for coloring , Finally, output the colored image .

import argparseimport matplotlibimport matplotlib.pyplot as pltfrom 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()# Load Colorer 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") resolutionsimg = load_img("imgs/test2.jpg")(tens_l_orig, tens_l_rs) = preprocess_img(img, HW=(256,256))if(opt.use_gpu): tens_l_rs = tens_l_rs.cuda()# colorizer outputs 256x256 ab map# resize and concatenate to original L channelimg_bw = postprocess_tens(tens_l_orig, torch.cat((0*tens_l_orig,0*tens_l_orig),dim=1))out_img_eccv16 = postprocess_tens(tens_l_orig, colorizer_eccv16(tens_l_rs).cpu())out_img_siggraph17 = postprocess_tens(tens_l_orig, colorizer_siggraph17(tens_l_rs).cpu())plt.imsave('%s_eccv16.png'%opt.save_prefix, out_img_eccv16)plt.imsave('%s_siggraph17.png'%opt.save_prefix, out_img_siggraph17)plt.figure(figsize=(16,4))plt.subplot(1,4,1)plt.imshow(img,aspect='auto')plt.title('Original')plt.axis('off')plt.subplot(1,4,2)plt.imshow(img_bw,aspect='auto')plt.title('Input')plt.axis('off')plt.subplot(1,4,3)plt.imshow(out_img_eccv16,aspect='auto')plt.title('Output (ECCV 16)')plt.axis('off')#plt.subplot(1,4,4)plt.imshow(out_img_siggraph17,aspect='auto')plt.title('Output (SIGGRAPH 17)')plt.axis('off')plt.show()
3️⃣ experimental result

Color three randomly selected test pictures . First of all, the first one is the original image we entered , Then convert the input original image into gray image , That's the second picture . Then we use 16 year ECCV Network model for color prediction , Get the result of processing , The third picture , And finally we use 17 year siggraph Fast shader model for automatic coloring , Get the result of the fourth figure . By contrast, we can see , The similarity between the coloring result and the original image is still very high , And there's no sense of disobedience , The effect is robust .

test1 It's Mr. Zhou , The color of Zhou Dong's clothes and piano are exactly the same , It's just that the background light is different .

test2 It's blue sky and white clouds , The reduction degree of natural scenery is the highest , Almost the same as the original picture .( I guess GAN Discriminator in Discriminator I can't recognize it. It's a colored fake picture .)

test3 yes Taylor Swift, Old mold is still beautiful , After coloring , Compared with the original picture , Just lack some sense of beauty .

Experiment source code + The report

In the field of computer vision , Things that are intuitively natural to humans , It is very difficult in computer vision . An image is just a numerical matrix in a computer . What is expressed in this matrix , Computers have no concept , This is also the problem that machine learning should solve now .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved