Assignment-4 NumPy Applications
Assignment-4 NumPy Applications
Spring 2024
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 = cv2.imread(‘image.png’,cv2.IMREAD_COLOR )
cv2.imshow(‘OUTPUT’, img)
Matplot Library
pip install matplotlib
import matplotlib.pyplot as plt
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 “Book1.csv”
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 (Book1.csv, Book2.csv, and Book3.csv)
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 a.PNG. 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 a.PNG) and display its grayscale version.
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 a.png. 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.
---*---