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 GObject
15 GObject.threads_init()
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 perspective.set_property("matrix", M)
64 print perspective.get_property("matrix")
67 perspective.set_property("matrix", [0])
68 print perspective.get_property("matrix")
70 videoconvert = Gst.ElementFactory.make("autovideoconvert", None)
71 pipeline.add(videoconvert)
73 videosink = Gst.ElementFactory.make("autovideosink", None)
74 pipeline.add(videosink)
76 caps = Gst.caps_from_string(
77 "video/x-raw,format=\"AYUV\",width=800,height=480")
79 videosrc.link_filtered(perspective, caps)
80 perspective.link(videoconvert)
81 videoconvert.link(videosink)
83 pipeline.set_state(Gst.State.PLAYING)
85 loop = GObject.MainLoop()
89 if __name__ == '__main__':