Updated - Lab Manual - 10 - DIP
Updated - Lab Manual - 10 - DIP
LAB MANUAL 10
LAB OBJECTIVE:
The objective of this lab is to understand & implement Color Image Processing techniques in
Python
3. Brightness Control
4. Contrast Stretching
5. Négative Transformation
6. Threshold
7. Filtering techniques
Histogram-Based Methods
values.
regions.
frequency components.
Color-Based Enhancement
White Balance Adjustment: Corrects color distortions for natural color appearance.
These techniques enhance images for better visual perception and automated analysis.
While true colorization is complex and often requires deep learning, simple techniques like
applying colormaps or using manual transformations can be implemented using OpenCV and
NumPy.
import cv2
import matplotlib.pyplot as plt
plt.subplot(1, 2, 2)
plt.imshow(cv2.cvtColor(color_image, cv2.COLOR_BGR2RGB))
plt.title('Colorized Image')
plt.show()
Output
import cv2
import matplotlib.pyplot as plt
plt.imshow(cv2.cvtColor(color_image, cv2.COLOR_BGR2RGB))
plt.title("Manual Color Mapping")
plt.show()
Output
This code applies the first technique to adjust brightness in a color image (1.jpg) using
cv2.add() (to increase brightness) and cv2.subtract() (to decrease brightness).
It also displays the original, brightened, and darkened images in a montage format.
import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
print("Error: Image not found! Make sure '1.jpg' is in the working
directory.")
else:
# Define brightness factor
brightness_value = 50 # Increase or decrease brightness by this
amount
Output
color image.
his method blends the original image with a zero matrix (black image), adding a brightness
factor (beta).
import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
print("Error: Image not found! Make sure '1.jpg' is in the working
directory.")
else:
# Brightness adjustment using cv2.addWeighted()
alpha = 1.2 # Weight for original image
beta = 50 # Brightness factor
bright_image = cv2.addWeighted(image, alpha, np.zeros(image.shape,
image.dtype), 0, beta)
plt.axis("off")
plt.show()
Explanation
Output
It brightens or darkens an image based on a power-law function, making it great for correcting
This code applies gamma correction to adjust brightness in "1.jpg", then displays the
import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
print("Error: Image not found! Make sure '1.jpg' is in the working
directory.")
else:
# Function to apply Gamma Correction
def adjust_gamma(image, gamma=1.0):
inv_gamma = 1.0 / gamma
table = np.array([(i / 255.0) ** inv_gamma * 255 for i in
np.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
plt.axis("off")
plt.show()
Output
import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
print("Error: Image not found! Make sure '1.jpg' is in the working
directory.")
else:
# Convert image to float32 for precise calculations
image_float = np.float32(image)
# Display images
plt.figure(figsize=(15, 5))
plt.imshow(montage_rgb)
plt.title("Original (Left) | Contrast Stretched (Right)")
plt.axis("off")
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
plt.show()
This method is effective for low-contrast images, improving details without over-enhancing noise.
Output
This transformation flips the brightness levels, making light areas dark and dark areas light
while inverting colors.
import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
print("Error: Image not found! Make sure '1.jpg' is in the working
directory.")
else:
# Apply negative transformation
negative_image = 255 - image
Output
Thresholding is a technique used to segment an image by converting it into binary (black &
white) or filtered color regions.
Unlike grayscale thresholding, for color images, we typically apply thresholding channel-
wise or in a different color space like HSV.
import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
print("Error: Image not found! Make sure '1.jpg' is in the working
directory.")
else:
# Convert image to HSV color space
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
plt.figure(figsize=(12, 5))
plt.imshow(montage_rgb)
plt.title("Original (Left) | Thresholded (Right) - HSV Method")
plt.axis("off")
plt.show()
Explanation
Loads "1.jpg" as a color image.
Output
import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
print("Error: Image not found! Make sure '1.jpg' is in the working
directory.")
else:
# Apply averaging filter
blurred_image = cv2.blur(image, (5, 5))
# Display images
plt.figure(figsize=(12, 5))
plt.imshow(montage_rgb)
plt.title("Original (Left) | Averaging Filter (Right)")
plt.axis("off")
plt.show()
Output
import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
print("Error: Image not found! Make sure '1.jpg' is in the working
directory.")
else:
# Apply Gaussian filter
gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0)
# Display
montage = np.hstack((image, gaussian_blur))
montage_rgb = cv2.cvtColor(montage, cv2.COLOR_BGR2RGB)
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
plt.figure(figsize=(12, 5))
plt.imshow(montage_rgb)
plt.title("Original (Left) | Gaussian Blur (Right)")
plt.axis("off")
plt.show()
Output
import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
print("Error: Image not found! Make sure '1.jpg' is in the working
directory.")
else:
# Apply median filter
median_blur = cv2.medianBlur(image, 5)
# Display
montage = np.hstack((image, median_blur))
montage_rgb = cv2.cvtColor(montage, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(12, 5))
plt.imshow(montage_rgb)
plt.title("Original (Left) | Median Filter (Right)")
plt.axis("off")
plt.show()
Output
import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
print("Error: Image not found! Make sure '1.jpg' is in the working
directory.")
else:
# Define a sharpening kernel
sharpening_kernel = np.array([[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]])
Explanation
Loads "1.jpg" as a color image.
[-1, 9, -1]
Output
Task
Implement an interactive trackbar (slider) to allow real-time control of gamma values and
threshold levels.
Apply edge detection (Sobel/Laplacian) on the processed image to extract key features.
Output