-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompression.py
64 lines (58 loc) · 2.76 KB
/
compression.py
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
'''
Description: Compression class
-> Obtains 8x8 blocks of the raw video and obtains dct coefficients
-> Can do the reverse as well
-> videoData class used to inherit block accessor
-> segmentation class used to obtain block's type
#----------------------------------------------------------------------------------------------------------------#
Class functions:
#----------------------------------------------------------------------------------------------------------------#
Notes:
'''
import time
import numpy as np
import cv2
import time
from videoData import videoData
class compression():
#------------------------------ Constructor ------------------------------#
def __init__(self, vidData):
self.vidData = vidData
self.__blockSize = 8
def computeDCT(self, block3D, block_size, blockType):
channelDCT = np.zeros(block_size*block_size*3 + 1)
# block_dimention_updated = np.einsum('jki->ijk',block3D)
channelDCT[0] = blockType
for channel in range (self.vidData.getNumChannels()):
block = block3D[channel, :, :]
block_f = np.float32(block) # float conversion/scale
dct_coeffs = cv2.dct(block_f) # the dct
channelDCT[(block_size*block_size*channel)+1:(block_size*block_size*(channel+1))+1] = dct_coeffs.reshape((1, block_size*block_size))
return channelDCT
def saveCMP(self):
# put DCT into a file
# st = time.time()
iIndices = list(range(0, self.vidData.getHeight(), self.__blockSize))
jIndices = list(range(0, self.vidData.getWidth(), self.__blockSize))
no_of_blocks = len(iIndices)*len(jIndices)
# print no_of_blocks
framesPerCMP = self.vidData.totalFrames
blockDCT = np.zeros((framesPerCMP*no_of_blocks,(8*8*3 )+ 1))
iIndices[-1] = self.vidData.getHeight() - self.__blockSize
jIndices[-1] = self.vidData.getWidth() - self.__blockSize
# self.vidData.totalFrames
cntr = 0
cntr_frame = 0
for frame in range(self.vidData.totalFrames):
for i in iIndices:
for j in jIndices:
blockType = self.vidData.getLabel(cntr_frame, i/self.__blockSize, j/self.__blockSize)
block = self.vidData.getBlock(frame, i, j, self.__blockSize)
blockDCT[cntr,:] = self.computeDCT(block, self.__blockSize, blockType)
cntr = cntr + 1
cntr_frame = cntr_frame + 1
if cntr_frame % framesPerCMP == 0 or cntr_frame==self.vidData.totalFrames:
#np.savetxt('DCT'+str(cntr_frame) + '.cmp', blockDCT[0:cntr,:], fmt='%1.1f')
np.save('DCTValues.npy',blockDCT)
cntr = 0
print('Wrote', 'DCT'+str(cntr_frame) + '.cmp')