1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
| import cv2 import numpy as np import math
def rgb_to_yuv(image): img = np.array(image, dtype=np.float32) R = img[:, :, 0] G = img[:, :, 1] B = img[:, :, 2] Y = np.round(0.299 * R + 0.587 * G + 0.114 * B) U = np.round(0.5 * R - 0.4187 * G - 0.0813 * B + 128) V = np.round(-0.1687 * R - 0.3313 * G + 0.5 * B + 128) Y = np.clip(Y, 0, 255).astype(np.uint8) U = np.clip(U, 0, 255).astype(np.uint8) V = np.clip(V, 0, 255).astype(np.uint8) return Y, U, V
def yuv_to_rgb(Y, U, V): Y = Y.astype(np.float32) U = U.astype(np.float32) - 128 V = V.astype(np.float32) - 128 R = Y + 1.402 * V G = Y - 0.344136 * U - 0.714136 * V B = Y + 1.772 * U R = np.clip(R, 0, 255).astype(np.uint8) G = np.clip(G, 0, 255).astype(np.uint8) B = np.clip(B, 0, 255).astype(np.uint8) return np.stack([R, G, B], axis=2)
def downsample(Y, U, V): U_ds = U[::2, ::2] V_ds = V[::2, ::2] return Y, U_ds, V_ds
def upsample(U_ds, V_ds): U = U_ds.repeat(2, axis=0).repeat(2, axis=1) V = V_ds.repeat(2, axis=0).repeat(2, axis=1) return U, V
def alpha(u): return 1 / math.sqrt(2) if u == 0 else 1
def DCT_block(block): block = block.astype(np.float32) - 128 dct = np.zeros((8, 8), dtype=np.float32) for u in range(8): for v in range(8): sum_val = 0.0 for x in range(8): for y in range(8): sum_val += block[x, y] * math.cos((2*x + 1) * u * math.pi / 16) * math.cos((2*y + 1) * v * math.pi / 16) dct[u, v] = 0.25 * alpha(u) * alpha(v) * sum_val return np.round(dct)
def apply_dct(image): h, w = image.shape h_padded = h if h % 8 == 0 else h + (8 - h % 8) w_padded = w if w % 8 == 0 else w + (8 - w % 8) padded = np.zeros((h_padded, w_padded), dtype=np.float32) padded[:h, :w] = image blocks = [] for i in range(0, h_padded, 8): for j in range(0, w_padded, 8): block = padded[i:i+8, j:j+8] dct_block = DCT_block(block) blocks.append(dct_block) return blocks
def quantize(blocks, Q): quantized = [] for block in blocks: quant = np.round(block / Q).astype(np.int32) quantized.append(quant) return quantized
def zigzag_order(block): zigzag_indices = [ (0,0),(0,1),(1,0),(2,0),(1,1),(0,2),(0,3),(1,2), (2,1),(3,0),(4,0),(3,1),(2,2),(1,3),(0,4),(0,5), (1,4),(2,3),(3,2),(4,1),(5,0),(6,0),(5,1),(4,2), (3,3),(2,4),(1,5),(0,6),(0,7),(1,6),(2,5),(3,4), (4,3),(5,2),(6,1),(7,0),(7,1),(6,2),(5,3),(4,4), (3,5),(2,6),(1,7),(2,7),(3,6),(4,5),(5,4),(6,3), (7,2),(7,3),(6,4),(5,5),(4,6),(3,7),(4,7),(5,6), (6,5),(7,4),(7,5),(6,6),(5,7),(6,7),(7,6),(7,7) ] return [block[i][j] for i, j in zigzag_indices]
def zigzag(blocks): zigzagged = [] for block in blocks: z = zigzag_order(block) zigzagged.append(z) return zigzagged
def DPCM_dc(zigzagged): dpcm = [] previous = 0 for block in zigzagged: current = block[0] diff = current - previous dpcm.append(diff) previous = current return dpcm
def RLE_ac(zigzagged): rle = [] for block in zigzagged: ac = [] zero_count = 0 for coef in block[1:]: if coef == 0: zero_count += 1 else: while zero_count > 15: ac.append((15, 0)) zero_count -= 16 ac.append((zero_count, coef)) zero_count = 0 if zero_count > 0: ac.append((0, 0)) rle.append(ac) return rle
def jpeg_compress(image_path): image = cv2.imread(image_path) Y, U, V = rgb_to_yuv(image) Y_ds, U_ds, V_ds = downsample(Y, U, V) dct_blocks = apply_dct(Y_ds) QY = np.array([ [16,11,10,16,24,40,51,61], [12,12,14,19,26,58,60,55], [14,13,16,24,40,57,69,56], [14,17,22,29,51,87,80,62], [18,22,37,56,68,109,103,77], [24,35,55,64,81,104,113,92], [49,64,78,87,103,121,120,101], [72,92,95,98,112,100,103,99] ]) quantized_blocks = quantize(dct_blocks, QY) zigzagged_blocks = zigzag(quantized_blocks) dpcm_dc = DPCM_dc(zigzagged_blocks) rle_ac = RLE_ac(zigzagged_blocks) return dpcm_dc, rle_ac
def jpeg_decompress(dpcm_dc, rle_ac, image_shape): """简化的JPEG解压缩过程""" dc = [] previous = 0 for diff in dpcm_dc: current = previous + diff dc.append(current) previous = current zigzagged = [] for i in range(len(dc)): block = [dc[i]] ac = rle_ac[i] for run, coef in ac: if run == 0 and coef == 0: block.extend([0] * (63 - len(block) + 1)) break block.extend([0] * run) block.append(coef) if len(block) < 64: block.extend([0] * (64 - len(block))) zigzagged.append(block) def inverse_zigzag(z): block = np.zeros((8, 8), dtype=np.float32) zigzag_indices = [ (0,0),(0,1),(1,0),(2,0),(1,1),(0,2),(0,3),(1,2), (2,1),(3,0),(4,0),(3,1),(2,2),(1,3),(0,4),(0,5), (1,4),(2,3),(3,2),(4,1),(5,0),(6,0),(5,1),(4,2), (3,3),(2,4),(1,5),(0,6),(0,7),(1,6),(2,5),(3,4), (4,3),(5,2),(6,1),(7,0),(7,1),(6,2),(5,3),(4,4), (3,5),(2,6),(1,7),(2,7),(3,6),(4,5),(5,4),(6,3), (7,2),(7,3),(6,4),(5,5),(4,6),(3,7),(4,7),(5,6), (6,5),(7,4),(7,5),(6,6),(5,7),(6,7),(7,6),(7,7) ] for idx, (i, j) in enumerate(zigzag_indices): block[i, j] = z[idx] return block
quantized_blocks = [inverse_zigzag(z) for z in zigzagged] QY = np.array([ [16,11,10,16,24,40,51,61], [12,12,14,19,26,58,60,55], [14,13,16,24,40,57,69,56], [14,17,22,29,51,87,80,62], [18,22,37,56,68,109,103,77], [24,35,55,64,81,104,113,92], [49,64,78,87,103,121,120,101], [72,92,95,98,112,100,103,99] ]) dequantized_blocks = [] for block in quantized_blocks: dequant = block * QY dequantized_blocks.append(dequant) def IDCT_block(block): block += 128 idct = np.zeros((8, 8), dtype=np.float32) for x in range(8): for y in range(8): sum_val = 0.0 for u in range(8): for v in range(8): sum_val += alpha(u) * alpha(v) * block[u, v] * math.cos((2*x + 1) * u * math.pi / 16) * math.cos((2*y + 1) * v * math.pi / 16) idct[x, y] = 0.25 * sum_val return np.clip(np.round(idct), 0, 255).astype(np.uint8)
reconstructed_blocks = [IDCT_block(block) for block in dequantized_blocks] def merge_blocks(blocks, image_shape): h, w = image_shape h_padded = h if h % 8 == 0 else h + (8 - h % 8) w_padded = w if w % 8 == 0 else w + (8 - w % 8) image = np.zeros((h_padded, w_padded), dtype=np.uint8) idx = 0 for i in range(0, h_padded, 8): for j in range(0, w_padded, 8): image[i:i+8, j:j+8] = reconstructed_blocks[idx] idx += 1 return image[:h, :w]
reconstructed_Y = merge_blocks(reconstructed_blocks, image_shape=(Y.shape)) U_reconstructed, V_reconstructed = upsample(U_ds, V_ds) reconstructed_image = yuv_to_rgb(reconstructed_Y, U_reconstructed, V_reconstructed) return reconstructed_image
def jpeg_compress_decompress(image_path, output_path): dpcm_dc, rle_ac = jpeg_compress(image_path) image = cv2.imread(image_path) Y, U, V = rgb_to_yuv(image) reconstructed_YUV = jpeg_decompress(dpcm_dc, rle_ac, Y.shape) cv2.imwrite(output_path, reconstructed_YUV) print(f"重建图像已保存至 {output_path}")
if __name__ == "__main__": input_image = 'data/dog.jpg' output_image = 'output.jpg' jpeg_compress_decompress(input_image, output_image)
|