tst_scene_render.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python
  2. # Python 2/3 compatibility
  3. from __future__ import print_function
  4. import numpy as np
  5. import cv2 as cv
  6. from numpy import pi, sin, cos
  7. defaultSize = 512
  8. class TestSceneRender():
  9. def __init__(self, bgImg = None, fgImg = None,
  10. deformation = False, speed = 0.25, **params):
  11. self.time = 0.0
  12. self.timeStep = 1.0 / 30.0
  13. self.foreground = fgImg
  14. self.deformation = deformation
  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. def getXOffset(self, time):
  31. return int( self.xAmpl*cos(time*self.speed))
  32. def getYOffset(self, time):
  33. return int(self.yAmpl*sin(time*self.speed))
  34. def setInitialRect(self, rect):
  35. self.initialRect = rect
  36. def getRectInTime(self, time):
  37. if self.foreground is not None:
  38. tmp = np.array(self.center) + np.array((self.getXOffset(time), self.getYOffset(time)))
  39. x0, y0 = tmp
  40. x1, y1 = tmp + self.foreground.shape[0:2]
  41. return np.array([y0, x0, y1, x1])
  42. else:
  43. x0, y0 = self.initialRect[0] + np.array((self.getXOffset(time), self.getYOffset(time)))
  44. x1, y1 = self.initialRect[2] + np.array((self.getXOffset(time), self.getYOffset(time)))
  45. return np.array([y0, x0, y1, x1])
  46. def getCurrentRect(self):
  47. if self.foreground is not None:
  48. x0 = self.currentCenter[0]
  49. y0 = self.currentCenter[1]
  50. x1 = self.currentCenter[0] + self.foreground.shape[0]
  51. y1 = self.currentCenter[1] + self.foreground.shape[1]
  52. return np.array([y0, x0, y1, x1])
  53. else:
  54. x0, y0 = self.currentRect[0]
  55. x1, y1 = self.currentRect[2]
  56. return np.array([x0, y0, x1, y1])
  57. def getNextFrame(self):
  58. img = self.sceneBg.copy()
  59. if self.foreground is not None:
  60. self.currentCenter = (self.center[0] + self.getXOffset(self.time), self.center[1] + self.getYOffset(self.time))
  61. img[self.currentCenter[0]:self.currentCenter[0]+self.foreground.shape[0],
  62. self.currentCenter[1]:self.currentCenter[1]+self.foreground.shape[1]] = self.foreground
  63. else:
  64. self.currentRect = self.initialRect + np.int( 30*cos(self.time*self.speed) + 50*sin(self.time*self.speed))
  65. if self.deformation:
  66. self.currentRect[1:3] += int(self.h/20*cos(self.time))
  67. cv.fillConvexPoly(img, self.currentRect, (0, 0, 255))
  68. self.time += self.timeStep
  69. return img
  70. def resetTime(self):
  71. self.time = 0.0
  72. def main():
  73. backGr = cv.imread(cv.samples.findFile('graf1.png'))
  74. fgr = cv.imread(cv.samples.findFile('box.png'))
  75. render = TestSceneRender(backGr, fgr)
  76. while True:
  77. img = render.getNextFrame()
  78. cv.imshow('img', img)
  79. ch = cv.waitKey(3)
  80. if ch == 27:
  81. break
  82. print('Done')
  83. if __name__ == '__main__':
  84. print(__doc__)
  85. main()
  86. cv.destroyAllWindows()