Examples >> Computing motion
Fork me on GitHub

Computing motionΒΆ

For this example, BigBuckBunny will be used to show off the computed motion vectors. Using scikit-video, we can produce one sequence showing motion vectors, additional statistical information, and the source video itself.

 1import matplotlib.pyplot as plt
 2import numpy as np
 3
 4import skvideo.datasets
 5import skvideo.io
 6import skvideo.motion
 7from skvideo.utils.image import imresize
 8
 9
10try:
11    xrange
12except NameError:
13    xrange = range
14
15def getPlots(motionData):
16    motionMagnitude = np.sqrt(np.sum(motionData**2, axis=2))
17
18    fig = plt.figure()
19    plt.quiver(motionData[::-1, :, 0], motionData[::-1, :, 1])
20    fig.axes[0].get_xaxis().set_visible(False)
21    fig.axes[0].get_yaxis().set_visible(False)
22    plt.tight_layout()
23    fig.canvas.draw()
24
25    # Get the RGBA buffer from the figure
26    w,h = fig.canvas.get_width_height()
27    buf = np.fromstring(fig.canvas.tostring_argb(), dtype=np.uint8)
28    buf.shape = (h, w, 4)
29    quiver = buf[:, :, 1:]
30    plt.close()
31
32    fig = plt.figure()
33    plt.imshow(motionMagnitude, cmap="Greys_r")
34    fig.axes[0].get_xaxis().set_visible(False)
35    fig.axes[0].get_yaxis().set_visible(False)
36    plt.tight_layout()
37    fig.canvas.draw()
38
39    w,h = fig.canvas.get_width_height()
40    buf = np.fromstring(fig.canvas.tostring_argb(), dtype=np.uint8)
41    buf.shape = (h, w, 4)
42    magnitude = buf[:, :, 1:]
43    plt.close()
44
45    # histogram it
46    fig = plt.figure()
47    hist, bins = np.histogram(motionMagnitude, bins=10, range=(-0.5, 9.5))
48    center = (bins[1:] + bins[:-1])/2.0
49    plt.scatter(center, hist)
50    plt.xlabel("Motion magnitude")
51    plt.ylabel("Count")
52    plt.ylim([0, 14000])
53    plt.grid()
54    plt.tight_layout()
55    fig.canvas.draw()
56
57    w,h = fig.canvas.get_width_height()
58    buf = np.fromstring(fig.canvas.tostring_argb(), dtype=np.uint8)
59    buf.shape = (h, w, 4)
60    histogram = buf[:, :, 1:]
61    plt.close()
62
63    return quiver, magnitude, histogram
64
65
66filename = skvideo.datasets.bigbuckbunny()
67
68videodata = skvideo.io.vread(filename)
69
70videometadata = skvideo.io.ffprobe(filename)
71frame_rate = videometadata['video']['@avg_frame_rate']
72
73T, M, N, C = videodata.shape
74
75motionData = skvideo.motion.blockMotion(videodata)
76
77writer = skvideo.io.FFmpegWriter("motion.mp4", inputdict={
78    "-r": frame_rate
79})
80
81for i in xrange(T-1):
82    a, b, c = getPlots(motionData[i])
83    frame = imresize(videodata[i + 1], (a.shape[0], a.shape[1]))
84    outputframe = np.zeros((frame.shape[0]*2, frame.shape[1]*2, 3), dtype=np.uint8)
85
86    outputframe[:frame.shape[0], :frame.shape[1]] = frame
87    outputframe[frame.shape[0]:, :frame.shape[1]] = a
88    outputframe[:frame.shape[0], frame.shape[1]:] = b
89    outputframe[frame.shape[0]:, frame.shape[1]:] = c
90
91    writer.writeFrame(outputframe)
92
93writer.close()

The video output of the above code: