diff options
-rw-r--r-- | src/argaze/AreaOfInterest/AOI2DScene.py | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/src/argaze/AreaOfInterest/AOI2DScene.py b/src/argaze/AreaOfInterest/AOI2DScene.py index 8301e23..60f17ef 100644 --- a/src/argaze/AreaOfInterest/AOI2DScene.py +++ b/src/argaze/AreaOfInterest/AOI2DScene.py @@ -7,15 +7,18 @@ __credits__ = [] __copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)" __license__ = "BSD" -from typing import Tuple +from typing import TypeVar, Tuple from argaze import DataStructures from argaze.AreaOfInterest import AOIFeatures from argaze import GazeFeatures -import cv2 as cv +import cv2 import numpy +AOI2DSceneType = TypeVar('AOI2DScene', bound="AOI2DScene") +# Type definition for type annotation convenience + class AOI2DScene(AOIFeatures.AOIScene): """Define AOI 2D scene.""" @@ -60,7 +63,7 @@ class AOI2DScene(AOIFeatures.AOIScene): if matching: top_left_corner_pixel = numpy.rint(aoi.clockwise()[0]).astype(int) - cv.putText(frame, name, top_left_corner_pixel, cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv.LINE_AA) + cv2.putText(frame, name, top_left_corner_pixel, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) # Draw form aoi.draw(frame, color) @@ -101,10 +104,40 @@ class AOI2DScene(AOIFeatures.AOIScene): if matching: top_left_corner_pixel = numpy.rint(aoi.clockwise()[0]).astype(int) - cv.putText(frame, name, top_left_corner_pixel, cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv.LINE_AA) + cv2.putText(frame, name, top_left_corner_pixel, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA) # Draw matching region matching_region.draw(frame, matching_color, 4) # Draw form aoi.draw(frame, color) + + def reframe(self, aoi_name: str, size: tuple) -> AOI2DSceneType: + """ + Project whole scene into an AOI. + + Parameters: + aoi: name of AOI where to project scene + + Returns: + reframed AOI 2D scene + """ + + assert(self[aoi_name].points_number == 4) + + # Edit perspective (P) allowing to transform source axis (Src) into destination axis (Dst) + Src = self[aoi_name].clockwise().astype(numpy.float32) + Src_origin = Src[0] + Src = Src - Src_origin + Dst = numpy.float32([[0, 0], [size[0], 0], [size[0], size[1]], [0, size[1]]]) + + P = cv2.getAffineTransform(Src[:3], Dst[:3])[:, :2] + + # Apply perspective matrix to each AOI + aoi2D_scene = AOI2DScene() + + for name, aoi2D in self.items(): + + aoi2D_scene[name] = numpy.matmul(aoi2D - Src_origin, P.T) + + return aoi2D_scene
\ No newline at end of file |