Black and white image colorization with OpenCV and Deep Learning
In this article, we’ll create a program to convert a black & white image i.e grayscale image to a colour image. We’re going to use the Caffe colourization model for this program. And you should be familiar with basic OpenCV functions and uses like reading an image or how to load a pre-trained model using dnn module etc. Now let’s discuss the procedure that we’ll follow to implement the program.
- Load the model and the convolution/kernel points
- Read and preprocess the image
- Generate model predictions using the L channel from our input image
- Use the output -> ab channel to create a resulting image
What is the L channel and ab channel? Basically like RGB colour space, there is something similar, known as Lab colour space. And this is the basis on which our program is based. Let’s discuss what it is briefly:
What is Lab Colour Space?
Like RGB, lab colour has 3 channels L, a, and b. But here instead of pixel values, these have different significances i.e :
- L-channel: light intensity
- a channel: green-red encoding
- b channel: blue-red encoding
And In our program, we’ll use the L channel of our image as input to our model to predict ab channel values and then rejoin it with the L channel to generate our final image.
Below is the implementation of all the steps I have mentioned above.
You can try reading the original research paper which implemented this technique – http://richzhang.github.io/colorization/ or, you can create your own model instead of using a pre-trained model.