0% found this document useful (0 votes)
21 views

Colorized Image by JUNAID

Uploaded by

Beauty Tips
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Colorized Image by JUNAID

Uploaded by

Beauty Tips
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

COLORIZED IMAGE

1. import numpy as np
 Imports the NumPy library, a powerful tool for numerical computing in Python.
 Assigns it the alias np for convenience, allowing you to use its functions and objects
with np. instead of the full name numpy..
Key capabilities of NumPy:
 Efficient array data structures: Handles large, multi-dimensional arrays efficiently,
making it ideal for numerical computations.
 Vectorized operations: Performs operations on entire arrays element-wise, often much
faster than Python's built-in lists.
 Mathematical functions: Offers a wide range of mathematical functions for linear
algebra, Fourier transforms, random number generation, and more.
Common uses:
 Data analysis and manipulation
 Scientific computing
 Machine learning and artificial intelligence
2. import cv2
 Imports the OpenCV library (Open Source Computer Vision Library), a
comprehensive toolkit for real-time computer vision.
Key capabilities of OpenCV:
 Image and video processing: Reads, displays, manipulates, and analyzes images and
videos.
 Feature detection and extraction: Detects objects, faces, features, and patterns in
images.
 Video analysis: Tracks objects, analyzes motion, and performs video-related tasks.
 Camera access: Captures live video from webcams or other devices.
Common uses:
 Image and video processing
 Computer vision applications (e.g., object detection, tracking, face recognition)
 Augmented reality
 Robotics
Together, NumPy and OpenCV are often used in tandem for:
 Handling image and video data in arrays
 Performing numerical computations on image data
 Implementing computer vision algorithms and techniques
1. import streamlit as st
 Imports the Streamlit library, a powerful tool for rapidly building and sharing data-driven
web applications in Python.
 Assigns it the alias st for easier use in code.
2. from PIL import Image, ImageFilter
 Imports specific modules from the Pillow library:
o Image: Provides tools for opening, manipulating, and saving various image file
formats.
o ImageFilter: Offers a collection of filters for applying image effects (e.g.,
blurring, sharpening).
3. import os

JUNAID MALIK (03041659294)


 Imports the Python os module for interacting with the operating system.
o Commonly used to access and manage files and directories.
Together, these libraries are often used to create interactive web applications that:
 Display and manipulate images: Streamlit provides the framework for building the web
app, while Pillow handles image processing tasks.
 Involve visual elements and user interactions: Streamlit offers widgets for buttons,
sliders, text inputs, and other interactive elements.
 Access and process files from the file system: The os module allows app components to
interact with files on the user's computer.
Common use cases:
 Building image-based web apps for tasks like image classification, object detection, or
photo editing.
 Creating interactive visual data exploration tools.
 Developing simple image-focused web applications without extensive web development
knowledge.

Breakdown of custom_image_colorizer function:


1. Grayscale Conversion and RGB Compatibility:
 gray_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY): Converts the input
image (presumably in BGR format) to grayscale using OpenCV's cvtColor function.
 gray_img_rgb = cv2.cvtColor(gray_img, cv2.COLOR_GRAY2RGB): Converts the
grayscale image back to RGB format for compatibility with later steps in the
model, which likely expects color information.
2. Deep Learning Model and Cluster Points Load:
 proto_txt_path, model_path, and points_path: These lines define the paths to the
model's protocol text file, the pre-trained model weights, and the cluster points
data, respectively.
 custom_net = cv2.dnn.readNetFromCaffe(proto_txt_path, model_path): Reads the
specified deep learning model from the provided prototxt and caffemodel files using
OpenCV's readNetFromCaffe function.
 custom_cluster_points = np.load(points_path): Loads the cluster points data (likely
representing color values) from the specified NumPy file.
3. Model Layer Modification:
 custom_class8 = custom_net.getLayerId("class8_ab"): Retrieves the ID of the
"class8_ab" layer within the loaded model.
 custom_conv8 = custom_net.getLayerId("conv8_313_rh"): Retrieves the ID of the
"conv8_313_rh" layer.
 custom_cluster_points = custom_cluster_points.transpose().reshape(2, 313, 1,
1): Reshapes the cluster points data into a specific format expected by the "class8_ab"
layer.
 custom_net.getLayer(custom_class8).blobs =
[custom_cluster_points.astype("float32")]: Assigns the reshaped cluster points data to
the "class8_ab" layer as its internal data (blobs).

JUNAID MALIK (03041659294)


 custom_net.getLayer(custom_conv8).blobs = [np.full([1, 313], 2.606,
dtype="float32")]: Sets a constant value of 2.606 in a specific format to the
"conv8_313_rh" layer's blobs.
4. Image Preprocessing for Model:
 custom_scaled_img = gray_img_rgb.astype("float32") / 255.0: Converts the RGB
image to float32 precision and scales its values between 0 and 1.
 custom_lab_img = cv2.cvtColor(custom_scaled_img,
cv2.COLOR_RGB2LAB): Converts the preprocessed image from RGB to LAB color
space, separating lightness (L) from color information (a and b channels).
 resized_lab_img = cv2.resize(custom_lab_img, (224, 224)): Resizes the LAB image to
