Repository navigation
Expand file tree
/
Copy pathJPEGEncoding.py
More file actions
175 lines (159 loc) · 6.1 KB
/
Copy pathJPEGEncoding.py
File metadata and controls
175 lines (159 loc) · 6.1 KB
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
#https://www.hdm-stuttgart.de/~maucher/Python/MMCodecs/html/jpegUpToQuant.html
import cv2
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
from Rle import *
B=8 # blocksize (In Jpeg the
cv2.CV_LOAD_IMAGE_UNCHANGED=1
img1 = cv2.imread("images/lena.bmp", cv2.CV_LOAD_IMAGE_UNCHANGED)
h,w=np.array(img1.shape[:2])/B * B
img1=img1[:h,:w]
#Convert BGR to RGB
img2=np.zeros(img1.shape,np.uint8)
img2[:,:,0]=img1[:,:,2]
img2[:,:,1]=img1[:,:,1]
img2[:,:,2]=img1[:,:,0]
plt.imshow(img2)
plt.figure()
#plt.show()
# point=plt.ginput(1)
# block=np.floor(np.array(point)/B) #first component is col, second component is row
# print "Coordinates of selected block: ",block
# scol=block[0,0]
# srow=block[0,1]
# plt.plot([B*scol,B*scol+B,B*scol+B,B*scol,B*scol],[B*srow,B*srow,B*srow+B,B*srow+B,B*srow])
# plt.axis([0,w,h,0])
transcol=cv2.cvtColor(img1, cv2.COLOR_BGR2YCR_CB)
SSV=2
SSH=2
crf=cv2.boxFilter(transcol[:,:,1],ddepth=-1,ksize=(1,1))
cbf=cv2.boxFilter(transcol[:,:,2],ddepth=-1,ksize=(1,1))
crsub=crf[::SSV,::SSH]
cbsub=cbf[::SSV,::SSH]
imSub=[transcol[:,:,0],crsub,cbsub]
#imSub= [transcol[:,:,0]]
transcol[:,:,1] = cv2.resize(crsub,(h,w))
transcol[:,:,2] = cv2.resize(cbsub,(h,w))
plt.imshow(cv2.cvtColor(transcol, cv2.COLOR_YCR_CB2BGR)[...,::-1])
plt.figure()
# SSV=2
# SSH=2
# crf=cv2.boxFilter(img2[:,:,1],ddepth=-1,ksize=(1,1))
# cbf=cv2.boxFilter(img2[:,:,2],ddepth=-1,ksize=(1,1))
# crsub=crf[::SSV,::SSH]
# cbsub=cbf[::SSV,::SSH]
# imSub=[transcol[:,:,0],crsub,cbsub]
# #imSub= [transcol[:,:,0]]
# img2[:,:,1] = cv2.resize(crsub,(h,w))
# img2[:,:,2] = cv2.resize(cbsub,(h,w))
# plt.imshow(img2)
# plt.show()
QY=np.array([[16,11,10,16,24,40,51,61],
[12,12,14,19,26,48,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]])
QC=np.array([[17,18,24,47,99,99,99,99],
[18,21,26,66,99,99,99,99],
[24,26,56,99,99,99,99,99],
[47,66,99,99,99,99,99,99],
[99,99,99,99,99,99,99,99],
[99,99,99,99,99,99,99,99],
[99,99,99,99,99,99,99,99],
[99,99,99,99,99,99,99,99]])
TransAll=[]
TransAllQuant=[]
RLE_Trans_Quant = []
ch=['Y','Cr','Cb']
#plt.figure()
QF=50.0
if QF < 50 and QF > 1:
scale = np.floor(5000/QF)
elif QF < 100:
scale = 200-2*QF
else:
print "Quality Factor must be in the range [1..99]"
scale=scale/100.0
Q=[QY*scale,QC*scale,QC*scale]
print scale
print Q[0]
print Q[1]
compressed = {}
for idx,channel in enumerate(imSub):
#plt.subplot(1,3,idx+1)
zigZag_Matrice_all = []
DC_Values = []
huffman_symbole_codes = []
vector_matrices = []
encoded_DC_Values = ''
channelrows=channel.shape[0]
channelcols=channel.shape[1]
Trans = np.zeros((channelrows,channelcols), np.float32)
TransQuant = np.zeros((channelrows,channelcols), np.float32)
blocksV=channelrows/B
blocksH=channelcols/B
vis0 = np.zeros((channelrows,channelcols), np.float32)
vis0[:channelrows, :channelcols] = channel
vis0=vis0-128
for row in range(blocksV):
for col in range(blocksH):
currentblock = cv2.dct(vis0[row*B:(row+1)*B,col*B:(col+1)*B])
Trans[row*B:(row+1)*B,col*B:(col+1)*B]=currentblock
TransQuant[row*B:(row+1)*B,col*B:(col+1)*B]=np.round(currentblock/Q[idx])
zigZag_Matrice = getZigZag(TransQuant[row*B:(row+1)*B,col*B:(col+1)*B])
#DC_Values.append(zigZag_Matrice[0])
vector_matrices = vector_matrices + zigZag_Matrice#[1:]
zigZag_Matrice_all.append(zigZag_Matrice)#[1:]
huffman_symbole_codes = dict(huffman(vector_matrices))
#encoded_DC_Values = DPCM(DC_Values)
encoded_image = rle(zigZag_Matrice_all, huffman_symbole_codes)
TransAll.append(Trans)
TransAllQuant.append(TransQuant)
compressed[idx] = compress(encoded_DC_Values, encoded_image, huffman_symbole_codes)
compressed_size = 0
for idx, compressed_channel in compressed.items():
compressed_size += len(compressed_channel)
print h * w * 8 * 3
print compressed_size
print "Compression ratio = %f"%(1-(float(compressed_size)/float(h * w * 8 * 3)))
# if idx==0:
# selectedTrans=Trans[int(srow*B):(int(srow+1)*B),int(scol*B):int((scol+1)*B)]
# else:
# sr=np.floor(srow/SSV)
# sc=np.floor(scol/SSV)
# selectedTrans=Trans[sr*B:(sr+1)*B,sc*B:(sc+1)*B].3
#plt.imshow(selectedTrans,cmap=cm.jet,interpolation='nearest')
#plt.colorbar(shrink=0.5)
#plt.title('DCT of '+ch[idx])
#plt.show()
DecAll=np.zeros((h,w,3), np.uint8)
for idx,channel in enumerate(TransAllQuant):
channelrows=channel.shape[0]
channelcols=channel.shape[1]
blocksV=channelrows/B
blocksH=channelcols/B
back0 = np.zeros((channelrows,channelcols), np.uint8)
for row in range(blocksV):
for col in range(blocksH):
dequantblock=channel[row*B:(row+1)*B,col*B:(col+1)*B]*Q[idx]
currentblock = np.round(cv2.idct(dequantblock))+128
currentblock[currentblock>255]=255
currentblock[currentblock<0]=0
back0[row*B:(row+1)*B,col*B:(col+1)*B]=currentblock
back1=cv2.resize(back0,(h,w))
DecAll[:,:,idx]=np.round(back1)
reImg=cv2.cvtColor(DecAll, cv2.COLOR_YCrCb2BGR)
cv2.imwrite('BackTransformedQuant.jpg', reImg)
#plt.figure()
img3=np.zeros(img1.shape,np.uint8)
img3[:,:,0]=reImg[:,:,2]
img3[:,:,1]=reImg[:,:,1]
img3[:,:,2]=reImg[:,:,0]
plt.imshow(img3)
SSE=np.sqrt(np.sum((img2-img3)**2))
print "Sum of squared error: ",SSE
#plt.show()