-
待处理图像:
Lena.jpg
-
开始分析
import numpy as np import matplotlib.pyplot as plt import cv2 import copy ####################### ### 1. 读取图片 ### ####################### # image = cv2.imread('Lena.jpg', 0) image = cv2.imread('Lena.jpg') (img_B, img_G, img_R) = cv2.split(image) print(img_R.shape) ########################################### ### 2. 离散余弦变换,并获取其幅频谱 ### ########################################### # 1. DCT img_B_dct = cv2.dct(np.float32(img_B)) img_G_dct = cv2.dct(np.float32(img_G)) img_R_dct = cv2.dct(np.float32(img_R)) # 2. Frequency Component img_B_dct_log = 20 * np.log(abs(img_B_dct)) img_G_dct_log = 20 * np.log(abs(img_G_dct)) img_R_dct_log = 20 * np.log(abs(img_R_dct)) # 3. iDCT img_B_back = cv2.idct(img_B_dct) img_G_back = cv2.idct(img_G_dct) img_R_back = cv2.idct(img_R_dct) ###################### ### 3. 可视化 ### ###################### plt.subplot(3, 3, 1) plt.imshow(img_B, cmap='gray') plt.title('B channel of Origin Img', fontproperties='Times New Roman') plt.axis('off') plt.subplot(3, 3, 2) plt.imshow(img_B_dct_log, cmap='gray') plt.title('DCT of B channel', fontproperties='Times New Roman') plt.axis('off') plt.subplot(3, 3, 3) plt.imshow(img_B_back, cmap='gray') plt.title('iDCT of B channel', fontproperties='Times New Roman') plt.axis('off') plt.subplot(3, 3, 4) plt.imshow(img_G, cmap='gray') plt.title('G channel of Origin Img', fontproperties='Times New Roman') plt.axis('off') plt.subplot(3, 3, 5) plt.imshow(img_G_dct_log, cmap='gray') plt.title('DCT of G channel', fontproperties='Times New Roman') plt.axis('off') plt.subplot(3, 3, 6) plt.imshow(img_G_back, cmap='gray') plt.title('iDCT of G channel', fontproperties='Times New Roman') plt.axis('off') plt.subplot(3, 3, 7) plt.imshow(img_R, cmap='gray') plt.title('R channel of Origin Img', fontproperties='Times New Roman') plt.axis('off') plt.subplot(3, 3, 8) plt.imshow(img_R_dct_log, cmap='gray') plt.title('DCT of R channel', fontproperties='Times New Roman') plt.axis('off') plt.subplot(3, 3, 9) plt.imshow(img_R_back, cmap='gray') plt.title('iDCT of R channel', fontproperties='Times New Roman') plt.axis('off') plt.savefig('1.png', dpi=300) # plt.show() plt.close() ############################### ### 4. 分析不同频率成分 ### ############################### ### 1. 准备 image_B_dct = copy.deepcopy(img_B_dct) image_G_dct = copy.deepcopy(img_G_dct) image_R_dct = copy.deepcopy(img_R_dct) image_B_dct_log = 20*np.log(abs(image_B_dct)) image_G_dct_log = 20*np.log(abs(image_G_dct)) image_R_dct_log = 20*np.log(abs(image_R_dct)) ### 2. 选择频率 LOW = 25 HIGH = 100 # (1) B for i in range(image_B_dct.shape[0]): for j in range(image_B_dct.shape[1]): if i < LOW and j < LOW: image_B_dct[i, j] = 0 image_B_dct_log[i, j] = 0 if i >= HIGH or j >= HIGH: image_B_dct[i, j] = 0 image_B_dct_log[i, j] = 0 # (2) G for i in range(image_G_dct.shape[0]): for j in range(image_G_dct.shape[1]): if i < LOW and j < LOW: image_G_dct[i, j] = 0 image_G_dct_log[i, j] = 0 if i >= HIGH or j >= HIGH: image_G_dct[i, j] = 0 image_G_dct_log[i, j] = 0 # (3) R for i in range(image_R_dct.shape[0]): for j in range(image_R_dct.shape[1]): if i < LOW and j < LOW: image_R_dct[i, j] = 0 image_R_dct_log[i, j] = 0 if i >= HIGH or j >= HIGH: image_R_dct[i, j] = 0 image_R_dct_log[i, j] = 0 ### 3. 逆DCT img_B_back = cv2.idct(image_B_dct) img_G_back = cv2.idct(image_G_dct) img_R_back = cv2.idct(image_R_dct) plt.subplot(2, 3, 1) plt.imshow(image_B_dct_log, cmap='gray') plt.title('compression B', fontproperties='Times New Roman') plt.axis('off') plt.subplot(2, 3, 4) plt.imshow(img_B_back, cmap='gray') plt.title('B back', fontproperties='Times New Roman') plt.axis('off') plt.subplot(2, 3, 2) plt.imshow(image_G_dct_log, cmap='gray') plt.title('compression G', fontproperties='Times New Roman') plt.axis('off') plt.subplot(2, 3, 5) plt.imshow(img_G_back, cmap='gray') plt.title('G Back', fontproperties='Times New Roman') plt.axis('off') plt.subplot(2, 3, 3) plt.imshow(image_R_dct_log, cmap='gray') plt.title('compression R', fontproperties='Times New Roman') plt.axis('off') plt.subplot(2, 3, 6) plt.imshow(img_R_back, cmap='gray') plt.title('R back', fontproperties='Times New Roman') plt.axis('off') plt.savefig('2.png', dpi=300) # plt.show() plt.close() ### 4. 合并通道 img_back = cv2.merge([img_B_back, img_G_back, img_R_back]) img_back = cv2.cvtColor(img_back, cv2.COLOR_BGR2RGB) img_back = (img_back - img_back.min()) / (img_back.max()-img_back.min()) *255 img_back = img_back.astype('uint8') plt.subplot(1,1,1) plt.imshow(img_back) plt.title('Combine back', fontproperties='Times New Roman') plt.axis('off') plt.savefig('3.png', dpi=300) # plt.show() plt.close()
-
结果展示
- 结果图:
1.png
- 结果图:
2.png
- 结果图:
3.png
- 结果图:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)