tst_scene_render.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python
  2. # Python 2/3 compatibility
  3. from __future__ import print_function
  4. import numpy as np
  5. from numpy import pi, sin, cos
  6. import cv2 as cv
  7. defaultSize = 512
  8. class TestSceneRender():
  9. def __init__(self, bgImg = None, fgImg = None, deformation = False, noise = 0.0, speed = 0.25, **params):
  10. self.time = 0.0
  11. self.timeStep = 1.0 / 30.0
  12. self.foreground = fgImg
  13. self.deformation = deformation
  14. self.noise = noise
  15. self.speed = speed
  16. if bgImg is not None:
  17. self.sceneBg = bgImg.copy()
  18. else:
  19. self.sceneBg = np.zeros(defaultSize, defaultSize, np.uint8)
  20. self.w = self.sceneBg.shape[0]
  21. self.h = self.sceneBg.shape[1]
  22. if fgImg is not None:
  23. self.foreground = fgImg.copy()
  24. self.center = self.currentCenter = (int(self.w/2 - fgImg.shape[0]/2), int(self.h/2 - fgImg.shape[1]/2))
  25. self.xAmpl = self.sceneBg.shape[0] - (self.center[0] + fgImg.shape[0])
  26. self.yAmpl = self.sceneBg.shape[1] - (self.center[1] + fgImg.shape[1])
  27. self.initialRect = np.array([ (self.h/2, self.w/2), (self.h/2, self.w/2 + self.w/10),
  28. (self.h/2 + self.h/10, self.w/2 + self.w/10), (self.h/2 + self.h/10, self.w/2)]).astype(int)
  29. self.currentRect = self.initialRect
  30. np.random.seed(10)
  31. def getXOffset(self, time):
  32. return int(self.xAmpl*cos(time*self.speed))
  33. def getYOffset(self, time):
  34. return int(self.yAmpl*sin(time*self.speed))
  35. def setInitialRect(self, rect):
  36. self.initialRect = rect
  37. def getRectInTime(self, time):
  38. if self.foreground is not None:
  39. tmp = np.array(self.center) + np.array((self.getXOffset(time), self.getYOffset(time)))
  40. x0, y0 = tmp
  41. x1, y1 = tmp + self.foreground.shape[0:2]
  42. return np.array([y0, x0, y1, x1])
  43. else:
  44. x0, y0 = self.initialRect[0] + np.array((self.getXOffset(time), self.getYOffset(time)))
  45. x1, y1 = self.initialRect[2] + np.array((self.getXOffset(time), self.getYOffset(time)))
  46. return np.array([y0, x0, y1, x1])
  47. def getCurrentRect(self):
  48. if self.foreground is not None:
  49. x0 = self.currentCenter[0]
  50. y0 = self.currentCenter[1]
  51. x1 = self.currentCenter[0] + self.foreground.shape[0]
  52. y1 = self.currentCenter[1] + self.foreground.shape[1]
  53. return np.array([y0, x0, y1, x1])
  54. else:
  55. x0, y0 = self.currentRect[0]
  56. x1, y1 = self.currentRect[2]
  57. return np.array([x0, y0, x1, y1])
  58. def getNextFrame(self):
  59. img = self.sceneBg.copy()
  60. if self.foreground is not None:
  61. self.currentCenter = (self.center[0] + self.getXOffset(self.time), self.center[1] + self.getYOffset(self.time))
  62. img[self.currentCenter[0]:self.currentCenter[0]+self.foreground.shape[0],
  63. self.currentCenter[1]:self.currentCenter[1]+self.foreground.shape[1]] = self.foreground
  64. else:
  65. self.currentRect = self.initialRect + int( 30*cos(self.time) + 50*sin(self.time/3))
  66. if self.deformation:
  67. self.currentRect[1:3] += int(self.h/20*cos(self.time))
  68. cv.fillConvexPoly(img, self.currentRect, (0, 0, 255))
  69. self.time += self.timeStep
  70. if self.noise:
  71. noise = np.zeros(self.sceneBg.shape, np.int8)
  72. cv.randn(noise, np.zeros(3), np.ones(3)*255*self.noise)
  73. img = cv.add(img, noise, dtype=cv.CV_8UC3)
  74. return img
  75. def resetTime(self):
  76. self.time = 0.0
  77. if __name__ == '__main__':
  78. backGr = cv.imread('../../../samples/data/lena.jpg')
  79. render = TestSceneRender(backGr, noise = 0.5)
  80. while True:
  81. img = render.getNextFrame()
  82. cv.imshow('img', img)
  83. ch = cv.waitKey(3)
  84. if ch == 27:
  85. break
  86. cv.destroyAllWindows()