返回目录
1. The usage of DCT functions
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('monarch.png', cv2.IMREAD_GRAYSCALE)
height, width = img.shape
img_dct = cv2.dct(np.array(img, np.float32))
img_dct[100:200, 100:200] = 0
img_r = np.array(cv2.idct(img_dct), np.uint8)
fig = plt.figure('DCT demo', figsize=(4,2))
plt.subplot(131)
plt.imshow(img, 'gray'), plt.title('Original'), plt.axis('off')
plt.subplot(132)
plt.imshow(np.array(img_dct, np.uint8), cmap='hot'), plt.title('DCT mod'), plt.axis('off')
plt.subplot(133)
plt.imshow(img_r, 'gray'), plt.title('Original mod'), plt.axis('off')
plt.tight_layout()
plt.show()
2. 不同局部复杂度的DCT
import cv2
import numpy as np
import matplotlib.pyplot as plt
img_gray = cv2.imread('monarch.png', cv2.IMREAD_GRAYSCALE)
height, width = img_gray.shape
w_region, h_region = 128, 128
fig = plt.figure('DCT comparison', figsize=(6,4))
x0, y0 = width-w_region, height-h_region
region_flat = img_gray[y0:(y0+h_region), x0:(x0+w_region)]
region_flat_dct = cv2.dct(np.array(region_flat, np.float32))
plt.subplot(231)
plt.imshow(img_gray, cmap='gray'), plt.title('Flat region'), plt.axis('off')
plt.gca().add_patch(plt.Rectangle((x0, y0), w_region, h_region, edgecolor='r', fill=False, linewidth=1))
plt.subplot(234)
plt.imshow(np.array(region_flat_dct, np.uint8), cmap='hot'), plt.title('Flat region: DCT'), plt.axis('off')
x0, y0 = np.int(width/2-w_region/2), 0
region_2 = img_gray[y0:(y0+h_region), x0:(x0+w_region)]
region_2_dct = cv2.dct(np.array(region_2, np.float32))
plt.subplot(232)
plt.imshow(img_gray, cmap='gray'), plt.title('Region 2'), plt.axis('off')
plt.gca().add_patch(plt.Rectangle((x0, y0), w_region, h_region, edgecolor='r', fill=False, linewidth=1))
plt.subplot(235)
plt.imshow(np.array(region_2_dct, np.uint8), cmap='hot'), plt.title('Region 2: DCT'), plt.axis('off')
x0, y0 = np.int(width/2-w_region/2*3), np.int(height/4)
region_complex = img_gray[y0:(y0+h_region), x0:(x0+w_region)]
region_complex_dct = cv2.dct(np.array(region_complex, np.float32))
plt.subplot(233)
plt.imshow(img_gray, cmap='gray'), plt.title('Complex region'), plt.axis('off')
plt.gca().add_patch(plt.Rectangle((x0, y0), w_region, h_region, edgecolor='r', fill=False, linewidth=1))
plt.subplot(236)
plt.imshow(np.array(region_complex_dct, np.uint8), cmap='hot'), plt.title('Complex region: DCT'), plt.axis('off')
plt.tight_layout()
plt.show()
3. 图像分块DCT
import cv2
import numpy as np
import matplotlib.pyplot as plt
img_gray = cv2.imread('monarch.png', cv2.IMREAD_GRAYSCALE)
height, width = img_gray.shape
dct_coeff = np.zeros((height, width), dtype=np.float32)
print(dct_coeff.shape)
N = 8 # 块大小
for row in np.arange(0, height-N, N):
for col in np.arange(0, width-N, N):
print(row, col)
block = np.array(img_gray[row:(row+N), col:(col+N)], dtype=np.float32)
dct_coeff[row:(row+N), col:(col+N)] = cv2.dct(block)
dct_coeff = np.abs(dct_coeff)
dct_coeff = np.array(255*(dct_coeff/np.max(dct_coeff)), np.uint8)
plt.figure()
plt.subplot(121), plt.imshow(img_gray, 'gray'), plt.title('Mornach'), plt.axis('off')
plt.subplot(122), plt.imshow(dct_coeff, 'gray'), plt.title('Mornach DCT'), plt.axis('off')
plt.tight_layout()
plt.show()
4. JPEG压缩
参见:JPEG图片压缩的Python实现
返回目录