Lab Report 2
Lab Report 2
ENGINEERING
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
return image
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
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
# 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
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)
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
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)
Output:
9|Page