3 # test program for the "perspective" geometric transform element
7 from math import cos, sin, radians
10 gi.require_version('Gst', '1.0')
11 from gi.repository import Gst
14 from gi.repository import GLib
15 from gi.repository import GObject
21 width, height = 800, 480
22 corners = np.array([(0, 0), (width, 0), (width, height), (0, height)], dtype=np.float32)
23 target = np.array([(top_x_shift, 0), (width - top_x_shift, 0), (width, height), (0, height)], dtype=np.float32)
25 mat = cv2.getPerspectiveTransform(corners, target)
26 ret, inv_mat = cv2.invert(mat)
28 return inv_mat.flatten()
31 def calc_rotation_matrix():
32 width, height = 800, 480
34 pivot = (width / 2, height / 2)
36 theta = radians(angle)
38 # The dimensions of the bounding box of the rotated rectangle
39 W = width * abs(cos(theta)) + height * abs(sin(theta))
40 H = width * abs(sin(theta)) + height * abs(cos(theta))
42 scale_factor = 1 / min(width / W, height / H)
44 mat = cv2.getRotationMatrix2D(pivot, angle, scale_factor)
45 mat = np.vstack([mat, [0, 0, 1]])
51 pipeline = Gst.ElementFactory.make('pipeline', None)
53 videosrc = Gst.ElementFactory.make('videotestsrc', None)
54 pipeline.add(videosrc)
56 # Test the perspective element
57 perspective = Gst.ElementFactory.make("perspective", None)
58 pipeline.add(perspective)
60 print(perspective.get_property("matrix"))
62 M = calc_rotation_matrix()
63 M2 = GObject.ValueArray()
66 perspective.set_property("matrix", M2)
67 print(perspective.get_property("matrix"))
70 perspective.set_property("matrix", [0])
71 print(perspective.get_property("matrix"))
73 videoconvert = Gst.ElementFactory.make("autovideoconvert", None)
74 pipeline.add(videoconvert)
76 videosink = Gst.ElementFactory.make("autovideosink", None)
77 pipeline.add(videosink)
79 caps = Gst.caps_from_string(
80 "video/x-raw,format=\"AYUV\",width=800,height=480")
82 videosrc.link_filtered(perspective, caps)
83 perspective.link(videoconvert)
84 videoconvert.link(videosink)
86 pipeline.set_state(Gst.State.PLAYING)
88 loop = GLib.MainLoop()
92 if __name__ == '__main__':