Colorization
Neil Haddley • August 3, 2023
colorization.ipynb
I used the instructions from this "Black and white image colorization with OpenCV and Deep Learning" post to add color to a black and white image

Color image generated from a black and white photograph
TEXT
1!pip install opencv-python 2!pip install Pillow 3 4import numpy as np 5import cv2 6from cv2 import dnn 7from PIL import Image 8 9#--------Model file paths--------# 10proto_file = 'colorization_deploy_v2.prototxt' 11model_file = 'colorization_release_v2.caffemodel' 12hull_pts = 'pts_in_hull.npy' 13img_path = 'img1.jpg' 14#--------------#--------------# 15 16#--------Reading the model params--------# 17# net = dnn.readNetFromCaffe(proto_file,model_file) 18net = dnn.readNetFromCaffe(proto_file,model_file) 19 20dnn.readNetFromCaffe 21 22kernel = np.load(hull_pts) 23#-----------------------------------#---------------------# 24 25 26#-----Reading and preprocessing image--------# 27img = cv2.imread(img_path) 28scaled = img.astype("float32") / 255.0 29lab_img = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB) 30#-----------------------------------#---------------------# 31 32# add the cluster centers as 1x1 convolutions to the model 33class8 = net.getLayerId("class8_ab") 34conv8 = net.getLayerId("conv8_313_rh") 35pts = kernel.transpose().reshape(2, 313, 1, 1) 36net.getLayer(class8).blobs = [pts.astype("float32")] 37net.getLayer(conv8).blobs = [np.full([1, 313], 2.606, dtype="float32")] 38#-----------------------------------#---------------------# 39 40# we'll resize the image for the network 41resized = cv2.resize(lab_img, (224, 224)) 42# split the L channel 43L = cv2.split(resized)[0] 44# mean subtraction 45L -= 50 46#-----------------------------------#---------------------# 47 48# predicting the ab channels from the input L channel 49 50net.setInput(cv2.dnn.blobFromImage(L)) 51ab_channel = net.forward()[0, :, :, :].transpose((1, 2, 0)) 52# resize the predicted 'ab' volume to the same dimensions as our 53# input image 54ab_channel = cv2.resize(ab_channel, (img.shape[1], img.shape[0])) 55 56 57# Take the L channel from the image 58L = cv2.split(lab_img)[0] 59# Join the L channel with predicted ab channel 60colorized = np.concatenate((L[:, :, np.newaxis], ab_channel), axis=2) 61 62# Then convert the image from Lab to BGR 63colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR) 64colorized = np.clip(colorized, 0, 1) 65 66# change the image to 0-255 range and convert it from float32 to int 67colorized = (255 * colorized).astype("uint8") 68 69# Let's resize the images and show them together 70img = cv2.resize(img,(640,640)) 71colorized = cv2.resize(colorized,(640,640)) 72 73result = cv2.hconcat([img,colorized]) 74cv2.imshow("Grayscale -> Colour", result) 75cv2.waitKey(0) 76 77color_coverted = cv2.cvtColor(colorized, cv2.COLOR_BGR2RGB) 78 79img = Image.fromarray(color_coverted, 'RGB') 80img.save('img1_converted.jpg') 81img