X-Git-Url: https://git.ao2.it/experiments/gstreamer.git/blobdiff_plain/7bdb0839fbadd5c43133b7e14a49d41d3dd84e44..a594457d7a30ecb0ec09d2193f9ea789d1430e46:/python/gst-test-perspective.py?ds=sidebyside diff --git a/python/gst-test-perspective.py b/python/gst-test-perspective.py index b51591a..1b094c2 100755 --- a/python/gst-test-perspective.py +++ b/python/gst-test-perspective.py @@ -1,8 +1,10 @@ #!/usr/bin/env python -# test program for the "perspective" geometric tranform element +# test program for the "perspective" geometric transform element -import cv +import cv2 +import numpy as np +from math import cos, sin, radians import gi gi.require_version('Gst', '1.0') @@ -17,17 +19,32 @@ def calc_matrix(): top_x_shift = 200 width, height = 800, 480 - corners = [(0, 0), (width, 0), (width, height), (0, height)] - target = [(top_x_shift, 0), (width - top_x_shift, 0), (width, height), (0, height)] + corners = np.array([(0, 0), (width, 0), (width, height), (0, height)], dtype=np.float32) + target = np.array([(top_x_shift, 0), (width - top_x_shift, 0), (width, height), (0, height)], dtype=np.float32) - mat = cv.CreateMat(3, 3, cv.CV_64F) - cv.GetPerspectiveTransform(corners, target, mat) + mat = cv2.getPerspectiveTransform(corners, target) + ret, inv_mat = cv2.invert(mat) - inv_mat = cv.CreateMat(3, 3, cv.CV_64F) - cv.Invert(mat, inv_mat) + return inv_mat.flatten() - matrix = [inv_mat[j, i] for j in range(mat.rows) for i in range(mat.cols)] - return matrix + +def calc_rotation_matrix(): + width, height = 800, 480 + + pivot = (width / 2, height / 2) + angle = 10 + theta = radians(angle) + + # The dimensions of the bounding box of the rotated rectangle + W = width * abs(cos(theta)) + height * abs(sin(theta)) + H = width * abs(sin(theta)) + height * abs(cos(theta)) + + scale_factor = 1 / min(width / W, height / H) + + mat = cv2.getRotationMatrix2D(pivot, angle, scale_factor) + mat = np.vstack([mat, [0, 0, 1]]) + + return mat.flatten() def main(): @@ -42,7 +59,7 @@ def main(): print perspective.get_property("matrix") - M = calc_matrix() + M = calc_rotation_matrix() perspective.set_property("matrix", M) print perspective.get_property("matrix")