0% found this document useful (0 votes)
7 views11 pages

Mustansar Lab 06

The document outlines a laboratory exercise on color models and image processing, specifically focusing on converting images from CMY to RGB, extracting blue colors using HSV, and applying colormaps to grayscale images. It details the objectives, methodologies, and results of each task, emphasizing the significance of color spaces in image processing. The conclusion highlights the practical applications of these techniques in areas such as image segmentation and object recognition.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Mustansar Lab 06

The document outlines a laboratory exercise on color models and image processing, specifically focusing on converting images from CMY to RGB, extracting blue colors using HSV, and applying colormaps to grayscale images. It details the objectives, methodologies, and results of each task, emphasizing the significance of color spaces in image processing. The conclusion highlights the practical applications of these techniques in areas such as image segmentation and object recognition.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

NATIONAL UNIVERSITY OF TEHNOLOGY

COMPUTER ENGINEERING DEPARTMENT

IMAGE PROCESSING AND ANALYSIS LAB

LAB NO-6
Implementation of Color Models and Color Image Processing

SUBMITTED BY:
Mustansar Munir
NUTECH-ID:
 F-22604030
SUBMITTED TO:
 DR.MADIHA ARSHAD
 Objective:

Convert the image from CMY to RGB using a specified key color (0.412).

 SOFTWARE:

GOOGLE COLAB

 Theory and Background

1. Color Spaces and Image Processing

In image processing, different color spaces are used to represent the colors in
images. Some of the most commonly used color spaces are:

 RGB (Red, Green, Blue): The most widely used color space in digital
images, where each pixel is represented by three values corresponding to the
intensity of red, green, and blue.
 HSV (Hue, Saturation, Value): A color space that separates the chromatic
content (hue) from the intensity (saturation and value). HSV is often
preferred in applications like color detection because it separates color
information (hue) from intensity, making it more robust for operations like
color filtering.
 CMY (Cyan, Magenta, Yellow): Often used in color printing, the CMY
model is the inverse of RGB. CMY is useful for color printing or in some
color correction algorithms.
 Grayscale: A single-channel image representing intensity, where each pixel
is a shade of gray (from black to white).

2. Applying Colormaps to Grayscale Images

Colormaps like HSV and Rainbow are applied to grayscale images for better
visual representation of intensity variations. These transformations allow the
intensity information to be visually enhanced, making it easier to identify patterns
or features that might not be obvious in a pure grayscale image.

 HSV Colormap: This colormap uses the hue to represent intensity levels,
with saturation and value remaining constant. It provides a visually
appealing way to map intensity variations onto a colorful scale.
 Rainbow Colormap: The rainbow colormap maps intensity values to the
colors of the rainbow. It is useful for highlighting extreme values and
creating visually attractive color-coded images.

3. Blue Color Extraction

In image processing, extracting specific colors (like blue letters) from an image is
useful for tasks such as text detection or color-based segmentation. In this task, the
image is converted to the HSV color space, and a color range for blue is defined.
A mask is then applied to the image, filtering out all non-blue areas, leaving only
blue-colored pixels. This technique is commonly used in image segmentation and
object recognition.

 Methodology and Procedure

1. CMY to RGB Conversion with Key (Key=0.412)

 Objective: Convert the image from CMY to RGB using a specified key
color (0.412).
 Procedure:
1. The RGB image is converted into CMY by subtracting each channel
from 1.
2. The conversion formula provided is then applied to the CMY image,
adjusting the RGB values based on the key value of 0.412.
3. The converted RGB values are clipped to ensure they are within valid
RGB ranges (0-255) and then displayed.
 Key Formula:

R,G,B=255×(1−C,1−M,1−Y)×(1−key)R, G, B = 255 \times (1 - C, 1 - M, 1 -


Y) \times (1 - \text{key})R,G,B=255×(1−C,1−M,1−Y)×(1−key)

Where C,M,YC, M, YC,M,Y are the cyan, magenta, and yellow values, and
the key is a fixed color adjustment factor.

2. Blue Letter Extraction (HSV Color Space)

 Objective: Extract the blue-colored regions (like blue letters) from a given
image.
 Procedure:
1. The image is loaded and converted to the HSV color space using
OpenCV. HSV is a more effective color space for color-based
segmentation than RGB.
2. A color mask is defined for the blue color, using a specific Hue
range (100-130), Saturation range (0-255), and Value range (0-
255).
3. A binary mask is created using cv2.inRange(), which highlights only
the blue regions in the image.
4. The original image is bitwise AND with the mask, leaving only the
blue regions.
5. The extracted regions are displayed.

3. Applying HSV and Rainbow Colormap to Grayscale Image

 Objective: Enhance the visualization of a grayscale image by applying HSV


and Rainbow colormaps.
 Procedure:
1. The grayscale image is loaded.
2. The HSV colormap is applied using cv2.applyColorMap(), which
converts the grayscale image into a colorful image where pixel
intensity is mapped to the HSV color range.
3. The Rainbow colormap is similarly applied, providing another
colorful representation of the intensity variations in the image.
4. Both the HSV and Rainbow color-mapped images are displayed
alongside the original grayscale image for comparison.

Results and Analysis

1. CMY to RGB Conversion (Key=0.412)

 Result: The image is successfully converted from CMY to RGB using the
provided formula. The resulting image adjusts the color intensities based on
the key value, creating a color transformation that might look more "muted"
or adjusted depending on the image content.
 Analysis: This conversion is useful for simulating a printing process or
