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
| import numpy as np import cv2
def cal_dct(X): N=X.shape[0] A=np.zeros((N,N)) for i in range(N): for j in range(N): if i == 0: a=np.sqrt(1/N) else: a=np.sqrt(2/N) A[i,j]=a*np.cos(np.pi*(j+0.5)*i/N) Y=A.dot(X).dot(A.T) return Y
def cal_idct(X): N=X.shape[0] A=np.zeros((N,N)) for i in range(N): for j in range(N): if i == 0: a=np.sqrt(1/N) else: a=np.sqrt(2/N) A[i,j]=a*np.cos(np.pi*(j+0.5)*i/N) Y=(A.T).dot(X).dot(A) return Y
def fast_DCT(X): P8=np.array([[1,0,0,0,0,0,0,0],[0,0,0,0,1,0,0,0],[0,0,1,0,0,0,0,0],[0,0,0,0,0,0,1,0],[0,1,0,0,0,0,0,0],[0,0,0,0,0,1,0,0],[0,0,0,1,0,0,0,0],[0,0,0,0,0,0,0,1]]) P4=np.array([[1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]])
M1=np.array([[np.sin(np.pi/16),0,0,np.cos(np.pi/16)],[0,np.sin(5*np.pi/16),np.cos(5*np.pi/16),0],[0,-np.sin(3*np.pi/16),np.cos(3*np.pi/16),0],[-np.sin(7*np.pi/16),0,0,np.cos(7*np.pi/16)]]) M2=np.sqrt(2)/2*np.array([[1,1,0,0],[1,-1,0,0],[0,0,-1,1],[0,0,1,1]]) M3=np.array([[1,0,0,0],[0,-np.cos(np.pi/4),np.cos(np.pi/4),0],[0,np.cos(np.pi/4),np.cos(np.pi/4),0],[0,0,0,1]])
R4=M1.dot(M2).dot(M3) B8=np.sqrt(2)/2*np.array([[1,0,0,0,0,0,0,1],[0,1,0,0,0,0,1,0],[0,0,1,0,0,1,0,0],[0,0,0,1,1,0,0,0],[0,0,0,1,-1,0,0,0],[0,0,1,0,0,-1,0,0],[0,1,0,0,0,0,-1,0],[1,0,0,0,0,0,0,-1]])
P2=np.array([[1,0],[0,1]]) C2=np.sqrt(2)/2*np.array([[1,1],[1,-1]]) R2=np.array([[np.sin(np.pi/8),np.cos(np.pi/8)],[-np.sin(3*np.pi/8),np.cos(3*np.pi/8)]]) B4=np.sqrt(2)/2*np.array([[1,0,0,1],[0,1,1,0],[0,1,-1,0],[1,0,0,-1]]) tmp1=np.concatenate((P2.T.dot(C2),np.zeros((2,2))),axis=1) tmp2=np.concatenate((np.zeros((2,2)),R2),axis=1) tmp=np.concatenate((tmp1,tmp2)) C4=P4.dot(tmp).dot(B4)
tmp1=np.concatenate((P4.T.dot(C4),np.zeros((4,4))),axis=1) tmp2=np.concatenate((np.zeros((4,4)),R4),axis=1) tmp=np.concatenate((tmp1,tmp2)) C8=P8.dot(tmp).dot(B8)
Y=C8.dot(X).dot(C8.T) return Y
def fast_IDCT(X): P8=np.array([[1,0,0,0,0,0,0,0],[0,0,0,0,1,0,0,0],[0,0,1,0,0,0,0,0],[0,0,0,0,0,0,1,0],[0,1,0,0,0,0,0,0],[0,0,0,0,0,1,0,0],[0,0,0,1,0,0,0,0],[0,0,0,0,0,0,0,1]]) P4=np.array([[1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]])
M1=np.array([[np.sin(np.pi/16),0,0,np.cos(np.pi/16)],[0,np.sin(5*np.pi/16),np.cos(5*np.pi/16),0],[0,-np.sin(3*np.pi/16),np.cos(3*np.pi/16),0],[-np.sin(7*np.pi/16),0,0,np.cos(7*np.pi/16)]]) M2=np.sqrt(2)/2*np.array([[1,1,0,0],[1,-1,0,0],[0,0,-1,1],[0,0,1,1]]) M3=np.array([[1,0,0,0],[0,-np.cos(np.pi/4),np.cos(np.pi/4),0],[0,np.cos(np.pi/4),np.cos(np.pi/4),0],[0,0,0,1]])
R4=M1.dot(M2).dot(M3) B8=np.sqrt(2)/2*np.array([[1,0,0,0,0,0,0,1],[0,1,0,0,0,0,1,0],[0,0,1,0,0,1,0,0],[0,0,0,1,1,0,0,0],[0,0,0,1,-1,0,0,0],[0,0,1,0,0,-1,0,0],[0,1,0,0,0,0,-1,0],[1,0,0,0,0,0,0,-1]])
P2=np.array([[1,0],[0,1]]) C2=np.sqrt(2)/2*np.array([[1,1],[1,-1]]) R2=np.array([[np.sin(np.pi/8),np.cos(np.pi/8)],[-np.sin(3*np.pi/8),np.cos(3*np.pi/8)]]) B4=np.sqrt(2)/2*np.array([[1,0,0,1],[0,1,1,0],[0,1,-1,0],[1,0,0,-1]]) tmp1=np.concatenate((P2.T.dot(C2),np.zeros((2,2))),axis=1) tmp2=np.concatenate((np.zeros((2,2)),R2),axis=1) tmp=np.concatenate((tmp1,tmp2)) C4=P4.dot(tmp).dot(B4)
tmp1=np.concatenate((P4.T.dot(C4),np.zeros((4,4))),axis=1) tmp2=np.concatenate((np.zeros((4,4)),R4),axis=1) tmp=np.concatenate((tmp1,tmp2)) C8=P8.dot(tmp).dot(B8)
Y=C8.T.dot(X).dot(C8) return Y
def main(): X=np.array([[42,66,68,66,42,66,68,66],[92,4,76,17,42,66,68,66],[79,85,74,71,42,66,68,66],[96,93,39,3,42,66,68,66],[42,66,68,66,42,66,68,66],[92,4,76,17,42,66,68,66],[79,85,74,71,42,66,68,66],[96,93,39,3,42,66,68,66]],dtype=np.float64) Y=cal_dct(X) print(Y) print('*************************') YY=cv2.dct(X) print(YY) print('************************') YYY=fast_DCT(X) print(YYY) X=cal_idct(Y) print(X) print('*************************') X=cv2.idct(YY) print(X) print('************************') X=fast_IDCT(YYY) print(X)
if __name__ =='__main__': main()
|