import numpy as np
import cv2
import matplotlib.pyplot as plt
dct变换以及逆变换实现
#dct
def dct(img,N):
G = np.zeros((N,N),dtype=np.float64)
for i in range(N):
for j in range(N):
if(i == 0):
G[i][j] = np.sqrt(0.5)
else:
G[i][j] = np.cos((i+0.5)*j*np.pi/N)
G = G*(np.sqrt(2/N))
return G@[email protected]()
#dct逆变换
def idct(img,N):
G = np.zeros((N,N),dtype=np.float64)
for i in range(N):
for j in range(N):
if(i == 0):
G[i][j] = np.sqrt(0.5)
else:
G[i][j] = np.cos((i+0.5)*j*np.pi/N)
G = G*(np.sqrt(2/N))
return G.transpose()@img@G
先将lena图像做dct变换,然后取dct系数左上角四分之一,对其进行逆变换得到图像
img_lena = cv2.imread("images/lena.jpg",0)
N,N = img_lena.shape
img_lena = np.float64(img_lena)
lena_dct = dct(img_lena,N)
lena_dct1 = lena_dct[0:int(N/4),0:int(N/4)] #取左上角四分之一
lena_idct = idct(lena_dct1,lena_dct1.shape[0]) #dct逆变换
lena_dct = np.log(np.abs(lena_dct)+1)
plt.subplot(131)
plt.title("lena")
plt.imshow(img_lena,cmap='gray')
plt.subplot(132)
plt.title("lena_dct")
plt.imshow(lena_dct,cmap='gray')
plt.subplot(133)
plt.title("lena_idct")
plt.imshow(lena_idct,cmap='gray')
plt.show()
标签:lena,plt,img,idct,np,dct
From: https://www.cnblogs.com/ryuta/p/16749110.html