Path: blob/master/modules/python/test/tst_scene_render.py
16337 views
#!/usr/bin/env python123# Python 2/3 compatibility4from __future__ import print_function56import numpy as np7from numpy import pi, sin, cos89import cv2 as cv1011defaultSize = 5121213class TestSceneRender():1415def __init__(self, bgImg = None, fgImg = None, deformation = False, noise = 0.0, speed = 0.25, **params):16self.time = 0.017self.timeStep = 1.0 / 30.018self.foreground = fgImg19self.deformation = deformation20self.noise = noise21self.speed = speed2223if bgImg is not None:24self.sceneBg = bgImg.copy()25else:26self.sceneBg = np.zeros(defaultSize, defaultSize, np.uint8)2728self.w = self.sceneBg.shape[0]29self.h = self.sceneBg.shape[1]3031if fgImg is not None:32self.foreground = fgImg.copy()33self.center = self.currentCenter = (int(self.w/2 - fgImg.shape[0]/2), int(self.h/2 - fgImg.shape[1]/2))3435self.xAmpl = self.sceneBg.shape[0] - (self.center[0] + fgImg.shape[0])36self.yAmpl = self.sceneBg.shape[1] - (self.center[1] + fgImg.shape[1])3738self.initialRect = np.array([ (self.h/2, self.w/2), (self.h/2, self.w/2 + self.w/10),39(self.h/2 + self.h/10, self.w/2 + self.w/10), (self.h/2 + self.h/10, self.w/2)]).astype(int)40self.currentRect = self.initialRect41np.random.seed(10)4243def getXOffset(self, time):44return int(self.xAmpl*cos(time*self.speed))454647def getYOffset(self, time):48return int(self.yAmpl*sin(time*self.speed))4950def setInitialRect(self, rect):51self.initialRect = rect5253def getRectInTime(self, time):5455if self.foreground is not None:56tmp = np.array(self.center) + np.array((self.getXOffset(time), self.getYOffset(time)))57x0, y0 = tmp58x1, y1 = tmp + self.foreground.shape[0:2]59return np.array([y0, x0, y1, x1])60else:61x0, y0 = self.initialRect[0] + np.array((self.getXOffset(time), self.getYOffset(time)))62x1, y1 = self.initialRect[2] + np.array((self.getXOffset(time), self.getYOffset(time)))63return np.array([y0, x0, y1, x1])6465def getCurrentRect(self):6667if self.foreground is not None:6869x0 = self.currentCenter[0]70y0 = self.currentCenter[1]71x1 = self.currentCenter[0] + self.foreground.shape[0]72y1 = self.currentCenter[1] + self.foreground.shape[1]73return np.array([y0, x0, y1, x1])74else:75x0, y0 = self.currentRect[0]76x1, y1 = self.currentRect[2]77return np.array([x0, y0, x1, y1])7879def getNextFrame(self):80img = self.sceneBg.copy()8182if self.foreground is not None:83self.currentCenter = (self.center[0] + self.getXOffset(self.time), self.center[1] + self.getYOffset(self.time))84img[self.currentCenter[0]:self.currentCenter[0]+self.foreground.shape[0],85self.currentCenter[1]:self.currentCenter[1]+self.foreground.shape[1]] = self.foreground86else:87self.currentRect = self.initialRect + np.int( 30*cos(self.time) + 50*sin(self.time/3))88if self.deformation:89self.currentRect[1:3] += int(self.h/20*cos(self.time))90cv.fillConvexPoly(img, self.currentRect, (0, 0, 255))9192self.time += self.timeStep9394if self.noise:95noise = np.zeros(self.sceneBg.shape, np.int8)96cv.randn(noise, np.zeros(3), np.ones(3)*255*self.noise)97img = cv.add(img, noise, dtype=cv.CV_8UC3)98return img99100def resetTime(self):101self.time = 0.0102103104if __name__ == '__main__':105106backGr = cv.imread('../../../samples/data/lena.jpg')107108render = TestSceneRender(backGr, noise = 0.5)109110while True:111112img = render.getNextFrame()113cv.imshow('img', img)114115ch = cv.waitKey(3)116if ch == 27:117break118cv.destroyAllWindows()119120121