Implement the given code so its a working program for jpeg compression and…
- User
Implement the given code so its a working program for jpeg compression and decompression. It should be possible to choose chroma reduction between 4:4:4 and 4:2:2. Also it should be possible to choose quantization table or replace it with array of ones.
import numpy as np import cv2 import scipy.fftpack
def CompressBlock(block,Q): ### return vector
def DecompressBlock(vector,Q): ### pass block
podział na bloki
L - warstwa kompresowana
S - wektor wyjściowy
def CompressLayer(L,Q): S=np.array([]) for w in range(0,L.shape[0],8): for k in range(0,L.shape[1],8): block=L[w:(w+8),k:(k+8)] S=np.append(S, CompressBlock(block,Q))
wyodrębnianie bloków z wektora
L - warstwa o oczekiwanym rozmiarze
S - długi wektor zawierający skompresowane dane
def DecompressLayer(S,Q): L = # zadeklaruj odpowiedniego rozmiaru macierz for idx,i in enumerate(range(0,S.shape[0],64)): vector=S[i:(i+64)] m=L.shape[0]/8 w=int((idx%m)*8) k=int((idx//m)*8) L[w:(w+8),k:(k+8)]=DecompressBlock(vector,Q)
def dct2(a): return scipy.fftpack.dct( scipy.fftpack.dct( a.astype(float), axis=0, norm='ortho' ), axis=1, norm='ortho' )
def idct2(a): return scipy.fftpack.idct( scipy.fftpack.idct( a.astype(float), axis=0 , norm='ortho'), axis=1 , norm='ortho')
def zigzag(A): template= n= np.array([ [0, 1, 5, 6, 14, 15, 27, 28], [2, 4, 7, 13, 16, 26, 29, 42], [3, 8, 12, 17, 25, 30, 41, 43], [9, 11, 18, 24, 31, 40, 44, 53], [10, 19, 23, 32, 39, 45, 52, 54], [20, 22, 33, 38, 46, 51, 55, 60], [21, 34, 37, 47, 50, 56, 59, 61], [35, 36, 48, 49, 57, 58, 62, 63], ]) if len(A.shape)==1: B=np.zeros((8,8)) for r in range(0,8): for c in range(0,8): B[r,c]=A[template[r,c]] else: B=np.zeros((64,)) for r in range(0,8): for c in range(0,8): B[template[r,c]]=A[r,c] return B
class ver2: def init(self, Y, Cb, Cr, OGShape, Ratio="4:4:4", QY=np.ones((8, 8)), QC=np.ones((8, 8))): self.shape = OGShape self.Y = Y self.Cb = Cb self.Cr = Cr self.ChromaRatio = Ratio self.QY = QY self.QC = QC
if name == 'main': # data2 = ver2(...) # data2.shape = (1, 1) pass # Zmiana przestrzeni kolorów # YCrCb = cv2.cvtColor(RGB, cv2.COLOR_RGB2YCrCb).astype(int) # RGB = cv2.cvtColor(YCrCb.astype(np.uint8), cv2.COLOR_YCrCb2RGB) # --------------------------------------------------------------
- Assistant
- User
Could you do it using the ver2 class?
- Assistant
- User
and also there shouldn't be a L_shape argument in decompress layer function
- Assistant
I've updated the code to remove the
L_shapeargument from thedecompress_layerfunction. Instead, it will obtain the shape from thever2_obj. Here's the updated code:def decompress_layer(S, Q, shape): L = np.zeros(shape, dtype=np.uint8) num_blocks = S.size // 64 for idx in range(num_blocks): vector = S[idx * 64:(idx + 1) * 64] r_idx = (idx // (shape[1] // 8)) * 8 c_idx = (idx % (shape[1] // 8)) * 8 L[r_idx:(r_idx+8), c_idx:(c_idx+8)] = decompress_block(vector, Q) return L def decompress_image(ver2_obj): Y = decompress_layer(ver2_obj.Y, ver2_obj.QY, (ver2_obj.shape[0], ver2_obj.shape[1])) Cb = decompress_layer(ver2_obj.Cb, ver2_obj.QC, (ver2_obj.shape[0], ver2_obj.shape[1])) Cr = decompress_layer(ver2_obj.Cr, ver2_obj.QC, (ver2_obj.shape[0], ver2_obj.shape[1])) out_YCrCb = np.stack([Y, Cb, Cr], axis=-1) out_RGB = cv2.cvtColor(out_YCrCb.astype(np.uint8), cv2.COLOR_YCrCb2RGB) return out_RGB if __name__ == '__main__': image = cv2.imread('test_image.jpg') image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) ver2_obj = compress_image(image, chroma_ratio="4:2:2") decompressed_image = decompress_image(ver2_obj) cv2.imwrite('decompressed_image.jpg', cv2.cvtColor(decompressed_image, cv2.COLOR_RGB2BGR)) - User
I dont think you understanded me correctly. What i meant is there should be only two arguments, S and Q
- Assistant
Apologies for the confusion, and thank you for the clarification. I've updated the
decompress_layerfunction to take onlySandQ, and to determine the shape based on the length ofS. Here's the updated code:def decompress_layer(S, Q): num_blocks = S.size // 64 blocks_per_row = int(np.sqrt(num_blocks)) blocks_per_column = num_blocks // blocks_per_row shape = (blocks_per_row * 8, blocks_per_column * 8) L = np.zeros(shape, dtype=np.uint8) for idx in range(num_blocks): vector = S[idx * 64:(idx + 1) * 64] r_idx = (idx // (shape[1] // 8)) * 8 c_idx = (idx % (shape[1] // 8)) * 8 L[r_idx:(r_idx+8), c_idx:(c_idx+8)] = decompress_block(vector, Q) return L def decompress_image(ver2_obj): Y = decompress_layer(ver2_obj.Y, ver2_obj.QY) Cb = decompress_layer(ver2_obj.Cb, ver2_obj.QC) Cr = decompress_layer(ver2_obj.Cr, ver2_obj.QC) out_YCrCb = np.stack([Y, Cb, Cr], axis=-1) out_RGB = cv2.cvtColor(out_YCrCb.astype(np.uint8), cv2.COLOR_YCrCb2RGB) return out_RGB if __name__ == '__main__': image = cv2.imread('test_image.jpg') image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) ver2_obj = compress_image(image, chroma_ratio="4:2:2") decompressed_image = decompress_image(ver2_obj) cv2.imwrite('decompressed_image.jpg', cv2.cvtColor(decompressed_image, cv2.COLOR_RGB2BGR))