18AIL67 LAB MANUAL ANSIHA H KANDACHAR
Digital Image processing Laboratory(18AIL67)
“Transforming pixels into insights: Discovering the world through the power of
image processing”
INDEX
Sl no Program Page no
1 Write a program to read a digital [Link] and display 1
image into 4 quadrants up,down,right and left
2 Write a program to show rotation,scaling and translation 3
of an image
3 Read an image,first apply erosion to the image and then 5
subtract the result from the original,demonstrate the
difference the edge if you use dilation instead of erosion
4 Read an image and extract and display low-level feature 7
such as edges,textures using filtering technique
5 Demonstrate enhancing and segmentation low contrast 9
2D images
18AIL67 LAB MANUAL ANSIHA H KANDACHAR
PROGRAM NO.1
Write a program to read a digital [Link] and display image into 4
quadrants up,down,right and left.
import cv2
# Load the image
image = [Link]("[Link]")
# Get the dimensions of the image
height, width = [Link][:2]
# Split the image into four quadrants
up_left = image[0:height//2, 0:width//2]
up_right = image[0:height//2, width//2:width]
down_left = image[height//2:height, 0:width//2]
down_right = image[height//2:height, width//2:width]
# Display the quadrants
[Link]("Up Left", up_left)
[Link]("Up Right", up_right)
[Link]("Down Left", down_left)
[Link]("Down Right", down_right)
# Wait for a key press and then close the windows
[Link](0)
[Link]()
1
18AIL67 LAB MANUAL ANSIHA H KANDACHAR
OUTPUT
2
18AIL67 LAB MANUAL ANSIHA H KANDACHAR
PROGRAM NO.2
Write a program to show rotation,scaling and translation of an image
import cv2
import numpy as np
# Load the image
image = [Link]("[Link]")
# Get the dimensions of the image
height, width = [Link][:2]
# Define the rotation angle, scaling factor, and translation offsets
angle = 45
scale = 1.5
dx = 50
dy = -100
# Perform rotation, scaling, and translation on the image
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), angle, scale)
rotated_image = [Link](image, rotation_matrix, (width, height))
translation_matrix = np.float32([[1, 0, dx], [0, 1, dy]])
translated_image = [Link](rotated_image, translation_matrix, (width, height))
# Display the original image, rotated image, scaled image, and translated image
[Link]("Original Image", image)
[Link]("Rotated Image", rotated_image)
[Link]("Scaled Image", [Link](image, None, fx=scale, fy=scale))
[Link]("Translated Image", translated_image)
# Wait for a key press and then close the windows
[Link](0)
[Link]()
OUTPUT
ORIGINAL IMAGE
3
18AIL67 LAB MANUAL ANSIHA H KANDACHAR
ROTATED IMAGE
SCALED IMAGE
TRANSLATED IMAGE
4
18AIL67 LAB MANUAL ANSIHA H KANDACHAR
PROGRAM NO.3
Read an image,first apply erosion to the image and then subtract the result from
the original,demonstrate the difference the edge if you use dilation instead of
erosion
import cv2
import numpy as np
# Load the image
image = [Link]("[Link]", cv2.IMREAD_GRAYSCALE)
# Define the erosion kernel and apply it to the image
kernel = [Link]((5, 5), np.uint8)
eroded_image = [Link](image, kernel, iterations=1)
# Subtract the eroded image from the original image
edge_image = [Link](image, eroded_image)
# Display the original image and edge image
[Link]("Original Image", image)
[Link]("Edge Image (Erosion)", edge_image)
# Apply dilation to the image
dilated_image = [Link](image, kernel, iterations=1)
# Subtract the dilated image from the original image
edge_image_dilation = [Link](image, dilated_image)
# Display the edge image with dilation
[Link]("Edge Image (Dilation)", edge_image_dilation)
# Wait for a key press and then close the windows
[Link](0)
[Link]()
OUTPUT
ORIGINAL IMAGE
5
18AIL67 LAB MANUAL ANSIHA H KANDACHAR
EROSION IMAGE
DILATION IMAGE
6
18AIL67 LAB MANUAL ANSIHA H KANDACHAR
PROGRAM NO.4
Read an image and extract and display low-level feature such as edges,textures
using filtering technique
import cv2
import numpy as np
# Load the image
image = [Link]('[Link]', cv2.IMREAD_GRAYSCALE)
# Define the Sobel edge filter kernels
sobel_x = [Link]([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
sobel_y = [Link]([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
# Apply the Sobel edge filters to the image
edge_x = cv2.filter2D(image, -1, sobel_x)
edge_y = cv2.filter2D(image, -1, sobel_y)
# Display the edge images
[Link]('Sobel X', edge_x)
[Link]('Sobel Y', edge_y)
# Define the texture filter kernel
texture = [Link]([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
# Apply the texture filter to the image
texture_image = cv2.filter2D(image, -1, texture)
# Display the texture image
[Link]('Texture', texture_image)
# Wait for a key press and then close the windows
[Link](0)
[Link]()
OUTPUT
Edge SOBEL X
7
18AIL67 LAB MANUAL ANSIHA H KANDACHAR
Edge SOBEL Y
TEXTURE
8
18AIL67 LAB MANUAL ANSIHA H KANDACHAR
PROGRAM NO.5
Demonstrate enhancing and segmentation low contrast 2D images
import cv2
import numpy as np
# Load the low contrast image
image = [Link]('[Link]', cv2.IMREAD_GRAYSCALE)
# Apply adaptive histogram equalization to enhance the contrast
clahe = [Link](clipLimit=2.0, tileGridSize=(8,8))
enhanced_image = [Link](image)
# Display the original and enhanced images
[Link]('Original', image)
[Link]('Enhanced', enhanced_image)
# Apply Otsu's thresholding to segment the image
_, threshold_image = [Link](enhanced_image, 0, 255, cv2.THRESH_BINARY +
cv2.THRESH_OTSU)
# Display the thresholded image
[Link]('Thresholded', threshold_image)
# Wait for a key press and then close the windows
[Link](0)
[Link]()
OUTPUT
THRESHOLDED
9
18AIL67 LAB MANUAL ANSIHA H KANDACHAR
ORIGINAL
ENHANCED
10