Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

The following code do the Discrete Cosine Transform ( DCT ) and Inverse DCT with 6 4 coefficients. % matplotlib inline import matplotlib.pyplot as plt

The following code do the Discrete Cosine Transform(DCT) and Inverse DCT with 64 coefficients.
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np
from sklearn.datasets import load_sample_image
from scipy.fftpack import dctn, idctn
from scipy.spatial.distance import pdist
# helper functions to extract patches from an image
def im_to_patch(im, patch_size):
return im.reshape(im.shape[0]// patch_size[0], patch_size[0], im.shape[1]// patch_size[1], patch_size[1]).swapaxes(1,2).reshape(-1, patch_size[0], patch_size[1]).copy()
def patch_to_im(patches, patch_size, im_size):
return patches.reshape(im_size[0]// patch_size[0], im_size[1]// patch_size[1], patch_size[0], patch_size[1]).swapaxes(1,2).reshape(im_size).copy()
def im_to_vec(im, patch_size):
return im.reshape(im.shape[0]// patch_size[0], patch_size[0], im.shape[1]// patch_size[1], patch_size[1]).swapaxes(1,2).reshape(-1, patch_size[0]* patch_size[1]).copy()
def vec_to_im(vec, patch_size, im_size):
return vec.reshape(im_size[0]// patch_size[0], im_size[1]// patch_size[1], patch_size[0], patch_size[1]).swapaxes(1,2).reshape(im_size).copy()
# accuracy metric - MSE
def mse(original, noisy):
return np.sum((original - noisy)**2)
china = load_sample_image('china.jpg').mean(axis=2)[:424, :]/256
china_dct = dctn(im_to_patch(china,[8,8]), axes=(1,2))
x, y = np.mgrid[:8, :8]
n_coefs =64
china_dct64= np.where(np.logical_and(x np.sqrt(n_coefs), y np.sqrt(n_coefs)), china_dct,0)
china_rc64= patch_to_im(idctn(china_dct64, axes=(1,2)),[8,8],[424,640])/256
print('64 Components (MSE ={:g})'.format(mse(china, china_rc64)))
plt.imshow(china_rc64, cmap='plasma')
plt.show()
The following code visualizes the basis vectors used to reconstruct each patch of the image:
from matplotlib import colors
def make_dct_basis(coefs, patch_size):
basis = np.empty(tuple(coefs)+ tuple(patch_size))
x, y = np.mgrid[:patch_size[0], :patch_size[1]]
for i in range(coefs[0]):
for j in range(coefs[1]):
basis[i, j]= np.cos(np.pi *(x +.5)* i / coefs[0])* np.cos(np.pi *(y +.5)* j / coefs[1])
return basis
dct_vecs = make_dct_basis([8,8],[64,64])
fig, axes = plt.subplots(8,8, figsize=(9,9))
ims =[]
for i in range(8):
for j in range(8):
ims +=[axes[i, j].imshow(dct_vecs[i, j], cmap='binary')]
axes[i, j].set_axis_off()
vmin = min([im.get_array().min() for im in ims])
vmax = max([im.get_array().max() for im in ims])
norm = colors.Normalize(vmin=vmin, vmax=vmax)
for im in ims:
im.set_norm(norm)
fig.suptitle('DCT Basis Patches')
plt.show()
According the things mentioned above, which DCT basis vectors will likely be used to represent the selected regions (ROIs) within the image below? Provide the rationale and write a python code to test this hypothesis.
ROI 1
ROI 3
ROI 2
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_2

Step: 3

blur-text-image_3

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

Entity Alignment Concepts Recent Advances And Novel Approaches

Authors: Xiang Zhao ,Weixin Zeng ,Jiuyang Tang

1st Edition

9819942527, 978-9819942527

More Books

Students explore these related Databases questions