Software Engineering Lab
Spring 2024
Assignment-4: NumPy and its Applications
Learning objectives:
NumPy stands for Numerical Python. NumPy is a Python library used for working with arrays. It also
has functions for working in domain of linear algebra, fourier transform, and matrices. NumPy is a
general-purpose array-processing package. It provides a high-performance multidimensional array
object and tools for working with these arrays. It is the fundamental package for scientific computing
with Python.
Install Dependencies:
Before its application, ensure the following:
Numpy
pip install numpy
import numpy as np
OpenCV
pip install opencv-python
import cv2
img = [Link](‘[Link]’,cv2.IMREAD_COLOR )
[Link](‘OUTPUT’, img)
Matplot Library
pip install matplotlib
import [Link] as plt
Check time for execution
import time
seconds = [Link]()
Once you install the requisite package, do the following tasks.
1. Write a program to load a .csv file as a NumPy 1-D array. Find the maximum and minimum
elements in the array.
Hint: For the data, use the .csv file “[Link]”
2. For the Numpy 1-D array as obtained in Q.1, sort the elements in ascending order.
3. For the sorted Numpy 1-D array as obtained in Q.2, reverse the array and print.
4. Write a program to load three .csv files ([Link], [Link], and [Link])
as a list of Numpy 1-D arrays. Print the means of all arrays as a list.
5. Write a program to read an image, store the image in NumPy 2-D array. For the image
consider [Link]. Display the image. Let the array be X.
Hint: Use OpenCV to work with image.
6. Copy the data in X to another NumPy 2-D Array, say Y. Write a program to multiply two
NumPy 2-D arrays, that is, Z = X×Y.
7. For all the problems 6, repeat without using NumPy and compare the computation times
doing the same with NumPy.
8. Write a program to convert a color image (say [Link]) and display its grayscale version.
9. Plot the pixel intensity histogram using the grayscale image.
Hint: Use matplotlib to plot
10. Create a black rectangle at the position [(40,100) top right, (70, 200) bottom left] in the
grayscale image.
11. For the grayscale image, display the binarized image with thresholds: [50, 70, 100, 150].
Hint: Binarizing is thresholding each pixel value, i.e., if pixel>threshold, then 1 else 0.
12. Consider the color image stored in [Link]. Create a filter of [[-1,-1,-1][0,0,0][1,1,1]], and
multiply this filter to the image and output the resultant image.
Links to the data for this assignment:
[Link] [Link] [Link] [Link]
For Q. 1-4: 5 Marks each.
For Q. 5-12, 10 marks each.
---*---