Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 5 . Write a function that accepts the name of an image file and an integer s . Use your function from Problem 3

Problem 5. Write a function that accepts the name of an image file and an integer s. Use
your function from Problem 3, to compute the best rank-s approximation of the image. Plot
the original image and the approximation in separate subplots. In the figure title, report the
difference in number of entries required to store the original image and the approximation (use
plt.suptitle()).
Your function should be able to handle both grayscale and color images. Read the image
in and check its dimensions to see if it is color or not. Grayscale images can be approximated
directly since they are represented by 2-dimensional arrays. For color images, let R,G, and B
be the matrices for the red, green, and blue layers of the image, respectively. Calculate the low-
rank approximations Rs,Gs, and Bs separately, then put them together in a new 3-dimensional
array of the same shape as the original image.
(Hint: np.dstack() may be useful for putting the color layers back together.)
Finally, it is possible for the low-rank approximations to have values slightly outside the
valid range of RGB values. Set any values outside of the interval 0,1 to the closer of the two
boundary values.
(Hint: fancy indexing or np.clip() may be useful here.)
To check, compressing hubble_gray.jpg with a rank 20 approximation should appear
similar to Figure 6.3b and save 1,161,478 matrix entries.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions

Question

Understanding Groups

Answered: 1 week ago