Mustansar Lab 06
Mustansar Lab 06
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
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).
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.
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.
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:
Where C,M,YC, M, YC,M,Y are the cyan, magenta, and yellow values, and
the key is a fixed color adjustment factor.
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.
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.
Convert CMY to RGB with key color 0.412 where the formula is
𝑹 𝑮 𝑩 = 𝟐𝟓𝟓 ∗ 𝟏 − 𝑪, 𝟏 − 𝑴, 𝟏 − 𝒀 ∗ 𝟏 – 𝒌𝒆𝒚
CODE:
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:
# 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])
# Apply the mask to the original image to extract the blue areas
blue_areas = cv2.bitwise_and(image, image, mask=mask)
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:
Apply HSV and RAINBOW color map to the gray scale image
CODE:
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.