!apt update
!apt-get install ffmpeg libsm6 libxext6 -y
!pip install opencv-python
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import math
from scipy import ndimage
import cv2
import copy
import warnings
from scipy import fftpack
from scipy.fftpack import fft2, ifft2
import utils
Hit:1 http://security.debian.org/debian-security buster/updates InRelease
Hit:2 http://deb.debian.org/debian buster InRelease
Hit:3 http://deb.debian.org/debian buster-updates InRelease
0% [Working]^C
^C
Requirement already satisfied: opencv-python in /root/venv/lib/python3.7/site-packages (4.5.1.48)
Requirement already satisfied: numpy>=1.14.5 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from opencv-python) (1.19.5)
^C
Traceback (most recent call last):
File "/root/venv/bin/pip", line 8, in <module>
sys.exit(main())
File "/root/venv/lib/python3.7/site-packages/pip/_internal/cli/main.py", line 73, in main
return command.main(cmd_args)
File "/root/venv/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 111, in main
return self._main(args)
File "/root/venv/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 226, in _main
self.handle_pip_version_check(options)
File "/root/venv/lib/python3.7/site-packages/pip/_internal/cli/req_command.py", line 151, in handle_pip_version_check
pip_self_version_check(session, options)
File "/root/venv/lib/python3.7/site-packages/pip/_internal/self_outdated_check.py", line 120, in pip_self_version_check
installed_version = get_installed_version("pip")
File "/root/venv/lib/python3.7/site-packages/pip/_internal/utils/misc.py", line 644, in get_installed_version
working_set = pkg_resources.WorkingSet()
File "/root/venv/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 567, in __init__
self.add_entry(entry)
File "/root/venv/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 623, in add_entry
for dist in find_distributions(entry, True):
File "/root/venv/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2065, in find_on_path
for dist in factory(fullpath):
File "/root/venv/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2127, in distributions_from_metadata
if len(os.listdir(path)) == 0:
KeyboardInterrupt
# Height and width of our images in parts 1 and 2
M = 512
N = 512
im_original = cv2.imread("images/puppy1.png", 0)
im_noisy = utils.noise_image(im_original, 6.0, N, M)
def denoise(img): #Hint: check out the "filter image" function in utils
# Denoise the image with specified method
p = np.asarray(img, dtype=np.uint8) + np.zeros((int(M/2),int(N/2)), dtype=np.uint8)
for i in range(1, int(M/2)-1):
for j in range(1, int(N/2)-1):
neighbors = np.asarray([p[i-1, j], p[i+1, j], p[i, j-1],p[i, j+1], p[i+1, j+1], p[i-1, j+1], p[i+1, j-1], p[i+1, j-1]])
mindist = min(abs(p[i,j] - n) for n in neighbors)
std = np.std(neighbors)
if mindist > std:
p[i, j] = np.mean(neighbors)
return p
plt.imshow(denoise(im_noisy), cmap = 'gray')
plt.show()
def compute_MSE(img, img_new):
for i in range(1, int(N/2)-1):
for j in range(1, int(M/2)-1):
inner = np.sum(np.square(np.subtract(img_new[i,j], img[i,j])))
# including the 1/M and 1/N cause the error curve to become too flat
# in comparison to the goal curve
# ... but maybe that is what we want? Less error is good...
MSE = (1/M)*(1/N)*inner
return MSE
noise_amounts = np.linspace(0,16,8, dtype=np.float64)
errors = []
for i in noise_amounts:
noisy_im = utils.noise_image(im_original, i, M, N)
filtered_im = denoise(noisy_im)
errors.append(compute_MSE(im_original, filtered_im))
goal_curve = [90, 95, 100, 120, 170, 250, 375, 550]
plt.plot(noise_amounts, errors, 'bo--', label='errors')
plt.plot(noise_amounts, goal_curve, 'gs--', label='goal')
plt.legend()
plt.xlabel('Gaussian Noise Added')
plt.ylabel('Reconstruction Error')
plt.show()
im1 = cv2.imread("images/kitten1.png",0)
plt.imshow(im1, cmap='gray')
plt.show()
# Keeping track of function documentaion for reference...
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.fft.fft2.html
# https://numpy.org/doc/stable/reference/generated/numpy.linalg.norm.html#numpy.linalg.norm
def get_coeffs(X):
# Compute discrete (2D) fourier for the image
series = fft2(X)
for n in range (0, int(N/2) -1)
cfts = (1/N)*np.sum(series*(math.e^(-j*k*n*((2*math.pi)/N))))
# Compute the magnitudes of the coefficients
mgs = np.linalg.norm(cfts)
return cfts, mgs
IndentationError: unexpected indent (<ipython-input-61-b153cf121475>, line 9)
coefficients, mags = get_coeffs(im1)
NameError: name 'get_coeffs' is not defined
# Pick 2 values for alpha to show the difference in quality of reconstruction
alpha1 = .99
alpha2 = .01
coeffs1 = utils.get_alpha_coeffs(coefficients, mags, alpha1, N, M)
coeffs2 = utils.get_alpha_coeffs(coefficients, mags, alpha2, N, M)
NameError: name 'coefficients' is not defined
# Keeping track of function documentaion for reference...
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.fft.ifft2.html#scipy.fft.ifft2
def inverse_ft(cfs):
# Get the IDFT
invert = ifft2(cfs)
for n in range (0, int(N/2) -1)
inv = (1/N)*np.sum(invert*(math.e^(-j*k*n*((2*math.pi)/N))))
# Get the magnitudes
inv_mags = np.linalg.norm(inv)
return inv, inv_mags
IndentationError: unexpected indent (<ipython-input-10-c7d6b266272f>, line 7)
inverse1, inverse_mags1 = inverse_ft(coeffs1)
inverse2, inverse_mags2 = inverse_ft(coeffs2)
NameError: name 'inverse_ft' is not defined
f = plt.figure(figsize=(12,12))
plt.subplot(131),plt.imshow(im1, cmap = 'gray')
plt.xlabel('Orginal', fontsize=8), plt.xticks([]), plt.yticks([])
plt.subplot(132),plt.imshow(inverse_mags1, cmap = 'gray')
plt.xlabel("alpha = " + str(alpha1), fontsize=8), plt.xticks([]), plt.yticks([])
plt.subplot(133),plt.imshow(inverse_mags2, cmap = 'gray')
plt.xlabel("alpha = " + str(alpha2), fontsize=8), plt.xticks([]), plt.yticks([])
plt.show()
NameError: name 'inverse_mags1' is not defined
# Computes reconstruction error
def compute_error(img, img_new):
error = compute_MSE(img,img_new)
return error
alphas = np.linspace(.01,.99,25, dtype=np.float64)
errors = np.zeros(len(alphas))
for a in range(len(alphas)):
cfs = utils.get_alpha_coeffs(coefficients, mags, alphas[a], N, M)
icfs = cv2.idft(cfs)
imags = cv2.magnitude(icfs[:,:,0],icfs[:,:,1])/(M*N)
errors[a] = compute_error(im1,imags)
plt.plot(alphas, errors, 'o')
plt.xlabel('alpha')
plt.ylabel('Reconstruction Error')
plt.title('FT Compression/Distortion Trade-Off')
plt.show()
# Redefine image dimensions for part 3.1
N = 35
# Comments reference SciPy Reference guide ->
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.fft.dct.html
def compute_dct(X):
# Set empty array for dct coefficients
dct = np.zeros((N, N), dtype=np.float64)
# Iterate through and compute values for all coefficients
for k in range(N):
for l in range(N): # from scipy DCT type 2
dct = 2*np.sum(X)
# For norm='ortho' both directions are scaled by the same factor of 1/sqrt(N)
dct = 2*dct/np.sqrt(N**2)
return dct
IndentationError: expected an indented block (<ipython-input-78-21f7f10ba0d7>, line 13)
img = cv2.imread("images/dct_sample1.png", 0)
dct = compute_dct(img)
dct2 = fftpack.dct(fftpack.dct(img.T, type=2, norm='ortho').T, type=2, norm='ortho')
# Plot the image next to the transform and the scipy transform
f = plt.figure(figsize=(12,12))
plt.subplot(131),plt.imshow(img, cmap = 'gray')
plt.xlabel('', fontsize=8), plt.xticks([]), plt.yticks([])
plt.title('Original Image')
plt.subplot(132),plt.imshow(dct, cmap = 'gray')
plt.xlabel('', fontsize=8), plt.xticks([]), plt.yticks([])
plt.title('Results of DCT Function')
plt.subplot(133),plt.imshow(dct2, cmap = 'gray')
plt.xlabel('', fontsize=8), plt.xticks([]), plt.yticks([])
plt.title('Scipys DCT Function')
plt.show()
NameError: name 'compute_dct' is not defined
# Redefine image dimensions for part 3.1
N = 256
im1 = cv2.imread("images/puppy3_small.png",0)
plt.imshow(im1, cmap='gray')
plt.show()
# Number of patches
prange = N/8
def partition_img(img):
return
def partitioned_dct(ppatches, K):
# Set empty array of sizes K^2 per patch
return dct_patches
patches = partition_img(im1)
for i in range(8):
for j in range(8):
plt.subplot(8,8,int(1+j+i*8)), plt.imshow(patches[i][j], cmap="gray")
plt.xticks([])
plt.yticks([])
plt.show()
partitioned_img = partition_img(im1)
dct4 = partitioned_dct(partitioned_img, 2)
dct16 = partitioned_dct(partitioned_img, 4)
dct36 = partitioned_dct(partitioned_img, 6)
dct64 = partitioned_dct(partitioned_img, 8)
plt.subplot(141),plt.imshow(dct4[0][0], cmap='gray'), plt.xticks([]),plt.yticks([])
plt.subplot(142),plt.imshow(dct16[0][0],cmap='gray'), plt.xticks([]),plt.yticks([])
plt.subplot(143),plt.imshow(dct36[0][0],cmap='gray'), plt.xticks([]),plt.yticks([])
plt.subplot(144),plt.imshow(dct64[0][0],cmap='gray'), plt.xticks([]),plt.yticks([])
plt.show()
# Compute the quantized version of the specified transform (DCT or DFT)
def quantize(dct_patches):
# Initialize the quantization matrix
Qmtrx = np.zeros((8,8))
Qmtrx[0,:] = [16, 11, 10, 16, 24, 40, 51, 61]
Qmtrx[1,:] = [12, 12, 14, 19, 26, 58, 60, 55]
Qmtrx[2,:] = [14, 13, 16, 24, 40, 57, 69, 56]
Qmtrx[3,:] = [14, 17, 22, 29, 51, 87, 80, 62]
Qmtrx[4,:] = [18, 22, 37, 56, 68, 109, 103, 77]
Qmtrx[5,:] = [24, 36, 55, 64, 81, 104, 113, 92]
Qmtrx[6,:] = [49, 64, 78, 87, 103, 121, 120, 101]
Qmtrx[7,:] = [72, 92, 95, 98, 112, 100, 103, 99]
# Store dimensions of DCT coefficient matrix patches for iteration
# Initialize an empty matrix to store the quantized coefficients
# Compute and store the quantized coefficients
return q_dct
qdct4 = quantize(dct4)
qdct16 = quantize(dct16)
qdct36 = quantize(dct36)
qdct64 = quantize(dct64)
plt.subplot(141),plt.imshow(qdct4[0][0], cmap='gray'), plt.xticks([]),plt.yticks([])
plt.subplot(142),plt.imshow(qdct16[0][0],cmap='gray'), plt.xticks([]),plt.yticks([])
plt.subplot(143),plt.imshow(qdct36[0][0],cmap='gray'), plt.xticks([]),plt.yticks([])
plt.subplot(144),plt.imshow(qdct64[0][0],cmap='gray'), plt.xticks([]),plt.yticks([])
plt.show()
def unquantize(qdct_patches):
# Initialize the quantization matrix
Qmtrx = np.zeros((8,8))
Qmtrx[0,:] = [16, 11, 10, 16, 24, 40, 51, 61]
Qmtrx[1,:] = [12, 12, 14, 19, 26, 58, 60, 55]
Qmtrx[2,:] = [14, 13, 16, 24, 40, 57, 69, 56]
Qmtrx[3,:] = [14, 17, 22, 29, 51, 87, 80, 62]
Qmtrx[4,:] = [18, 22, 37, 56, 68, 109, 103, 77]
Qmtrx[5,:] = [24, 36, 55, 64, 81, 104, 113, 92]
Qmtrx[6,:] = [49, 64, 78, 87, 103, 121, 120, 101]
Qmtrx[7,:] = [72, 92, 95, 98, 112, 100, 103, 99]
# Store dimensions of DCT coefficient matrix patches for iteration
# Initialize an empty matrix to store the quantized coefficients
# Compute and store the quantized coefficients
return iq_dct
# Computes iDCTs for patches
def inverse_dct(patches):
# Set empty array for recovered set of patches image
i_patches = np.zeros((prange,prange, 8, 8), dtype=np.float64)
return i_patches
def stitch(patches):
recovered = np.zeros((N,N), dtype=np.float64)
return recovered
recovered4 = stitch(inverse_dct(unquantize(qdct4)))
recovered16 = stitch(inverse_dct(unquantize(qdct16)))
recovered36 = stitch(inverse_dct(unquantize(qdct16)))
recovered64 = stitch(inverse_dct(unquantize(qdct64)))
plt.subplot(141),plt.imshow(recovered4,cmap='gray'), plt.title('K^2=4',fontsize=8), plt.xticks([]), plt.yticks([])
plt.subplot(142),plt.imshow(recovered16, cmap='gray'), plt.title('K^2 = 16',fontsize=8), plt.xticks([]),plt.yticks([])
plt.subplot(143),plt.imshow(recovered36,cmap='gray'), plt.title('K^2 = 36',fontsize=8), plt.xticks([]),plt.yticks([])
plt.subplot(144),plt.imshow(recovered64, cmap='gray'), plt.title('K^2 = 64',fontsize=8), plt.xticks([]),plt.yticks([])
plt.show()
# Computes reconstruction error
def compute_error(img, img_new):
error = compute_MSE(img,img_new)
return error
dcq_errors = [compute_error(im1, q) for q in [recovered4, recovered16, recovered36, recovered64]]
K2list = [4,16,36,64]
jpeg1 = cv2.imread("images/puppy3_small1.jpg", 0)
j1 = compute_error(im1, jpeg1)
j1 = [j1, j1, j1, j1]
jpeg2 = cv2.imread("images/puppy3_small2.jpg", 0)
j2 = compute_error(im1, jpeg2)
j2 = [j2, j2, j2, j2]
jpeg3 = cv2.imread("images/puppy3_small3.jpg", 0)
j3 = compute_error(im1, jpeg3)
j3 = [j3, j3, j3, j3]
jpeg4 = cv2.imread("images/puppy3_small4.jpg", 0)
j4 = compute_error(im1, jpeg4)
j4 = [j4, j4, j4, j4]
# Plot the MSE Reconstruction Errors against the Jpeg standard
plt.plot(K2list, dcq_errors, 'k-s', label='Quantized DCT')
plt.plot(K2list, j1, 'r--', label='JPEG Low Standard')
plt.plot(K2list, j2, 'y--', label='JPEG Medium Standard')
plt.plot(K2list, j3, 'g--', label='JPEG High Standard')
plt.plot(K2list, j4, 'b--', label='JPEG Max Standard')
plt.legend()
plt.xlabel('K^2')
plt.ylabel('Reconstruction Error')
plt.title('DFT and DCT Reconstruction Error for Varying K^2')
plt.savefig('JPEG_standard_plot.png')
plt.show()