Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/python/test/tst_scene_render.py
16337 views
1
#!/usr/bin/env python
2
3
4
# Python 2/3 compatibility
5
from __future__ import print_function
6
7
import numpy as np
8
from numpy import pi, sin, cos
9
10
import cv2 as cv
11
12
defaultSize = 512
13
14
class TestSceneRender():
15
16
def __init__(self, bgImg = None, fgImg = None, deformation = False, noise = 0.0, speed = 0.25, **params):
17
self.time = 0.0
18
self.timeStep = 1.0 / 30.0
19
self.foreground = fgImg
20
self.deformation = deformation
21
self.noise = noise
22
self.speed = speed
23
24
if bgImg is not None:
25
self.sceneBg = bgImg.copy()
26
else:
27
self.sceneBg = np.zeros(defaultSize, defaultSize, np.uint8)
28
29
self.w = self.sceneBg.shape[0]
30
self.h = self.sceneBg.shape[1]
31
32
if fgImg is not None:
33
self.foreground = fgImg.copy()
34
self.center = self.currentCenter = (int(self.w/2 - fgImg.shape[0]/2), int(self.h/2 - fgImg.shape[1]/2))
35
36
self.xAmpl = self.sceneBg.shape[0] - (self.center[0] + fgImg.shape[0])
37
self.yAmpl = self.sceneBg.shape[1] - (self.center[1] + fgImg.shape[1])
38
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
np.random.seed(10)
43
44
def getXOffset(self, time):
45
return int(self.xAmpl*cos(time*self.speed))
46
47
48
def getYOffset(self, time):
49
return int(self.yAmpl*sin(time*self.speed))
50
51
def setInitialRect(self, rect):
52
self.initialRect = rect
53
54
def getRectInTime(self, time):
55
56
if self.foreground is not None:
57
tmp = np.array(self.center) + np.array((self.getXOffset(time), self.getYOffset(time)))
58
x0, y0 = tmp
59
x1, y1 = tmp + self.foreground.shape[0:2]
60
return np.array([y0, x0, y1, x1])
61
else:
62
x0, y0 = self.initialRect[0] + np.array((self.getXOffset(time), self.getYOffset(time)))
63
x1, y1 = self.initialRect[2] + np.array((self.getXOffset(time), self.getYOffset(time)))
64
return np.array([y0, x0, y1, x1])
65
66
def getCurrentRect(self):
67
68
if self.foreground is not None:
69
70
x0 = self.currentCenter[0]
71
y0 = self.currentCenter[1]
72
x1 = self.currentCenter[0] + self.foreground.shape[0]
73
y1 = self.currentCenter[1] + self.foreground.shape[1]
74
return np.array([y0, x0, y1, x1])
75
else:
76
x0, y0 = self.currentRect[0]
77
x1, y1 = self.currentRect[2]
78
return np.array([x0, y0, x1, y1])
79
80
def getNextFrame(self):
81
img = self.sceneBg.copy()
82
83
if self.foreground is not None:
84
self.currentCenter = (self.center[0] + self.getXOffset(self.time), self.center[1] + self.getYOffset(self.time))
85
img[self.currentCenter[0]:self.currentCenter[0]+self.foreground.shape[0],
86
self.currentCenter[1]:self.currentCenter[1]+self.foreground.shape[1]] = self.foreground
87
else:
88
self.currentRect = self.initialRect + np.int( 30*cos(self.time) + 50*sin(self.time/3))
89
if self.deformation:
90
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
if self.noise:
96
noise = np.zeros(self.sceneBg.shape, np.int8)
97
cv.randn(noise, np.zeros(3), np.ones(3)*255*self.noise)
98
img = cv.add(img, noise, dtype=cv.CV_8UC3)
99
return img
100
101
def resetTime(self):
102
self.time = 0.0
103
104
105
if __name__ == '__main__':
106
107
backGr = cv.imread('../../../samples/data/lena.jpg')
108
109
render = TestSceneRender(backGr, noise = 0.5)
110
111
while True:
112
113
img = render.getNextFrame()
114
cv.imshow('img', img)
115
116
ch = cv.waitKey(3)
117
if ch == 27:
118
break
119
cv.destroyAllWindows()
120
121