DL mini

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Mini Project : 3

Title - Implement Colorizing Old B&W Images: color old black and white
images to colorful images Project title: Colorizing Old B&W Images using CNN.
Theory - Colorizing black and white images to colorful images involves a
complex process that requires expertise and specialized software. However, here
are some general steps involved in the process: Scan the black and white image:
The first step is to scan the black and white image and convert it into a digital
format.

Preprocessing the Image


To preprocess the image before colorization, follow these steps:
1. Read the Image: Load the image using OpenCV's cv2.imread function.
2. Scale the Image: Convert the image to float32 format and scale its pixel
values to the range [0, 1].
3. Convert to LAB Color Space: Convert the scaled image from BGR to LAB
color space using cv2.cvtColor.
4. Resize the Image: Resize the LAB image to the desired dimensions
(typically required by the colorization model).

Refining the Colorized Image


To refine the colorized image after prediction, take these steps:
1. Convert LAB to BGR: Convert the LAB colorized image to BGR color
space using cv2.cvtColor.
2. Clip Pixel Values: Clip the pixel values of the colorized image to the range
[0, 255].
3. Convert to uint8: Convert the pixel values of the colorized image from
float32 to uint8 data type.
4. Resize Image: Resize the colorized image to match the dimensions of the
original input image.

CaffeModel and Prototxt Use Case in Collecting Data


When using CaffeModel and Prototxt in the context of collecting data for
colorization models, consider the following:
1. Model Architecture: Understand the architecture and requirements of the
colorization model, specified in the Prototxt file.
2. Pretrained Weights: Utilize the pretrained CaffeModel to initialize the
model's parameters, facilitating training on the collected data.
3. Fine-Tuning: Fine-tune the pretrained model on the collected dataset to
improve performance and adapt it to specific colorization tasks.
Source Code -
import numpy as np
import cv2
from cv2 import dnn

#--------Model file paths--------#


proto_file = 'Model\colorization_deploy_v2.prototxt'
model_file = 'Model\colorization_release_v2.caffemodel'
hull_pts = 'Model\pts_in_hull.npy'
img_path = 'images/building.jpg'
#--------------#--------------#

#--------Reading the model params--------#


net = dnn.readNetFromCaffe(proto_file,model_file)
kernel = np.load(hull_pts)
#-----------------------------------#---------------------#

#-----Reading and preprocessing image--------#


img = cv2.imread(img_path)
scaled = img.astype("float32") / 255.0
lab_img = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)
#-----------------------------------#---------------------#

# add the cluster centers as 1x1 convolutions to the model


class8 = net.getLayerId("class8_ab")
conv8 = net.getLayerId("conv8_313_rh")
pts = kernel.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")]
#-----------------------------------#---------------------#

# we'll resize the image for the network


resized = cv2.resize(lab_img, (224, 224))
# split the L channel
L = cv2.split(resized)[0]
# mean subtraction
L -= 50
#-----------------------------------#---------------------#

# predicting the ab channels from the input L channel

net.setInput(cv2.dnn.blobFromImage(L))
ab_channel = net.forward()[0, :, :, :].transpose((1, 2, 0))
# resize the predicted 'ab' volume to the same dimensions as our
# input image
ab_channel = cv2.resize(ab_channel, (img.shape[1], img.shape[0]))

# Take the L channel from the image


L = cv2.split(lab_img)[0]
# Join the L channel with predicted ab channel
colorized = np.concatenate((L[:, :, np.newaxis], ab_channel), axis=2)

# Then convert the image from Lab to BGR


colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
colorized = np.clip(colorized, 0, 1)

# change the image to 0-255 range and convert it from float32 to int
colorized = (255 * colorized).astype("uint8")

# Let's resize the images and show them together


img = cv2.resize(img,(640,640))
colorized = cv2.resize(colorized,(640,640))

result = cv2.hconcat([img,colorized])

cv2.imshow("Grayscale -> Colour", result)

cv2.waitKey(0)

OUTPUT

Conclusion- In this way coloring B & W images is Implemented.

You might also like