Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

use code provided and in python please You need to write a function compress (img, k, debug=False) that take an image array img, a parameter

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribeduse code provided and in python please

You need to write a function compress (img, k, debug=False) that take an image array img, a parameter k that controls compression, and optionally a parameter debug that is used to visualize variable values and images. The function should return a reduced array using a SVD compression method shown below. Your function should work with the following code: import matplotlib.pyplot as plt from numpy import linalg as la import numpy as np import matplotlib.image as mpimg if __name__= '_-_ain_': \# read the image file and display the image img = mpimg.imread('octopus.jpg') plt.imshow(img) plt.show() \# analyze the image array print(img.ndim) print(img.shape) print(img.dtype) print(img.max()) print(img.min()) img_approx = compress(img, 50) plt.imshow(img_approx) plt.show() plt.imsave("octopus_new.jpg", img_approx) Write your function and the main code into a file svd_compress.py. The file octopus.jpg is located in Files/Labs. The Singular Value Decomposition (SVG) is one of the methods to decompose matrix into three matrices that can be used later to generate an approximation of an image. Let's see how it can be done. Given a matrix A, the following product can be computed: UVT=A where U and V are square and is the same size as A. is a diagonal matrix and contains the singular values of A, organized from largest to smallest. These values are always non-negative and can be used as an indicator of the 'importance' of some features represented by the matrix A. Choosing how many values on the diagonal are important allows us to disregard 'insignificant' data and compress the image file. Let's practice with image files. All image files are represented as multidimensional arrays. In this lab, we will use a file that has RGB colors. There are other files that use RGBA, grayscale, or CMYK color schemas. If you use other files in your future projects, you need to adjust the code accordingly. First, we import an image file and check its attributes: Next, we decompose the image matrix using SVD and verify that our decomposition worked correctly using the equation UVT=A : \# find SVD U, s, Vt = la.svd(red) print(U.shape, s.shape, Vt. shape) \# verify SVD Sigma = np.zeros((X,Y)) \# convert s (a 10 array) into Sigma (a 2D-array) for i in range(X): Sigma[i, i] = 5[i] print(np.allclose(red, U @ Sigma e Vt)) \# verify the equality of the original array and decomposed array NOTE: the above snippet is applied to the red pixels only! Now, we can truncate the array and approximate the red color pixel values by assigning k=50 (or any other number less than Y ): \# choose significant values in the diagonal of SVD k=50 red_approx = U @Sigma[ [:,:k]@Vt[:k,:] print(red_approx.shape) plt.imshow(red_approx, cmap="Reds") plt.show() You can do the same procedure for other color arrays, green and blue. You need to use the same k value. After you are done, you need to combine the arrays into one array that approximates the original image as shown below. Sometimes, you may need to normalize the pixels: img_approx = np.stack ((red, green, blue), axis=2) img_approx = img_approx img_approx.min () img_approx = img_approx / img_approx.max () plt.imshow(img_approx) plt.show( )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions