Colorization of images using CNN in Python
In this tutorial program, we will learn about the colorization of images using deep learning techniques using the Python programming language. So here I am going to discuss what are the basic steps of this deep learning problem and how to approach it.
We are using Convolutional Neural Networks(CNN) for this problem. It is used in Computer Vision or OpenCV problems.
Colorization of images using ConVet in Python:
- A Convolutional Neural Network (CNN) is a Deep Learning algorithm that can take in an input image, assign weights and biases to various objects in the image.
- The pre-processing is required in CNN is much lower as compared to other Machine learning algorithms.
- The problems solved using CNN include Image classification, object Detection, and Neural Style transfer. Many activation functions are available in this.
The libraries that need to be imported for this are:
import libraries NumPy and cv2.
import numpy as np import argparse import cv2
Loading data and conversion of images and passing through the layers:
- After loading the images, convert all images from the one color space to other color spaces respectively.
- The input to the network is trained and the network is used to predict the outcomes.
- Load the input image from imread function present in OpenCV, Scale the image accordingly.
ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", type=str,required=True, help="path to input black and white image") ap.add_argument("-p", "--prototxt", type=str,required=True, help="path to Caffe prototxt file") ap.add_argument("-m", "--model", type=str,required=True, help="path to Caffe pre-trained model") ap.add_argument("-c", "--points", type=str,required=True, help="path to cluster center points") args = vars(ap.parse_args()) print("loading model...") net = cv2.dnn.readNetFromCaffe(args["prototxt"],args["model"]) pts = np.load(args["points"]) class8 = net.getLayerId("class8_ab") conv8 = net.getLayerId("conv8_313_rh") pts = pts.transpose().reshape(2, 313, 1, 1) net.getLayer(class8).blobs = [pts.astype("float32")] net.getLayer(conv8).blobs = [np.full([1, 313], 2.606,dtype="float32")] print("load the input image from disk, scale the pixel intensities to therange [0,1], and then converting the image from BGR to the Lab colorspace") image = cv2.imread(args["image"]) scaled = image.astype("float32") / 216.0 lab = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)
Resize the images and colorization:
- We will resize the input image to 216×216, the required input dimensions for the neural network.
- Scaling the predicted volume to the same coordinates as our input image. After this, we scale the predicted volume to be the same coordinates as our input image.
- We are also reshaping the image.
The channel from the original image is appended with the predicted channels.
scale - cv2.scale(216,216) L -= 50 net.setInput(cv2.dnn.blobFromImage(L)) ab = net.forward()[0, :, :, :].transpose((1, 2, 0)) ab = cv2.resize(ab, (image.shape, image.shape)) print(" grab the 'L' channel from the *original* input image (not the resized one) and concatenate the original 'L' channel with the predicted 'ab' channels") L = cv2.split(lab) colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
- The channels from the input image and appending of the original channel and predicted channels together obtaining colorized images.
Convert the colorized image from one random color space to the standard space of color.
- Finally, we obtain the colorized image of our original grayscale image.
colorized = cv2.Color(colorized, cv2.COLOR_LAB2BGR) colorized = np.clip(color, 0, 1) #colorised images colorized = (216 * image).astype("ufloat32") colorized = (216 * colorized).astype("uint8") #to show output cv2.imshow("orig", image) cv2.imshow("Color", colorized) cv2.waitKey(1)
The Sample Output image: