Lab Report 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

DEPARTMENT OF COMPUTER & SOFTWARE

ENGINEERING

COLLEGE OF E&ME, NUST, RAWALPINDI

EC312 Digital Image Processing


Lab Report - 01

SUBMITTED TO:
LE Ma’am Sundas Ashraf

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

Submission Date:18/02/24

1|Page
Lab-01: Introduction to Python & Python Package
Lab Tasks

Task-1:
Write a function Sort_tuple() that sort a tuple of tuples by its 2nd item and return
tuple.
Input: tuple1 = (('a', 23),('b', 37),('c', 11), ('d',29))
Output: (('c', 11), ('a', 23), ('d', 29), ('b', 37)).
Code:

import numpy as np
import matplotlib as plt
import cv2

tuple_1 = (('a', 23), ('b', 37), ('c', 11), ('d', 29))


print("Tuple: " , tuple_1)

def Sort_tuple(tuple_1):
tuple_1 = list(tuple_1)
for i in range(len(tuple_1)-1):
for j in range(len(tuple_1)-1):
if tuple_1[i][1] > tuple_1[j+1][1]:
temp = tuple_1[j]
tuple_1[j] = tuple_1[j+1]
tuple_1[j+1]= temp
return tuple(tuple_1)

print("Sorted Tuple: ",(Sort_tuple(tuple_1)))

Output:

2|Page
Task-2:
Write a function to get the key of a minimum value from the dictionary data holder:
Input: Dict = {'Physics': 82, 'Math': 65,'history': 75}
Output: Math
Code:

Dict = {'Physics': 82, 'Math': 65,'history': 75}


print("Dictionary: " ,Dict)
def get_min(Dict):
Dict = list(Dict.items())
for i in range(len(Dict) - 1):
for j in range(len(Dict) - 1):
if Dict[j][1] > Dict[j + 1][1]:
temp = Dict[j]
Dict[j] = Dict[j + 1]
Dict[j + 1] = temp

return Dict[0][0]

print("Key of Minimum value in Dictionary : ",get_min(Dict))

Output:

Task-3:
Write a program that takes an integer as an input from the user and represents it in
binary, octal or hexadecimal. The user should specify the output number system i.e.
whether the number should be converted to binary, octal or hexadecimal.

Code:

3|Page
def represent(num,rep):
rep=rep.upper()
if(rep=='B'):
return bin(num)
elif(rep == 'O'):
return oct(num)
elif(rep=='H'):
return hex(num)

num= input("Enter Number you want to convert : ")


rep= input("Enter base of Number system : ")
print("The number after conversion is ",represent(int(num),rep),rep)

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 numpy as np

def normalization(l):
min_val = min(l)
max_val = max(l)
newList = np.array(l)
return (newList - min_val) / (max_val-min_val)

list_1 = [312,41,55,11,5,142,56,1,9]
print("List : ",list_1)
print("After Normalization: \n", normalization(list_1))

4|Page
Output:

Task-5:
Write a Python function that takes a 2D array of size 6*6 representing temperature
readings should be in range of (1 to 50) across the region and returns the average
temperature for each non-overlapping 2x2 grid within the region.
Code:

import numpy as np

matrix = np.random.randint(0,51,(6,6),dtype=int)

print("6x6 Matrix : \n", matrix)


list_1=[]
for i in range(0,5,2):
for j in range(0,5,2):
print("\nfor : (",i,j,") index\n")
print(matrix[i][j]) ," ", print(matrix[i][j + 1]) ," ",
print(matrix[i + 1][j]) ," ", print(matrix[i + 1][j + 1])

list_1.append((matrix[i][j]+matrix[i][j+1]+matrix[i+1][j]+matrix[i+1][j+1])/4
)

print("\nAverage of 2x2 Matrix : ",list_1)

print("After Normalization: \n", normalization(list_1))

Output:

5|Page
6|Page

You might also like