Hello peeps! Hope you are doing great! Today, I just thought of a process to restore colors in black and white images and soon I started implementing it. I guess you must also have wondered about it. So, here is a handy blog to help you in that.


In this tutorial, you will learn how to colorize black and white images using OpenCV, Deep Learning, and Python.

Image colorization is the process of taking an input grayscale (black and white) image and then producing an output colorized image that represents the semantic colors and tones of the input

There is a well trained deep learning model that has been trained on how does colored photographs look when they are converted into grayscale. Note that each color that is recognized by the human eye heavily depends upon what intensity it has been falling with. Studying that pattern between the color and its converted black and white version can help in restoring back the color.

LAB image format is a way of storing the images just like the RGB one. L- luminosity ( our eyes have greater than 90% light receptors and rest colour ones.). A- for red-green ad B- for blue-yellow. This image format helps us study that pattern easily.

Notice how in this sample image, the color of Thor’s armor has been restored correctly to red. The trees and the complexion has been colorized correctly too!

Importing the libraries

We will be using Numpy to do the mathematics and cv2 for image processing.

import numpy as np
import cv2

Loading the model

Next, we’ll define variables that will hold the path to our caffe protoxt file, pre-trained model, NumPy cluster center points file and to input black and white image.

protxt= "model/colorization_deploy_v2.prototxt"
model= "model/colorization_release_v2.caffemodel"
points= "model/pts_in_hull.npy"
image= "images/bx1.jpeg"

Next, we’ll be loading our Caffe model and cluster center points. OpenCV can read Caffe models via the cv2.dnn.readNetFromCaffe function. We are using np.load() as the cluster point file is in numpy format.

net = cv2.dnn.readNetFromCaffe(protxt, model)
pts = np.load(points)

Now, we’ll be loading the centers for ab channel quantization. We’ll treat each of the points as 1×1 convolutions and add them to the model.

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")]

Pre-processing the image

Now, we’ll load the input image using cv2.imread() and then scale and convert them to type float. These calculations are specific to the deep learning model chosen. The image is then converted in LAB image format.

image = cv2.imread(image)
scaled = image.astype("float32") / 255.0
lab = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)

Next, we’ll be resizing the file in 224×224 dimension and then we’ll split the ‘L’ channel . Just a reminder, intensity of the light plays a major role in color determination.

resized = cv2.resize(lab, (224, 224))
L = cv2.split(resized)[0]
L -= 50

Now we can pass the input L channel through the network to predict the ab channels

ab = net.forward()[0, :, :, :].transpose((1, 2, 0))

We’ll resize the predicted ‘ab’ volume to the same dimensions as our input image

ab = cv2.resize(ab, (image.shape[1], image.shape[0]))

Post-processing the image

Now, its time for post-processing. Post processing includes:

  • Grabbing the L channel from the original input image and concatenating the original L channel and predicted ab channels together which results in colorized image.
  • Converting the colorized image from the Lab color space to RGB
  • Clipping any pixel intensities that fall outside the range [0, 1] 
  • Bringing the pixel intensities back into the range [0, 255]. We divided by 255, during the pre-processing steps and now during post-processing, we are multiplying by 255 . The scaling and “uint8” conversion isn’t a requirement but that it helps the code work between OpenCV 3.4.x and 4.x versions.

So, we’ll start by grabbing the L channel and concatenating it with predicted ab channels.

L = cv2.split(lab)[0]
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)

Next, we’ll be converting the LAB image to RGB one and restoring the values within 0-255 range.

colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
colorized = np.clip(colorized, 0, 1)
colorized = (255 * colorized).astype("uint8")

Finally, both our original image and colorized image are displayed on the screen!

cv2.imshow("Original", image)
cv2.imshow("Colorized", colorized)


sWe have successfully build and executed the code to restore colors in black and white images. If you liked my work feel free to share it and use the comments section for feedbacks.

-Suniti Jain


eunice richard · July 12, 2020 at 9:49 am

You can certainly see your skills in the paintings you write.
The sector hopes for even more passionate writers like you
who are not afraid to say how they believe. Always go after your heart.

clinton toth · July 12, 2020 at 10:23 am

