BAHRIA UNIVERSITY, ISLAMABAD
Department of Computer Science
Digital Image Processing
Title: Introduction to Python For DIP
Objectives: To study and understand the working of the basics of Python, a high level computing
language for algorithmic development, data visualization and data analysis.
Tools Used: Python
Procedure: Open VS/Colab and perform the following tasks
Task 1: This task involves studying the first two chapters of the "Python Getting Started Guide".
Task 2:
1. Creating a matrix and finding the sum of diagonal elements
[2, 4, 6], [8, 10, 8], [2, 10, 4]
2 Given a matrix A, write statement(s) to find the sum of the diagonal elements of A.
3 Now print sumOfDiag.
4 User Input:
x = float(input('What is the original value? '))
5 Using the colon operator, create a vector V with values [100 98 96, …., 0].
Digital Image Processing Lab Journal 1
print(V)
1. 100 is the starting value of the vector.
2. :-2: indicates that the values in the vector should be decremented by 2 each time.
3. 0 is the ending value of the vector.
4. So, this code will generate a vector V with values starting from 100, then 98, then
96, and so on, until it reaches 0.
6 Extract the second column of a matrix and store it in a vector.
Extract the second row of a matrix and store it in a vector.
7 Create a new matrix of size 6,6.
Extract the 1st, 3rd and 5th row of matrix into another matrix.
8 Multiply each element of a matrix A by 10 and store the result in matrix newMatrix. Display
result.
Digital Image Processing Lab Journal 1
Task 3:
Write and explain the output of the following statements.
1. import numpy as np
A = np.array([[2, 4, 6],
[8, 10, 8],
[2, 10, 4]])
Output:
2. import numpy as np
X = np.array([[1, 2],
[3, 4]])
Y = np.array([[1, 0],
[0, 1]])
# Element-wise multiplication
Digital Image Processing Lab Journal 1
B=X*Y
Output:
3. import numpy as np
# Create array similar to (0:2:10)
# Perform operations similar to [x x.^2]
Output:
Task 4
Create a 2 x 2 matrix. Write a PYTHON program which loads this file in a matrix A. Find the determinant and
inverse of this matrix. Multiply the matrix with its inverse and display the result.
Digital Image Processing Lab Journal 1
You can achieve this in Python using NumPy. First, you'll need to create a file containing the 2x2 matrix.
Then, you can load the file into a NumPy array, find its determinant and inverse, multiply the matrix with
its inverse, and display the result. Here's a step-by-step guide:
Create a file containing the 2x2 matrix. Let's name it matrix.txt:
12
34
Now, you can write a Python program to load this file, find the determinant and inverse, multiply the matrix
with its inverse, and display the result. Here's the code:
Task 5
a) Write a program that loads iris data set (that comes with Python):
b) Find the mean and standard deviation of the sepal length, sepal width, petal length and petal width
for each category of iris.
Digital Image Processing Lab Journal 1
Digital Image Processing Lab Journal 1