コード例 #1
0
class BFrameHandle():
	def __init__(self, QP):
		self.data = []
		self.IMT = ImageTransform()
		self.IT = IntegerTransform()
		self.IT.QuantizationMatrix(QP)

	def encode3Channels(self, IFrame, PFrame, BFrame):
		yuvI = self.IMT.rgb2yuv(IFrame)
		yuvP = self.IMT.rgb2yuv(PFrame)
		yuvB = self.IMT.rgb2yuv(BFrame)

		[Y_I, Cr_I, Cb_I] = self.IMT.chromaSub(yuvI)
		[Y_P, Cr_P, Cb_P] = self.IMT.chromaSub(yuvP)
		[Y_B, Cr_B, Cb_B] = self.IMT.chromaSub(yuvB)

		[diffY, motionInfoY] = self.encode(Y_I,Y_P,Y_B)
		[diffCr, motionInfoCr] = self.encode(Cr_I,Cr_P,Cr_B)
		[diffCb, motionInfoCb] = self.encode(Cb_I,Cb_P,Cb_B)

		return [diffY, motionInfoY, diffCr, motionInfoCr, diffCb, motionInfoCb]
	
	def encode(self, IFrame, PFrame, BFrame):
		mvB = MotionVecB(IFrame, PFrame, BFrame) #Initialize A instance
		#[motionVect1, minMad1, motionVect2, minMad2]
		motionInfo = mvB.getTwoMotionVector()

		estimatedBFrame = mvB.recoverPfromI(IFrame, PFrame, motionInfo)
		diffEstMinusReal = BFrame - estimatedBFrame 

		# Integer Transfer		
		diffEstMinusRealVec = self.IMT.vecMat(diffEstMinusReal, 4)
		diffEstMinusRealVecIntTran = self.IT.EnIntegerTransformVec(diffEstMinusRealVec)

		return [diffEstMinusRealVecIntTran, motionInfo]

	def decode3Channels(self, IFrame, PFrame, diffAndmotionVector):
		yuvI = self.IMT.rgb2yuv(IFrame)
		yuvP = self.IMT.rgb2yuv(PFrame)

		[Y_I, Cr_I, Cb_I] = self.IMT.chromaSub(yuvI)
		[Y_P, Cr_P, Cb_P] = self.IMT.chromaSub(yuvP)

		[diffY, motionInfoY, diffCr, motionInfoCr, diffCb, motionInfoCb] = diffAndmotionVector		

		Y_B = self.decode(Y_I, Y_P, diffY, motionInfoY)
		Cr_B = self.decode(Cr_I, Cr_P, diffCr, motionInfoCr)
		Cb_B = self.decode(Cb_I, Cb_P, diffCb, motionInfoCb)

		#Expand all 3 channels
		yuvRec = self.IMT.chromaExpand(Y_B, Cr_B, Cb_B)
		rgbImRec = self.IMT.yuv2rgb(yuvRec)
		rgbIm =  self.IMT.double2uintImage(rgbImRec)

		return rgbIm


	def decode(self, IFrame, PFrame, diff,  motionInfo):
		mvB = MotionVecB(IFrame, PFrame, np.zeros_like(IFrame)) #Here use both I frame to initialize, as no need for Pframe
		estimatedBFrame = mvB.recoverPfromI(IFrame, PFrame, motionInfo)
		
		#DeInteger Transform for diff
		diffDetraned = self.IT.DeIntegerTransformVec(diff)
		diffRec = self.IMT.dvecMat(np.shape(IFrame), diffDetraned, 4)

		PFrame = estimatedBFrame + diffRec


		return PFrame
コード例 #2
0
import cv2
from MotionVec import *
from IntegerTransform import *
from ImageTransform import *
from FrameHandleHelpers import *
from IFrameHandle import *

#frames = []
QP = 0
cycleCount = 0
CONSTANT_VIDEO_PATH = "SampleVideo_360x240_50mb.mp4"
cap = cv2.VideoCapture(CONSTANT_VIDEO_PATH)

IMT = ImageTransform()
IT = IntegerTransform()
IT.QuantizationMatrix(0)

FRameBuffer = []
Frame = []
FrameDecode = []

PHand = PFrameHandle(QP)
IHand = IFrameHandle(QP)
'''
Displaying Sequence: I P P I P P I P P  
Coding Sequence I P B B P B B I B B 
'''

while True:
    if cycleCount % 3 == 0:  #IFrame
        ret, Frame = cap.read()