Valuable information. Fortunate me I found your web site by chance, and
I’m stunned why this twist of fate didn’t took place earlier!
I bookmarked it.

Kapat · July 12, 2020 at 10:55 am

I reckon something truly interesting about
your web blog so I saved to my bookmarks.

leonard floyd · July 12, 2020 at 11:01 am

Merely wanna state that this is very beneficial, Thanks for taking your time to write this.

florine macdonald · July 12, 2020 at 11:18 am

Incredible story there.

colette furey · July 12, 2020 at 11:57 am

Sweet web site, super design, really clean and employ genial.

garfield sayre · July 12, 2020 at 12:41 pm

Ahaa, its pleasant discussion regarding this article here at this webpage, I have read all
that, so at this time me also commenting at this place.

guillermo pownall · July 12, 2020 at 1:45 pm

This page certainly has all the information I wanted concerning this subject and didn’t know who to ask.

tina stover · July 12, 2020 at 2:17 pm

Thanks for some other informative blog. The place else could I get that kind of info written in such an ideal approach?
I’ve a project that I’m simply now running on, and I’ve been on the look out for such info.

martina ransom · July 12, 2020 at 2:57 pm

I think this is one of the most significant info for me.
And i am glad reading your article. But should remark on some general things, The website style is great, the articles is really
excellent 😀 Good job, cheers

leonard good · July 12, 2020 at 3:05 pm

It’s fantastic that you are getting ideas
from this piece of writing as well as from our dialogue made
at this place.

lilla corones · July 12, 2020 at 3:07 pm

Oh my goodness! Incredible article dude! Many thanks, However I am having troubles with your RSS.
I don?t understand why I can’t join it. Is there anyone
else having similar RSS issues? Anybody who knows
the answer can you kindly respond? Thanks!!

tiara garrison · July 12, 2020 at 3:44 pm

Very good article. I definitely appreciate this site. Thanks!

ellieash more · July 12, 2020 at 3:50 pm

Definitely, what a magnificent blog and illuminating posts, I will bookmark your site.All the Best!

georgia maskell · July 12, 2020 at 4:00 pm

After looking at a number of the blog posts on your website,
I honestly appreciate your technique of blogging. I saved as
a favorite it to my bookmark site list and will be checking back
soon. Please check out my website too and tell me how you feel.

krista drechsler · July 12, 2020 at 4:30 pm

obviously like your web-site however you need to take a look
at the spelling on quite a few of your posts. Many of them are rife with spelling problems and I to find
it very troublesome to tell the reality then again I’ll
certainly come back again.

mirtao flynn · July 12, 2020 at 4:40 pm

Great post.

reed neely · July 12, 2020 at 4:55 pm

I saw a lot of website but I conceive this one holds something extra in it.

barbara linares · July 12, 2020 at 5:11 pm

Post writing is also a fun, if you be familiar with afterward you can write or else it is
complicated to write.

hung oatley · July 12, 2020 at 5:46 pm

Quality posts is the important to be a focus for the viewers to visit the site,
that’s what this web site is providing.

marcia gertz · July 12, 2020 at 6:35 pm

Greetings! Very helpful advice in this particular article!

It’s the little changes that produce the largest changes.
Thanks for sharing!

uta mcdonagh · July 12, 2020 at 6:37 pm

Hello, you used to write great, but the last few posts
have been kinda boring… I miss your tremendous writings.

Past few posts are just a little out of track! come

karise ward · July 12, 2020 at 7:03 pm

Utterly composed subject matter, Really enjoyed reading through.

wilma clegg · July 12, 2020 at 7:58 pm

Hi there, I discovered your website by way of Google even as looking for a similar subject, your website came
up, it appears great. I’ve bookmarked it in my google bookmarks.[X-N-E-W-L-I-N-S-P-I-N-X]Hi
there, simply was aware of your blog via Google, and located
that it is really informative. I am going to be careful for brussels.

I will be grateful when you proceed this in future.

Numerous other people will probably be benefited out of your writing.

Jack · July 12, 2020 at 8:05 pm

