!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
from scipy import ndimage
import cv2
import copy
import warnings
from scipy import fftpack
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
2 packages can be upgraded. Run 'apt list --upgradable' to see them.
ffmpeg is already the newest version (7:4.1.6-1~deb10u1).
libsm6 is already the newest version (2:1.2.3-1).
libxext6 is already the newest version (2:1.3.3-1+b2).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
Requirement already satisfied: opencv-python in /root/venv/lib/python3.7/site-packages (4.5.2.52)
Requirement already satisfied: numpy>=1.14.5 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from opencv-python) (1.19.5)
#Define image dimensions
N = 50
M = 35
def compute_dct(X):
# Set empty array for dct coefficients
dct = np.zeros((M, N), dtype=np.float64)
# Iterate through and compute values for all coefficients
for k in range(M):
for l in range(N):
for m in range(N):
if k == 0:
c1 = 1/np.sqrt(2.0)
else:
c1 = 1
if 1 == 0:
c2 = 1/np.sqrt(2.0)
else:
c2 = 1
for n in range(M):
Cmn = (np.cos((k*np.pi/(2*M))*(2*m+1)))*(np.cos((l*np.pi/(2*N))*(2*n+1)))
dct[k,1] += X[m,n]*c1*c2*Cmn
# normalize the transform
dct = 2*dct/np.sqrt(N**2)
return dct
image = cv2.imread("europa.jpg", 0)
my_dct = compute_dct(image)
scipy_dct = fftpack.dct(fftpack.dct(image.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(image, cmap = 'gray')
plt.xlabel('', fontsize=8), plt.xticks([]), plt.yticks([])
plt.title('Original Image')
plt.subplot(132),plt.imshow(my_dct, cmap = 'gray')
plt.xlabel('', fontsize=8), plt.xticks([]), plt.yticks([])
plt.title('Results of DCT Function')
plt.subplot(133),plt.imshow(scipy_dct, cmap = 'gray')
plt.xlabel('', fontsize=8), plt.xticks([]), plt.yticks([])
plt.title('Scipys DCT Function')
plt.show()
# Redefine image dimensions for part 3.1
N = 800
patch_len = 10
# Number of patches
prange = N/patch_len
def partition_img(img):
# Set empty array
patches = np.zeros((int(prange), int(prange), 10, 10))
for i in range (prange):
for r in range (prange):
patches[i,r] = img[(i*patch_len):((i*patch_len)+patch_len),(r*patch_len):((r*patch_len)+patch_len)]
return patches
def partitioned_dct(ppatches, K):
part_patches = np.zeros((int(prange), int(prange), 10, 10))
flat_list = np.ravel(compute_dct(image))
np.ndarray.min
return dct_patches
patches = partition_img(image)
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()