对图像处理经常用到DCT, Python下有很多带有DCT算法包, 这里使用OpenCV的DCT做变换, 并简单置0部分数据, 再查看反变换图像的效果.
import numpy as np
import cv2
# from matplotlib import pyplot as plt
y = cv2.imread('window.bmp', 0)
# print(y.shape)
cv2.imshow("gray",y)
y1 = y.astype(np.float32)
# print(y1.dtype)
Y = cv2.dct(y1)
print(Y.shape)
for i in range(0,240):
for j in range(0,320):
if i > 100 or j > 100:
Y[i,j] = 0
cv2.imshow("Dct",Y)
y2 = cv2.idct(Y)
# print(y2.dtype)
cv2.imshow("iDCT",y2.astype(np.uint8))
cv2.waitKey(0)
对于320x240的图像, 在频域对大于100的行和列都置零(相当于低通滤波)后, 反变换后图像有点模糊, 但还能还原大致效果.