Ahaa, its pleasant discussion about this post here at this weblog, I
have read all that, so now me also commenting at this
place. It’s the best time to make a few plans
for the long run and it’s time to be happy. I have read
this put up and if I could I wish to counsel you few attention-grabbing things or tips.
Perhaps you could write subsequent articles regarding this article.
I desire to learn more issues approximately it!
It’s perfect time to make some plans for the longer term and it’s time to be happy.
I have read this put up and if I may I wish to counsel you few attention-grabbing
issues or suggestions. Maybe you can write next articles referring to this article.
I wish to read even more things about it!

clinton cole · July 12, 2020 at 9:00 pm

Hey there! I know this is kind of off topic but I was wondering which blog platform
are you using for this site? I’m getting fed up of WordPress because
I’ve had issues with hackers and I’m looking
at alternatives for another platform. I would be great if you could point
me in the direction of a good platform.

yolanda eleano · July 12, 2020 at 9:36 pm

Glad to be one of many visitants on this awful website :D.

trisha vanotterloo · July 12, 2020 at 9:45 pm

Hi, I do believe this is a great site. I stumbled upon it 😉 I will return yet again since i have bookmarked it.
Money and freedom is the best way to change, may you be rich and continue
to help other people.

https://liwanspace.com/fabulous-and-super-all-natural-skincare-techniques-for-arranging-women-with-dry-skin/ · July 12, 2020 at 11:32 pm

Really instructive and superb body structure of content material, now that’s user genial 🙂

liwan space · July 12, 2020 at 11:58 pm

You made some nice points there. I did a search on the subject and found most
persons will consent with your site.

pete benham · July 13, 2020 at 1:04 am

We are a group of volunteers and opening a new scheme in our
community. Your site provided us with valuable info to work on. You have done a formidable job and our entire community
will be thankful to you.

Hannah · July 13, 2020 at 1:08 am

I read this article fully concerning the difference of hottest
and earlier technologies, it’s awesome article.

chris montez · July 13, 2020 at 1:13 am

A lot of thanks for all your valuable labor on this site.
Kim enjoys doing investigations and it’s really simple to
grasp why. Most people hear all regarding the lively ways you present functional
secrets via this blog and attract participation from other ones on this issue
so my simple princess has been learning a great deal.
Have fun with the rest of the new year. Your performing a glorious job.

caleb puglisi · July 13, 2020 at 1:21 am

You are my inspiration, I possess few web logs and often run out from brand 🙂

candelaria binette · July 13, 2020 at 1:54 am

I would like to thnkx for the efforts you have
put in writing this blog. I am hoping the same high-grade
site post from you in the upcoming as well. Actually
your creative writing abilities has encouraged me to
get my own site now. Actually the blogging is spreading its wings rapidly.

Your write up is a great example of it.

Esther Dowd · July 13, 2020 at 2:02 am

Greetings I am so excited I found your blog, I really found you by error, while I was researching on Google
for something else, Anyhow I am here now and would just like to say thanks a lot for a tremendous post and a all
round thrilling blog (I also love the theme/design). I have bookmarked it and also included your RSS feeds,
so when I have time I will be back to read much more, Please do
keep up the excellent job.

jerri monte · July 13, 2020 at 2:18 am

I every time used to read post in news papers but now as I am a user of web therefore from now I am using net for articles, thanks to web.

erme linda · July 13, 2020 at 2:39 am

Hi there, I enjoy reading through your post. I wanted to write a little comment to support you.

werner tolley · July 13, 2020 at 3:01 am

I think you have mentioned some very interesting points, regards
for the post.

abigail hinz · July 13, 2020 at 4:08 am

Hey There. I found your weblog using msn. This is an extremely
smartly written article. I’ll make sure to bookmark it and return to read more of your helpful info.
Thank you for the post. I’ll certainly return.

byron childers · July 13, 2020 at 4:08 am

Very soon this website will be famous among all blog users, due to it’s fastidious content

tayla black · July 13, 2020 at 4:27 am

I know this web page provides quality based posts
and additional information, is there any other website which gives these
data in quality?

Bella · July 13, 2020 at 5:07 am

Great post. I was checking continuously this blog
and I am impressed! Extremely useful information specially the last part 🙂 I care for such information much.
I was looking for this particular info for a very long time.

Thank you and good luck.

sherrill hilliard · July 13, 2020 at 5:26 am