コード例 #3
0
class IntraCoding():
	"""This class is for intra coding and decoding for I frames"""
	def __init__(self,QP):
		#self.deintra_image = []
		self.intra_image = []
		self.IT = IntegerTransform()
		self.IT.QuantizationMatrix(QP)
		

	def IntraEncoding(self,Y,Y_r,i,j,N):
		rows = i - 1
		cols = j - 1
		T = Y_r[rows,cols+1:cols+N+1]
		L = Y_r[rows+1:rows+N+1,cols]
		c0 = self.mode0(Y,T,N)
		c1 = self.mode1(Y,L,N)
		c2 = self.mode2(Y,L,T,N)
		BK = Y[rows+1:rows+1+N,cols+1:cols+1+N]
		SOD = [0,0,0]
		SOD[0] = sum(sum(abs(c0-BK)))
		SOD[1] = sum(sum(abs(c1-BK)))
		SOD[2] = sum(sum(abs(c2-BK)))
		mode = SOD.index(min(SOD))
		if (mode == 0):
			self.intra_image[i:i+4,j:j+4] = c0
			return BK - c0
		elif (mode == 1):
			self.intra_image[i:i+4,j:j+4] = c1
			return BK - c1
		elif (mode == 2):
			self.intra_image[i:i+4,j:j+4] = c2
			return BK - c2

	'''def IntraDecoding(self,Y,mode,i,j,N):
		rows = i - 1
		cols = j - 1
		for k in range(0,2*N):
			T(k) = Y(rows,cols+k)
			L(k) = Y(rows+k,cols)
		if (mode == 0):
			self.deintra_image(i:i+4,j:j+4) = mode0(Y,T,N)
		elif (mode == 1):
			self.deintra_image(i:i+4,j:j+4) = mode1(Y,L,N)
		elif (mode == 2):
			self.deintra_image(i:i+4,j:j+4) = mode2(Y,L,T,N)
		elif (mode == 3):
			self.deintra_image(i:i+4,j:j+4) = np.zeros_like(Y(0:4,0:4))'''
	

	def IntraCodingVec(self,Y):
		self.intra_image = np.zeros_like(Y)
		Y_r = np.zeros_like(Y)
		for m in range(np.shape(Y)[0]//16):
			for n in range(np.shape(Y)[1]//16):
				for i in range(m*4,m*4+4):
					for j in range(n*4,n*4+4):
						BK = Y[i*4:i*4+4,j*4:j*4+4]
						if (i == 0 and j == 0):
							mode = 3
							ICP_en = self.IT.EnIntegerTransform(BK)
							Y_r[i*4:i*4+4,j*4:j*4+4] = self.IT.DeIntegerTransform(ICP_en)
						elif (j == 0):
							rows = i - 1
							T = Y_r[rows,0:4]
							mode = 0
							self.intra_image[i*4:i*4+4,j*4:j*4+4] = self.mode0(Y,T,4)
							ICP = BK - self.intra_image[i*4:i*4+4,j*4:j*4+4]
							ICP_en = self.IT.EnIntegerTransform(ICP)
							ICP_r = self.IT.DeIntegerTransform(ICP_en)
							Y_r[i*4:i*4+4,j*4:j*4+4] = self.intra_image[i*4:i*4+4,j*4:j*4+4] + ICP_r
						elif (i == 0):
							cols = j - 1
							L = Y_r[0:4,cols]
							mode = 1
							self.intra_image[i*4:i*4+4,j*4:j*4+4] = self.mode1(Y,L,4)
							ICP = BK - self.intra_image[i*4:i*4+4,j*4:j*4+4]
							ICP_en = self.IT.EnIntegerTransform(ICP)
							ICP_r = self.IT.DeIntegerTransform(ICP_en)
	
							Y_r[i*4:i*4+4,j*4:j*4+4] = self.intra_image[i*4:i*4+4,j*4:j*4+4] + ICP_r
						else:
							ICP = self.IntraEncoding(Y,Y_r,i*4,j*4,4)
							ICP_en = self.IT.EnIntegerTransform(ICP)
							ICP_r = self.IT.DeIntegerTransform(ICP_en)
							Y_r[i*4:i*4+4,j*4:j*4+4] = self.intra_image[i*4:i*4+4,j*4:j*4+4] + ICP_r

		return Y_r
		

	'''def IntraDeCodingVec(self,Y):
		self.deintra_image = np.zeros_like(Y)
		for m in range(np.shape(Y)[0]//16):
			for n in range(np.shape(Y)[1]//16):
				for i in range(m*4,m*4+4):
					for j in range(n*4,n*4+4):
						self.IntraDeCoding(Y,self.mode(i,j),i*4,j*4,4)'''
		



	def mode0(self,Y,T,N):
		out = np.zeros_like(Y[0:4,0:4])
		for i in range(N):
			for j in range(N):
				out[i,j] = T[i]
		return out
	def mode1(self,Y,L,N):
		out = np.zeros_like(Y[0:4,0:4])
		for i in range(N):
			for j in range(N):
				out[i,j] = L[i]
		return out
	def mode2(self,Y,L,T,N):
		out = np.zeros_like(Y[0:4,0:4])
		for i in range(N):
			for j in range(N):
				out[i,j] = round(np.mean(np.concatenate([L,T]))+4/9.0)
		return out