Experiment 4
Experiment 4
Experiment No. 4
Question 1
Image Compression :
image compression is about reducing the size of image files so they take up
less storage space or can be transmitted more quickly over the internet.
There are two main ways to compress an image. In lossless compression, no
image data is lost during the process, so you can perfectly recover the
original image after decompression. This is useful when image quality must
be preserved, such as in scientific imaging. Lossy compression, however,
sacrifices some image quality to significantly reduce the file size. This
method is used when a smaller file is more important than perfect quality,
like when uploading photos to a website. JPEG is an example of lossy
compression, where some image details are discarded to reduce file size
while maintaining an acceptable level of visual quality.
Question 2
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
Question 3
Question 5
QUESTION: Observe the output of code in Jupyter notebook (colab) and explain the
code line by line.
ANSWER: First line imports the necessary libraries. numpy is used for numerical
operations and matplotlib.pyplot is used for plotting images.
np.diag(S[:k]) creates a diagonal matrix S_k from the first k singular values.
U[:, :k] and Vt[:k, :] reduce the U and Vt matrices to the first k columns/rows.
np.dot(U_k, np.dot(S_k, Vt_k)) reconstructs the image using the reduced rank
matrices. This approximates the original image using only the top k singular
values.
Question 6
Question 7
QUESTION: Did you find the case study useful? Should we take such application-
oriented case study for further important Data science topics.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
ANSWER: Yes, the case study on image compression using Singular Value Decomposition
(SVD) is useful. It demonstrates practical applications of advanced Numpy
operations and helps in understanding how dimensionality reduction techniques
can be applied to real-world data, like images.
Such application-oriented case studies are beneficial for grasping important data
science topics, as they show how theoretical concepts are implemented in
practice and how they can solve real problems.
Program
PROBLEM You need to perform image compression using Singular Value Decomposition
STATEME (SVD). The goal is to reduce the dimensionality of the image data while
NT: preserving as much of the original image as possible.
1. Create a Synthetic Image: Define a 2D numpy array with shape
(100, 100) to simulate an image.
2. Perform SVD (singular value decomposition): Compute the SVD of
the image matrix.
3. Reconstruct the Image with Reduced Rank: Reconstruct the image
using a limited number of singular values.
4. Plot the Original and Compressed Images: Compare the original and
compressed images.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
# 2. Perform SVD
U, S, Vt = np.linalg.svd(image, full_matrices=False)
plt.subplot(1, 2, 2)
plt.title('Compressed Image')
plt.imshow(compressed_image, cmap='gray')
plt.axis('off')
plt.show()
OUTPUT:
CONCLUSION: From this problem, I learned that by utilizing only a limited number of the
most significant singular values, I can effectively compress an image. This
approach reduces the overall data required for storing the image while
retaining the crucial features. The exercise highlighted the balance between
compression and image quality. By using just the top 20 singular values, I
was able to produce a compressed image that, although less detailed, still
resembled the original.