understanding how different color spaces interact. The key parameter allows
fine control over the color transformation, which can be adjusted depending
on the application (e.g., color correction or printing simulations).
2. Blue Letter Extraction (HSV Color Space)

 Result: The blue-colored regions (letters) are successfully extracted from the
image. The mask filters out non-blue regions, leaving only the areas where
the hue corresponds to blue.
 Analysis: By using the HSV color space, the extraction of blue-colored
objects becomes more robust and easier to manipulate. The result highlights
how powerful the HSV color space is for detecting specific colors,
especially in varied lighting conditions.

3. Applying HSV and Rainbow Colormap to Grayscale Image

 Result: The grayscale image is enhanced by applying the HSV and


Rainbow colormaps, providing a more visually engaging representation of
intensity variations.
 Analysis: The HSV colormap provides smooth transitions between colors,
enhancing the visualization of intensity. The Rainbow colormap highlights
extremes and can help identify patterns or features that may not be
immediately visible in the grayscale image. Both colormaps are useful for
different purposes: HSV is more natural and aesthetically pleasing, while the
Rainbow colormap can emphasize contrast more dramatically.

LAB TASK NO-1:

Convert CMY to RGB with key color 0.412 where the formula is

𝑹 𝑮 𝑩 = 𝟐𝟓𝟓 ∗ 𝟏 − 𝑪, 𝟏 − 𝑴, 𝟏 − 𝒀 ∗ 𝟏 – 𝒌𝒆𝒚

CODE:

# Step 1: Import necessary libraries


import cv2
import numpy as np
import matplotlib.pyplot as plt

# Step 2: Read the image from the given path


image = cv2.imread('/content/puppu.jpg') # Replace with your image path
if different

# Step 3: Check if image was loaded successfully


if image is None:
print("Error: Image not found. Please check the file path.")
else:
# Step 4: Convert image from BGR (OpenCV default) to RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Step 5: Convert RGB to CMY (normalized)


cmy = 1 - (image_rgb / 255.0)

# Step 6: CMY to RGB conversion using key = 0.412


key = 0.412
rgb_from_cmy = 255 * (1 - cmy) * (1 - key)

# Step 7: Clip and convert to uint8 for display


rgb_from_cmy = np.clip(rgb_from_cmy, 0, 255).astype(np.uint8)

# Step 8: Display original and converted images using matplotlib


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image_rgb)
plt.title('Original RGB Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(rgb_from_cmy)
plt.title('RGB from CMY (key = 0.412)')
plt.axis('off')

plt.tight_layout()
plt.show()

OUTPUT:
LAB TASK NO-2:

Extract blue color letters from the given image where the color range is
(100,0,0) to (130,255,255)

CODE:

# Install required packages


!pip install opencv-python-headless matplotlib

# Import necessary libraries


import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load the image (make sure it's uploaded or present in /content/)


image = cv2.imread('/content/kukuu.jpg') # Replace with your image
filename

# Check if image was loaded


if image is None:
print("Error: Image not found.")
else:
# Convert BGR image to HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# Define the HSV color range for blue (H: 100 to 130, S: 0 to 255, V:
0 to 255)
lower_blue = np.array([100, 0, 0])
upper_blue = np.array([130, 255, 255])

# Create a mask for blue color


mask = cv2.inRange(hsv, lower_blue, upper_blue)

# Apply the mask to the original image to extract the blue areas
blue_areas = cv2.bitwise_and(image, image, mask=mask)

# Convert the result to RGB for better visualization


blue_areas_rgb = cv2.cvtColor(blue_areas, cv2.COLOR_BGR2RGB)

# Display the original image and the extracted blue areas


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(blue_areas_rgb)
plt.title('Extracted Blue Letters')
plt.axis('off')

plt.tight_layout()
plt.show()
OUTPUT:

LAB TASK NO-3:

Apply HSV and RAINBOW color map to the gray scale image

CODE:

# Install required packages


!pip install opencv-python-headless matplotlib

# Import necessary libraries


import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load the image (make sure it's uploaded or present in /content/)


image = cv2.imread('/content/gray scaled.jpg', cv2.IMREAD_GRAYSCALE) #
Replace with your grayscale image

# Check if image was loaded


if image is None:
print("Error: Image not found.")
else:
# Apply HSV colormap
hsv_colormap = cv2.applyColorMap(image, cv2.COLORMAP_HSV)

# Apply Rainbow colormap


rainbow_colormap = cv2.applyColorMap(image, cv2.COLORMAP_RAINBOW)

# Display the grayscale image, HSV, and Rainbow colormaps


plt.figure(figsize=(12, 6))

plt.subplot(1, 3, 1)
plt.imshow(image, cmap='gray')
plt.title('Grayscale Image')
plt.axis('off')

plt.subplot(1, 3, 2)
plt.imshow(hsv_colormap)
plt.title('HSV Colormap')
plt.axis('off')

plt.subplot(1, 3, 3)
plt.imshow(rainbow_colormap)
plt.title('Rainbow Colormap')
plt.axis('off')

plt.tight_layout()
plt.show()

OUTPUT:

Conclusion

 CMY to RGB Conversion allows for simulating how colors are printed or
how the CMY model behaves when adjusted with a key.
 Blue Color Extraction using the HSV color space effectively isolates blue
regions, which is useful for tasks like text extraction, color-based
segmentation, and object recognition.
 Applying Colormaps to grayscale images enhances the visual
representation of intensity information, making it easier to interpret the data,
detect patterns, and highlight regions of interest.

These techniques are fundamental in image processing and can be extended to


more complex applications such as image segmentation, object recognition, and
data visualization.

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