0% found this document useful (0 votes)
24 views

Lab Report 2

The document discusses several tasks completed in a digital image processing lab. Task 1 involves creating an image with a border. Task 2 changes the bit level resolution of an image. Task 3 creates an image with colored boxes in the corners. Task 4 performs image normalization. Task 5 generates a distance map from the center of an image.

Uploaded by

Uzair Waheed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lab Report 2

The document discusses several tasks completed in a digital image processing lab. Task 1 involves creating an image with a border. Task 2 changes the bit level resolution of an image. Task 3 creates an image with colored boxes in the corners. Task 4 performs image normalization. Task 5 generates a distance map from the center of an image.

Uploaded by

Uzair Waheed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DEPARTMENT OF COMPUTER & SOFTWARE

ENGINEERING

COLLEGE OF E&ME, NUST, RAWALPINDI

EC312 Digital Image Processing


Lab Report - 02

SUBMITTED TO:
LE Ma’am Sundas Ashraf

SUBMITTED BY:
Uzair Waheed
Reg # 371828
DE- 43 CE

Submission Date:20/02/24

1|Page
Lab-02: Basic Image Processing using Python Packages
Lab Tasks

Task-1:
Create an r x c matrix of ones and pad 10 pixels wide border of zeros across each
side of it, such that its order will become (4+500+4) x (4+500+4) = 508x508. Create a
generic function so that the values (500 and 4) can be passed by the user.
Code:
import cv2
import numpy as np

def borders(rows, columns, border_size):

image = np.ones((rows, columns), dtype=np.uint8) * 255

# Add black borders


image[:border_size, :] = 0 # Top border
image[-border_size:, :] = 0 # Bottom border
image[:, :border_size] = 0 # Left border
image[:, -border_size:] = 0 # Right border

return image

rows = int(input("Enter the number of rows: "))


columns = int(input("Enter the number of columns: "))
borderSize = int(input("Enter padding : "))

result_image = borders(rows, columns,borderSize)

# Display the image


cv2.imshow("Image with Black Borders", result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

2|Page
Output:

Task-2:
Intensity level resolution defines the resolution at bit level i.e. how many bits are used
to represent a pixel value. The more bits we have per pixel, the more levels there are.
For example, a grayscale image has 256 different levels because for each pixel value
we use 8 bits to store the value. If the bits per pixel are decreased to 4, then the
maximum levels that Digital Image Processing Lab Manual 2 we can have is 16.
Similarly, if only 1 bit is used for it then we can have only two levels (or a binary
image). To change the bit level resolution, different levels (or intensities) can be
grouped together to form new levels. For example, for a 3 bits/pixel image: 0 1 Read a
grayscale image (given below) and convert the image to 16 levels, then to 4 levels
and finally to 1. Display all four images.

Code:

import cv2
import numpy as np

img = cv2.imread('task2.jpg', cv2.IMREAD_GRAYSCALE)


cv2.imshow("Real Image ", img)
cv2.waitKey(0)

def bit_lvl_resolution(levels):
img_lvl = np.uint8(img // (256 / levels) * (256 / levels))
return img_lvl

3|Page
img_16lvl = bit_lvl_resolution(16)
cv2.imshow("16 level ", img_16lvl)
cv2.waitKey(0)

img_4lvl = bit_lvl_resolution(4)
cv2.imshow("4 level ", img_4lvl)
cv2.waitKey(0)

img_2lvl = bit_lvl_resolution(2)
cv2.imshow("2 level ", img_2lvl)
cv2.waitKey(0)

Output:

4|Page
5|Page
Task-3:
Write a function to create a white image of 500x500 (or any other size entered by the
user) and then create 4 boxes of Red, Green, Blue and Black respectively on each
corner of the image as shown below. The size of the colored boxes should be 1/8th
the size of the image. (HINT: the arrays of ones and zeros can be in more than 2
dimensions).
Code:
import cv2
import numpy as np

def create_image_with_colored_boxes(rows, columns, box_size_ratio=8):


image = np.ones((rows, columns, 3), dtype=np.uint8) * 255

box_size = max(rows, columns) // box_size_ratio

# top-left
image[:box_size, :box_size] = (0, 0, 255)

# top-right
image[:box_size, -box_size:] = (0, 255, 0)

# bottom-left
image[-box_size:, :box_size] = (255, 0, 0)

# bottom-right

6|Page
image[-box_size:, -box_size:] = (0, 0, 0)

return image

rows = int(input("Enter the number of rows: "))


columns = int(input("Enter the number of columns: "))

result_image = create_image_with_colored_boxes(rows, columns)

# Display the image


cv2.imshow("Image with Colored Boxes", result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Task-4:
Min-Max normalization is one of the normalization techniques that can be used for
numerical data. It is performed using the following operation: Write a function
min_max_normlaization that takes a list as input and performs the min-max
normalization on it. The length of the list can vary.

Code:

import cv2

img = cv2.imread('task4.jpg')
cv2.imshow("Image", img)

7|Page
cv2.waitKey(0)
if img is None:
print('Error: Unable to load image.')
else:
cv2.imshow("Vertical Flip ", img[::-1, :])
cv2.waitKey(0)

cv2.imshow("Horizontal Flip ", img[:, ::-1])


cv2.waitKey(0)

cv2.imshow("Horizontal & vertical Flip ", img[::-1, ::-1])


cv2.waitKey(0)
_

Output:

Task-5:
Create an empty image of zeros of size 501x501 and then create a distance map by
calculating the Euclidian Distance of every pixel from the center. Display the distance
map. It should look something like given here. Enclose you code in the form of a
function and let the user decide whether Euclidian distance should be used for
distance or any other distance formula e.g. City block distance. (Don’t forget to
declare the data type of the array of zeros as uint8):
Code:

import numpy as np
import cv2

def distance_image(size, algorithm):


img = np.zeros((size, size))
center = size // 2
for x in range(size):
for y in range(size):

8|Page
if algorithm == 0: # 0 = Euclidean
dist = np.sqrt(np.square(x - center) + np.square(y - center))
elif algorithm == 1: # 1 = Manhattan
dist = np.abs(x - center) + np.abs(y - center)
elif algorithm == 2: # 2 = Maximum
dist = max(abs(x - center), abs(y - center))
img[x, y] = dist
max_value = np.max(img)
img = (img / max_value) * 255
return np.uint8(img)

image_size = int(input("Enter the size of the image: "))


algorithm = int(input("Enter the algorithm (0 = Euclidean, 1 = Manhattan, 2 =
Maximum): "))
img = distance_image(image_size, algorithm)
cv2.imshow("Distance Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

9|Page

You might also like