Week 8 Programs
Exercises on Introduction to Image Processing
8.1 Intensity Transformations
Intensity transformations involve modifying the pixel values of an image to enhance its
appearance or extract specific details. These transformations include contrast stretching, gamma
correction, and logarithmic scaling. Such modifications help improve image quality for
applications such as medical imaging and satellite image enhancement. By adjusting brightness
and contrast, intensity transformations can highlight important details in an image that might
otherwise go unnoticed.
import cv2
import numpy as np
import [Link] as plt
img = [Link]("[Link]", 0)
if img is None:
print("Error: Image not found")
else:
negative = 255 - img
img f = [Link](np.float32)
c = 255 / [Link](1 + [Link](img_f))
log_trans = np.uint8(c * [Link](1 + img_f))
gamma = 0.5
gamma_trans = np.uint8(255 * (img / 255) ** gamma)
r_min, r_max = [Link](), [Link]()
contrast = np.uint8((img - r_min) / (r_max - r_min) * 255)
titles = ["Original", "Negative", "Log", "Gamma", "Contrast"]
images = [img, negative, log_trans, gamma_trans, contrast]
for i in range(5):
[Link](2, 3, i+1)
[Link](images[i], cmap='gray')
[Link](titles[i])
[Link]('off')
[Link]()
8.2 Image edge-detection
An image edge-detection algorithm detects the edges in each channel of an image using
differences in the intensity values of pixel neighborhoods. Common edge detection methods
include the Sobel operator, Prewitt operator, and Canny edge detector, each with different levels
of sensitivity and accuracy. Edge detection is widely used in object recognition, autonomous
vehicles, and medical image analysis, where precise boundary identification is essential for
decision-making. Given an input image the task is to prepare this image to feed it to the algorithm
for execution with high accuracy and minimal computational complexity.
import cv2
# Get the image path from the user
fname = input("Enter the path of the image to process: ")
# Read the image in grayscale
img = [Link](fname, cv2.IMREAD_GRAYSCALE)
# Check if the image is loaded successfully
if img is None:
print("Error: Could not read the image. Please check the file path.")
else:
# Apply Canny Edge Detection
edges = [Link](img, 100, 200) # Threshold values: 100 and 200
# Display the original and edge-detected images
[Link]("Original Image", img)
[Link]("Edge Detection (Canny)", edges)
[Link](0) # Wait until a key is pressed
[Link]() # Close all OpenCV windows
8.3 Visualizing image in different color spaces
A digital image is typically represented in the RGB (Red, Green, Blue) color space, where each
pixel is defined by a combination of red, green, and blue intensities. However, different color
spaces serve different purposes. The HSV (Hue, Saturation, Value) color space is useful for color-
based segmentation because it separates intensity (Value) from color information. Other color
models, such as CMYK (Cyan, Magenta, Yellow, Black) and LAB color space, provide additional
advantages in specific image processing applications. Converting between color spaces allows
better analysis and manipulation of image content.
import cv2
import [Link] as plt
# Read the image (BGR format)
img = [Link]("[Link]")
if img is None:
print("Error: Image not found")
else:
# Convert color spaces
img_rgb = [Link](img, cv2.COLOR_BGR2RGB)
img_gray = [Link](img, cv2.COLOR_BGR2GRAY)
img_hsv = [Link](img, cv2.COLOR_BGR2HSV)
img_lab = [Link](img, cv2.COLOR_BGR2LAB)
img_ycbcr = [Link](img, cv2.COLOR_BGR2YCrCb)
# Display images
[Link](figsize=(10, 8))
[Link](2, 3, 1)
[Link](img_rgb)
[Link]("RGB")
[Link]("off")
[Link](2, 3, 2)
[Link](img_gray, cmap='gray')
[Link]("Grayscale")
[Link]("off")
[Link](2, 3, 3)
[Link](img_hsv)
[Link]("HSV")
[Link]("off")
[Link](2, 3, 4)
[Link](img_lab)
[Link]("LAB")
[Link]("off")
[Link](2, 3, 5)
[Link](img_ycbcr)
[Link]("YCbCr")
[Link]("off")
plt.tight_layout()
[Link]()
8.4 Gray scaling of Images
Gray scaling is the process of converting an image from other color spaces e.g. RGB, CMYK, HSV,
etc. to shades of gray. It varies between complete black and complete white. Dimension
reduction: For example, In RGB images there are three color channels and three dimensions while
gray scale images are single-dimensional. Reduces model complexity: Consider training neural
articles on RGB images of 10x10x3 pixels. The input layer will have 300 input nodes. On the other
hand, the same neural network will need only 100 input nodes for gray scale images.
Method 1:
# Visualizing image in different color spaces
import cv2
image = [Link]('[Link]')
[Link]('Original', image)
[Link](0)
# Use the cvtColor() function to grayscale the image
gray_image = [Link](image, cv2.COLOR_BGR2GRAY)
[Link]('Grayscale', gray_image)
[Link](0)
# Window shown waits for any key pressing event
[Link]()
Method 2:
import cv2
# Use the second argument or (flag value) zero
# that specifies the image is to be read in grayscale mode
img = [Link]('[Link]', 0)
[Link]('Grayscale Image', img)
[Link](0)
# Window shown waits for any key pressing event
[Link]()
Method 3:
import cv2
import numpy as np
# Load the input image
img = [Link]('[Link]')
img = [Link](np.float32)
# Obtain the dimensions of the image array using the shape method
row,col,channels = [Link]
# Take the average of pixel values of the BGR Channels
# to convert the colored image to grayscale image
for i in range(row):
for j in range(col):
# Find the average of the BGR pixel values
img[i, j] = sum(img[i, j])*0.33
img = [Link](np.uint8)
[Link]('Grayscale Image', img)
[Link](0)
# Window shown waits for any key pressing event
[Link]()
8.5 To find width and height of an image
Determining the dimensions of an image is a fundamental step in image processing. The width
and height of an image define its resolution and are crucial for tasks such as resizing, cropping,
and feature extraction. In Python, the OpenCV library allows users to retrieve an image’s
dimensions using the .shape attribute. Knowing the size of an image is essential for various
applications, including deep learning, where input image dimensions must be consistent for
neural network training.
import cv2
# Read the image
img = [Link]("[Link]")
# Check if image loaded
if img is None:
print("Error: Image not found")
else:
height, width, channels = [Link]
print("Width :", width)
print("Height:", height)
Output:
Width : 289
Height: 175