224x224, likely the expected input size for the deep learning model.
 L_channel_img = cv2.split(resized_lab_img)[0]: Extracts the L channel (lightness)
from the resized LAB image.
 L_channel_img -= 50: Subtracts 50 from the L channel values, potentially for
normalization or bias correction within the model.
5. Predicting Color Channels:
 custom_net.setInput(cv2.dnn.blobFromImage(L_channel_img)): Prepares the L
channel image as input for the deep learning model using
OpenCV's blobFromImage function.
 ab_channel_preds = custom_net.forward()[0, :, :, :].transpose((1, 2, 0)): Runs a
forward pass through the model and retrieves the predicted a and b channels (color
information) from the output. The indexing and transposition likely adjust the format of
the prediction to match the original image size.
6. ab_channel_preds_resized = cv2.resize(ab_channel_preds, (input_img.shape[1], input_img.shape[0]))
 Resizes the predicted a and b channels (color information) to match the original image
size:
o ab_channel_preds: Contains the a and b channels predicted by the model, likely in a
smaller size.
o cv2.resize(): OpenCV function to resize images.
o (input_img.shape[1], input_img.shape[0]): Uses the dimensions of the original input
image to resize the channels accordingly.
7. L_original_img = cv2.split(custom_lab_img)[0]
 Extracts the original L channel (lightness) from the preprocessed LAB image:
o custom_lab_img: The preprocessed image in LAB color space.
o cv2.split(): OpenCV function to split a multi-channel image into its individual
channels.
o [0]: Selects the first channel (L channel) from the split result.
8. custom_colorized_img = np.concatenate((L_original_img[:, :, np.newaxis], ab_channel_preds_resized),
axis=2)
 Combines the original L channel with the resized a and b channels to create a colorized
image in LAB space:
o np.concatenate(): NumPy function to concatenate arrays along a specified axis.
o L_original_img[:, :, np.newaxis]: Adds a new dimension (channel) to the L channel to
make it 3-dimensional for concatenation.
o ab_channel_preds_resized: The resized a and b channels.
o axis=2: Concatenates along the channel dimension (depth).
9. custom_colorized_img_rgb = cv2.cvtColor(custom_colorized_img, cv2.COLOR_LAB2RGB)

JUNAID MALIK (03041659294)


 Converts the colorized image from LAB back to RGB color space for display:
o cv2.cvtColor(): OpenCV function to convert between color spaces.
o custom_colorized_img: The colorized image in LAB space.
o cv2.COLOR_LAB2RGB: Specifies conversion from LAB to RGB.
10. custom_colorized_img_clipped = np.clip(custom_colorized_img_rgb, 0, 1)
 Clips any RGB values outside the 0 to 1 range to ensure valid color values:
o np.clip(): NumPy function to clip values within a specified range.
o custom_colorized_img_rgb: The RGB image after conversion.
o 0, 1: The lower and upper bounds for clipping.
10. custom_colorized_img_uint8 = (255 * custom_colorized_img_clipped).astype("uint8")
 Multiplies the clipped RGB values by 255 and converts them to 8-bit unsigned integers
for display or storage:
o 255: Scales the RGB values from [0, 1] to [0, 255] for 8-bit representation.
o astype("uint8"): Converts the floating-point values to 8-bit unsigned integers.
11. return custom_colorized_img_uint8
 Returns the final colorized image in 8-bit RGB format, ready for display or further
processing.

Explanation of the custom_enhance_image function:


1. Converting to PIL Image:
 pil_img = Image.fromarray(img): Converts the input image (assumed to be a NumPy array)
into a PIL Image object, allowing for image manipulation with Pillow.
2. Sharpening the Image:
 radius = int(2 * sharpness_factor): Calculates the radius for the unsharp mask filter based on
the provided sharpness_factor. A larger radius will increase the sharpening effect.
 sharpened_img = pil_img.filter(ImageFilter.UnsharpMask(radius=radius,
percent=int(sharpness_factor * 100))): Applies the unsharp mask filter to enhance edges and
create a sharper image.
o The percent parameter controls the intensity of the sharpening effect.
3. Creating a White Overlay:
 overlay = Image.new('RGBA', pil_img.size, (255, 255, 255, int(255 * opacity_factor))) : Creates a
new image with the same size as the original image, filled with white color and a semi-
transparent alpha channel.
o The alpha channel's transparency is controlled by the opacity_factor.
4. Blending with Overlay:
 final_img = Image.alpha_composite(sharpened_img.convert('RGBA'), overlay): Blends the
sharpened image (converted to RGBA format) with the white overlay using alpha
compositing. This creates a subtle brightness boost and a "glow" effect.
5. Converting Back to NumPy Array:
 enhanced_img_np = np.array(final_img.convert('RGB')): Converts the final enhanced image (in
RGBA format) back to a NumPy array in RGB format, suitable for further processing or
display.
6. Returning Enhanced Image:
 return enhanced_img_np: Returns the enhanced image as a NumPy array.

JUNAID MALIK (03041659294)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy