def _run_(self): ''' ''' print '- Running Decoder...' hf = h.HuffCoDec() r, c, chnl = self.R, self.C, self.NCHNL Z = self.Z if self.mode == '444': for ch in range(chnl): hufcd = self.fl.readline()[:-1] # print hufcd[0:20] nblk, seqrec = hf.invhuff(hufcd, ch) for i in range(self.nBlkRows): for j in range(self.nBlkCols): blk = h.zagzig(seqrec[i*self.nBlkCols + j]) self.imRaw[r*i:r*i+r, c*j:c*j+c, ch] = np.round_( cv2.idct( blk*Z[:,:,ch] )) elif self.mode == '420': #import math as m if chnl == 1: rYmg = self.imRaw else: #Y = self.imRaw[:,:,0] Y = np.zeros( (self.M, self.N) ) dims, CrCb = h.adjImg( downsample(np.zeros( (self.M, self.N, 2) ), self.mode)[1] ) rYmg = [ Y, CrCb[:,:,0], CrCb[:,:,1] ] for ch in range(chnl): hufcd = self.fl.readline()[:-1] if ch == 0: rBLK = self.nBlkRows cBLK = self.nBlkCols else: rBLK, cBLK = int(np.floor(dims[0]/self.R)), int(np.floor(dims[1]/self.C)) # print hufcd[0:20] nblk, self.seqrec = hf.invhuff(hufcd, ch) for i in range(rBLK): for j in range(cBLK): blk = h.zagzig(self.seqrec[i*cBLK + j]) #print rYmg[ch][r*i:r*i+r, c*j:c*j+c].shape, ch, i, j rYmg[ch][r*i:r*i+r, c*j:c*j+c] = np.round_( cv2.idct( blk*Z[:,:,ch] )) # UPSAMPLE if chnl == 1: self.imRaw = rYmg #[:self.Mo, : self.No] else: self.imRaw[:,:,0] = rYmg[0] self.imRaw[:,:,1] = upsample(rYmg[1], self.mode)[:self.M, :self.N] self.imRaw[:,:,2] = upsample(rYmg[2], self.mode)[:self.M, :self.N] self.fl.close() imrec = cv2.cvtColor((self.imRaw[:self.Mo, :self.No]+128), cv2.COLOR_YCR_CB2BGR) imrec[imrec>255]=255 imrec[imrec<0]=0 print 'Decoder Complete...' return np.uint8(imrec)
def _run_(self): ''' ''' print '- Running Encoder...' hf = h.HuffCoDec() flnm = self.filepath.split('/')[-1:][0].split('.')[0] + '.huff' fo = open(flnm,'w') fo.write(str(self.Mo) + ',' + str(self.No) + ',' + str(self.Do) + ',' + str(self.qually) + ',' + self.mode + '\n') dYmg = self.Ymg - 128 r, c, chnl = self.r, self.c, self.NCHNL coefs = np.zeros((r, c, chnl)) seqhuff = '' #nbits = self.NumBits if self.mode == '444': for ch in range(chnl): DCant = 0 for i in range(self.nBlkRows): for j in range(self.nBlkCols): sbimg = dYmg[r*i:r*i+r, c*j:c*j+c, ch] #Subimagens nxn # TRANSFORMADA - Aplica DCT coefs = cv2.dct(sbimg) # QUANTIZAÇÃO/LIMIARIZAÇÃO zcoefs = np.round( coefs/self.Z[:,:,ch] ) #Coeficientes normalizados - ^T(u,v)=arred{T(u,v)/Z(u,v)} # CODIFICAÇÃO - Codigos de Huffman # - FOWARD HUFF seq = h.zigzag(zcoefs) #Gera Sequencia de coeficientes 1-D hfcd = hf.fwdhuff(DCant, seq, ch) #Gera o codigo huffman da subimagem DCant = seq[0] self.NumBits += hfcd[0] seqhuff += hfcd[1] #Salvar os codigos em arquivo fo.write(seqhuff+'\n') seqhuff = '' elif self.mode == '420': if chnl == 1: Ymg = dYmg else: Y = dYmg[:,:,0] dims, CrCb = h.adjImg(downsample(dYmg[:,:,1:3], self.mode)[1]) Ymg = [ Y, CrCb[:,:,0], CrCb[:,:,1] ] self.lYmg = Ymg for ch in range(chnl): DCant = 0 if ch == 0: #LUMINANCIA rBLK = self.nBlkRows cBLK = self.nBlkCols else: #CROMINANCIA rBLK, cBLK = int(np.floor(dims[0]/self.r)), int(np.floor(dims[1]/self.c)) for i in range(rBLK): for j in range(cBLK): sbimg = Ymg[ch][r*i:r*i+r, c*j:c*j+c] #Subimagens nxn # TRANSFORMADA - Aplica DCT coefs = cv2.dct(sbimg) # QUANTIZAÇÃO/LIMIARIZAÇÃO zcoefs = np.round( coefs/self.Z[:,:,ch] ) #Coeficientes normalizados - ^T(u,v)=arred{T(u,v)/Z(u,v)} # CODIFICAÇÃO - Codigos de Huffman - FOWARD HUFF seq = h.zigzag(zcoefs) #Gera Sequencia de coeficientes 1-D hfcd = hf.fwdhuff(DCant, seq, ch) #Gera o codigo huffman da subimagem DCant = seq[0] self.NumBits += hfcd[0] seqhuff += hfcd[1] #Salvar os codigos em arquivo fo.write(seqhuff + '\n') seqhuff = '' fo.close() self.avgBits = (float(self.NumBits)/float(self.Mo*self.No)) self.CRate = 24./self.avgBits self.Redunc = 1.-(1./self.CRate) print '- Encoder Complete...'
def _run_(self): ''' ''' hf = h.HuffCoDec( self.hufftables ) #flnm = self.filepath.split('/')[-1:][0].split('.')[0] + '.huff' #fo = open(flnm,'w') #fo.write(str(self.Mo) + ',' + str(self.No) + ',' + str(self.Do) + ',' + # str(self.qually) + ',' + self.mode + '\n') outseq = [] # dYmg = self.Ymg - 128 dYmg = self.Ymg - 128 r, c, chnl = self.r, self.c, self.NCHNL coefs = np.zeros((r, c, chnl)) if self.mode == '444': for ch in range(chnl): DCant = 0 seqhuff = '' #nbits = self.NumBits a, b = [0, 0] for i in range(self.nBlkRows): temp_seq = '' for j in range(self.nBlkCols): sbimg = dYmg[r * i:r * i + r, c * j:c * j + c, ch] #Subimagens nxn # TRANSFORMADA - Aplica DCT coefs = cv2.dct(sbimg) # SELEÇÃO DA MATRIZ DE QUANTIZAÇÃO vec_index = int(a * (self.nBlkCols / 2) + b) Z = self.Zhvs[int(abs(self.MV[vec_index][0]))][int( abs(self.MV[vec_index][1]))] # QUANTIZAÇÃO/LIMIARIZAÇÃO zcoefs = np.round( coefs / Z ) #Coeficientes normalizados - ^T(u,v)=arred{T(u,v)/Z(u,v)} # CODIFICAÇÃO - Codigos de Huffman # - FOWARD HUFF seq = h.zigzag( zcoefs) #Gera Sequencia de coeficientes 1-D hfcd = hf.fwdhuff( DCant, seq, ch) #Gera o codigo huffman da subimagem DCant = seq[0] self.NumBits += hfcd[0] temp_seq += hfcd[1] if j % 2 != 0: b += 1 if i % 2 != 0: a += 1 b = 0 else: b = 0 seqhuff += temp_seq #Salvar os codigos em arquivo #fo.write(seqhuff+'\n') outseq.append(seqhuff) elif self.mode == '420': if chnl == 1: Ymg = dYmg else: Y = dYmg[:, :, 0] dims, CrCb = h.adjImg( downsample(dYmg[:, :, 1:3], self.mode)[1]) Ymg = [Y, CrCb[:, :, 0], CrCb[:, :, 1]] self.lYmg = Ymg for ch in range(chnl): DCant = 0 seqhuff = '' #nbits = self.NumBits a, b = [0, 0] if ch == 0: #LUMINANCIA rBLK = self.nBlkRows cBLK = self.nBlkCols else: #CROMINANCIA rBLK, cBLK = int(np.floor(dims[0] / self.r)), int( np.floor(dims[1] / self.c)) for i in range(rBLK): for j in range(cBLK): sbimg = Ymg[ch][r * i:r * i + r, c * j:c * j + c] #Subimagens nxn # TRANSFORMADA - Aplica DCT coefs = cv2.dct(sbimg) # SELEÇÃO DA MATRIZ DE QUANTIZAÇÃO vec_index = 0 if ch == 0: #LUMINANCIA vec_index = int(a * (self.nBlkCols / 2) + b) else: #CROMINANCIA vec_index = int(a * (self.nBlkCols / 2) + b) Z = self.Zhvs[int(abs(self.MV[vec_index][0]))][int( abs(self.MV[vec_index][1]))] # QUANTIZAÇÃO/LIMIARIZAÇÃO zcoefs = np.round( coefs / Z ) #Coeficientes normalizados - ^T(u,v)=arred{T(u,v)/Z(u,v)} # CODIFICAÇÃO - Codigos de Huffman - FOWARD HUFF seq = h.zigzag( zcoefs) #Gera Sequencia de coeficientes 1-D hfcd = hf.fwdhuff( DCant, seq, ch) #Gera o codigo huffman da subimagem DCant = seq[0] self.NumBits += hfcd[0] seqhuff += hfcd[1] if ch == 0: if j % 2 != 0: b += 1 else: pass else: b += 1 if ch == 0: if i % 2 != 0: a += 1 b = 0 else: b = 0 else: a += 1 b = 0 outseq.append(seqhuff) #fo.close() self.avgBits = (float(self.NumBits) / float(self.M * self.N)) self.CRate = 24. / self.avgBits self.Redunc = 1. - (1. / self.CRate) #print '- Encoder Complete...' #return (self.CRate, self.Redunc, self.NumBits) return outseq
def _run_(self): ''' ''' #print '- Running Mjpeg Decoder...' hf = h.HuffCoDec(self.hufftables) r, c, chnl = self.R, self.C, self.NCHNL if self.mode == '444': for ch in range( chnl ): #hufcd = self.fl.readline()[:-1] # print hufcd[0:20] nblk, seqrec = hf.invhuff(self.huffcodes[ch], ch) a, b = [0, 0] for i in range(self.nBlkRows): for j in range(self.nBlkCols): # SELEÇÃO DA MATRIZ DE QUANTIZAÇÃO vec_index = int(a * (self.nBlkCols / 2) + b) # Quantization table Z = 0 if len(self.motionVec[vec_index]) == 2: Z = self.hvstables[abs( self.motionVec[vec_index][0])][abs( self.motionVec[vec_index][1])] elif len(self.motionVec[vec_index]) == 3: Z = self.hvstables[abs( self.motionVec[vec_index][1])][abs( self.motionVec[vec_index][2])] else: Z = self.hvstables[int( (abs(self.motionVec[vec_index][1]) + abs(self.motionVec[vec_index][3])) / 2.)][int( (abs(self.motionVec[vec_index][2]) + abs(self.motionVec[vec_index][4])) / 2.)] blk = h.zagzig(seqrec[i * self.nBlkCols + j]) self.imRaw[r * i:r * i + r, c * j:c * j + c, ch] = np.round_(cv2.idct(blk * Z)) if j % 2 != 0: b += 1 if i % 2 != 0: a += 1 b = 0 else: b = 0 elif self.mode == '420': #import math as m if chnl == 1: rYmg = self.imRaw else: #Y = self.imRaw[:,:,0] Y = np.zeros((self.M, self.N)) dims, CrCb = h.adjImg( downsample(np.zeros((self.M, self.N, 2)), self.mode)[1]) rYmg = [Y, CrCb[:, :, 0], CrCb[:, :, 1]] for ch in range(chnl): a, b = [0, 0] if ch == 0: rBLK = self.nBlkRows cBLK = self.nBlkCols else: rBLK, cBLK = int(np.floor(dims[0] / self.R)), int( np.floor(dims[1] / self.C)) # print hufcd[0:20] nblk, self.seqrec = hf.invhuff(self.huffcodes[ch], ch) for i in range(rBLK): for j in range(cBLK): # SELEÇÃO DA MATRIZ DE QUANTIZAÇÃO vec_index = 0 if ch == 0: #LUMINANCIA vec_index = int(a * (self.nBlkCols / 2) + b) else: #CROMINANCIA vec_index = int(a * (self.nBlkCols / 2) + b) # Quantization table Z = 0 if len(self.motionVec[vec_index]) == 2: Z = self.hvstables[abs( self.motionVec[vec_index][0])][abs( self.motionVec[vec_index][1])] elif len(self.motionVec[vec_index]) == 3: Z = self.hvstables[abs( self.motionVec[vec_index][1])][abs( self.motionVec[vec_index][2])] else: Z = self.hvstables[int( (abs(self.motionVec[vec_index][1]) + abs(self.motionVec[vec_index][3])) / 2.)][int( (abs(self.motionVec[vec_index][2]) + abs(self.motionVec[vec_index][4])) / 2.)] blk = h.zagzig(self.seqrec[i * cBLK + j]) #print rYmg[ch][r*i:r*i+r, c*j:c*j+c].shape, ch, i, j rYmg[ch][r * i:r * i + r, c * j:c * j + c] = np.round_( cv2.idct(blk * Z)) if ch == 0: if j % 2 != 0: b += 1 else: pass else: b += 1 if ch == 0: if i % 2 != 0: a += 1 b = 0 else: b = 0 else: a += 1 b = 0 # UPSAMPLE if chnl == 1: self.imRaw = rYmg #[:self.Mo, : self.No] else: self.imRaw[:, :, 0] = rYmg[0] self.imRaw[:, :, 1] = upsample(rYmg[1], self.mode)[:self.M, :self.N] self.imRaw[:, :, 2] = upsample(rYmg[2], self.mode)[:self.M, :self.N] #self.fl.close() # imrec = cv2.cvtColor((self.imRaw[:self.Mo, :self.No]+128), cv2.COLOR_YCR_CB2BGR) # imrec = self.imRaw[:self.Mo, :self.No]+128 imrec = self.imRaw + 128.0 # imrec[imrec>255.0]=255.0 # imrec[imrec<0.0]=0.0 #print 'Mjpeg Decoder Complete...' return imrec