Pretty nice post. I just stumbled upon your blog and wanted to say that I’ve really enjoyed surfing around your weblog posts.
After all I’ll be subscribing on your feed and I am hoping you write again very soon!

Dr Diabetic Ingredients · July 13, 2020 at 5:47 am

Heya i’m for the first time here. I came across this board and I find It really useful & it
helped me out a lot. I hope to give something back and aid others like
you helped me.

Also visit my blog post; Dr Diabetic Ingredients

marko winchester · July 13, 2020 at 6:06 am

Just desire to say your article is as amazing. The clearness to your submit is simply great and i could assume you are a
professional on this subject. Well along with
your permission allow me to seize your feed to keep up to date with imminent
post. Thank you a million and please continue the rewarding work.

ramon tancred · July 13, 2020 at 6:14 am

Some truly interesting points you have written.Helped me
a lot, just what I was looking for 😀

danny bower · July 13, 2020 at 6:16 am

This paragraph is actually a nice one it helps new the web viewers, who are wishing for blogging.

ity.im · July 13, 2020 at 6:38 am

hello! I really like your writing very a lot!
proportion we keep in touch extra approximately your article
on AOL? I require a specialist on this area to unravel my problem.
May be that’s you! Taking a look forward to see you.

senaida blakey · July 13, 2020 at 6:47 am

Sweet blog! I found it while browsing on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Appreciate it!

nigel cleary · July 13, 2020 at 6:50 am

There is definitely a lot to know about this issue. I love all the
points you made.

annetta timmons · July 13, 2020 at 7:09 am

Helpful information. Fortunate me I found your website by accident, and I am shocked why this
twist of fate didn’t came about earlier! I bookmarked

Gravitas Keto Reviews · July 13, 2020 at 7:22 am

Some genuinely great info, Glad I observed this.

madec · July 13, 2020 at 7:29 am

Have you ever considered publishing an e-book or guest authoring on other websites?
I have a blog based on the same ideas you discuss and would love to have you share some
stories/information. I know my readers would
enjoy your work. If you are even remotely interested, feel free
to shoot me an email.

leilade gotardi · July 14, 2020 at 3:25 am

Thank you for another wonderful post. Where else could anyone get
that type of information in such a perfect means of
writing? I’ve a presentation next week, and I am on the look for such info.

sergioa brahams · July 14, 2020 at 4:36 am

What’s up, yes this paragraph is in fact fastidious and
I have learned lot of things from it about blogging. thanks.

evelyn salier · July 14, 2020 at 4:53 am

Wow! This could be one particular of the most useful blogs
We have ever arrive across on this subject. Basically
Wonderful. I am also an expert in this topic therefore I can understand your effort.

davis benavides · July 14, 2020 at 5:56 am

As I am a web site possessor I believe the content material here is rattling magnificent ,
appreciate it for your efforts. You should keep it up forever!
Good Luck.

chris chambliss · July 14, 2020 at 7:05 am

I could not resist commenting. Very well written!

Santia · July 14, 2020 at 8:08 am

Super-Duper site! I am loving it!! Will come back again. I am bookmarking your feeds also

Watt · July 14, 2020 at 8:41 am

Wow, that’s what I was seeking for, what a information! existing here
at this blog, thanks admin of this site.

Taip · July 14, 2020 at 10:10 am

You have noted very interesting points! ps nice site.

Dora mila · July 14, 2020 at 10:45 am

Hello my loved one! I wish to say that this article is
awesome, nice written and include approximately all significant
infos. I would like to peer more posts like this .

Mahima · July 14, 2020 at 1:26 pm

Hi! I could have sworn I’ve been to this blog before but after reading through some of the
post I realized it’s new to me. Anyways, I’m definitely happy I found
it and I’ll be book-marking and checking back frequently!

Camila · July 14, 2020 at 3:14 pm

Dead indited content, Really enjoyed studying.

Goto · July 16, 2020 at 5:48 pm

I have to thank you for the efforts you’ve put in writing this
site. I’m hoping to view the same high-grade blog posts by you in the future as well.

In truth, your creative writing abilities has inspired me to get
my own site now 😉

Here is my web blog: Goto

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Additional settings
Formula color
Text color
Type math using LaTeX
Nothing to preview