tst_scene_render.py 3.86 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/usr/bin/env python


# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
from numpy import pi, sin, cos

10
import cv2 as cv
11 12 13 14 15

defaultSize = 512

class TestSceneRender():

16
    def __init__(self, bgImg = None, fgImg = None, deformation = False, noise = 0.0, speed = 0.25, **params):
17 18
        self.time = 0.0
        self.timeStep = 1.0 / 30.0
19
        self.foreground = fgImg
20
        self.deformation = deformation
21 22
        self.noise = noise
        self.speed = speed
23

24
        if bgImg is not None:
25 26
            self.sceneBg = bgImg.copy()
        else:
27
            self.sceneBg = np.zeros(defaultSize, defaultSize, np.uint8)
28 29 30 31

        self.w = self.sceneBg.shape[0]
        self.h = self.sceneBg.shape[1]

32
        if fgImg is not None:
33 34 35 36 37 38
            self.foreground = fgImg.copy()
            self.center = self.currentCenter = (int(self.w/2 - fgImg.shape[0]/2), int(self.h/2 - fgImg.shape[1]/2))

            self.xAmpl = self.sceneBg.shape[0] - (self.center[0] + fgImg.shape[0])
            self.yAmpl = self.sceneBg.shape[1] - (self.center[1] + fgImg.shape[1])

39
        self.initialRect = np.array([ (self.h/2, self.w/2), (self.h/2, self.w/2 + self.w/10),
40
         (self.h/2 + self.h/10, self.w/2 + self.w/10), (self.h/2 + self.h/10, self.w/2)]).astype(int)
41
        self.currentRect = self.initialRect
42 43 44 45 46 47 48 49
        np.random.seed(10)

    def getXOffset(self, time):
        return int(self.xAmpl*cos(time*self.speed))


    def getYOffset(self, time):
        return int(self.yAmpl*sin(time*self.speed))
50 51 52 53

    def setInitialRect(self, rect):
        self.initialRect = rect

54 55
    def getRectInTime(self, time):

56
        if self.foreground is not None:
57 58 59 60 61 62 63 64 65
            tmp = np.array(self.center) + np.array((self.getXOffset(time), self.getYOffset(time)))
            x0, y0 = tmp
            x1, y1 = tmp + self.foreground.shape[0:2]
            return np.array([y0, x0, y1, x1])
        else:
            x0, y0 = self.initialRect[0] + np.array((self.getXOffset(time), self.getYOffset(time)))
            x1, y1 = self.initialRect[2] + np.array((self.getXOffset(time), self.getYOffset(time)))
            return np.array([y0, x0, y1, x1])

66
    def getCurrentRect(self):
67

68
        if self.foreground is not None:
69 70 71 72 73 74 75 76 77 78

            x0 = self.currentCenter[0]
            y0 = self.currentCenter[1]
            x1 = self.currentCenter[0] + self.foreground.shape[0]
            y1 = self.currentCenter[1] + self.foreground.shape[1]
            return np.array([y0, x0, y1, x1])
        else:
            x0, y0 = self.currentRect[0]
            x1, y1 = self.currentRect[2]
            return np.array([x0, y0, x1, y1])
79 80 81 82

    def getNextFrame(self):
        img = self.sceneBg.copy()

83
        if self.foreground is not None:
84 85 86 87 88 89 90
            self.currentCenter = (self.center[0] + self.getXOffset(self.time), self.center[1] + self.getYOffset(self.time))
            img[self.currentCenter[0]:self.currentCenter[0]+self.foreground.shape[0],
             self.currentCenter[1]:self.currentCenter[1]+self.foreground.shape[1]] = self.foreground
        else:
            self.currentRect = self.initialRect + np.int( 30*cos(self.time) + 50*sin(self.time/3))
            if self.deformation:
                self.currentRect[1:3] += int(self.h/20*cos(self.time))
91
            cv.fillConvexPoly(img, self.currentRect, (0, 0, 255))
92

93
        self.time += self.timeStep
94

95 96
        if self.noise:
            noise = np.zeros(self.sceneBg.shape, np.int8)
97 98
            cv.randn(noise, np.zeros(3), np.ones(3)*255*self.noise)
            img = cv.add(img, noise, dtype=cv.CV_8UC3)
99 100 101 102 103 104 105 106
        return img

    def resetTime(self):
        self.time = 0.0


if __name__ == '__main__':

107
    backGr = cv.imread('../../../samples/data/lena.jpg')
108

109
    render = TestSceneRender(backGr, noise = 0.5)
110 111 112 113

    while True:

        img = render.getNextFrame()
114
        cv.imshow('img', img)
115

116
        ch = cv.waitKey(3)
117
        if ch == 27:
118
            break
119
    cv.destroyAllWindows()