2 import matplotlib.pyplot as plt
3 from matplotlib.collections import LineCollection
4 from matplotlib import colors as mcolors
9 def draw_mv_ls(axis, mv_ls, mode=0):
10 colors = np.array([(1., 0., 0., 1.)])
12 np.array([[ptr[0], ptr[1]], [ptr[0] + ptr[2], ptr[1] + ptr[3]]])
15 line_segments = LineCollection(
16 segs, linewidths=(1.,), colors=colors, linestyle='solid')
17 axis.add_collection(line_segments)
19 axis.scatter(mv_ls[:, 0], mv_ls[:, 1], s=2, c='b')
22 mv_ls[:, 0] + mv_ls[:, 2], mv_ls[:, 1] + mv_ls[:, 3], s=2, c='b')
25 def draw_pred_block_ls(axis, mv_ls, bs, mode=0):
26 colors = np.array([(0., 0., 0., 1.)])
35 x_ls = [x, x + bs, x + bs, x, x]
36 y_ls = [y, y, y + bs, y + bs, y]
38 segs.append(np.column_stack([x_ls, y_ls]))
39 line_segments = LineCollection(
40 segs, linewidths=(.5,), colors=colors, linestyle='solid')
41 axis.add_collection(line_segments)
44 def read_frame(fp, no_swap=0):
45 plane = [None, None, None]
48 word_ls = line.split()
49 word_ls = [int(item) for item in word_ls]
54 word_ls = line.split()
55 word_ls = [int(item) for item in word_ls]
57 plane[i] = np.array(word_ls).reshape(rows, cols)
59 plane[i] = plane[i].repeat(2, axis=0).repeat(2, axis=1)
60 plane = np.array(plane)
62 plane = np.swapaxes(np.swapaxes(plane, 0, 1), 1, 2)
68 # [1.164, 0 , 1.596 ],
69 # [1.164, -0.391, -0.813],
70 # [1.164, 2.018 , 0 ] ]
72 #c = np.array([[ -16 , -16 , -16 ],
76 mat = np.array([[1, 0, 1.4075], [1, -0.3445, -0.7169], [1, 1.7790, 0]])
77 c = np.array([[0, 0, 0], [0, -128, -128], [-128, -128, 0]])
78 mat_c = np.dot(mat, c)
79 v = np.array([mat_c[0, 0], mat_c[1, 1], mat_c[2, 2]])
81 rgb = np.dot(yuv, mat) + v
83 rgb = rgb.clip(0, 255)
87 def read_feature_score(fp, mv_rows, mv_cols):
89 word_ls = line.split()
90 feature_score = np.array([math.log(float(v) + 1, 2) for v in word_ls])
91 feature_score = feature_score.reshape(mv_rows, mv_cols)
94 def read_mv_mode_arr(fp, mv_rows, mv_cols):
96 word_ls = line.split()
97 mv_mode_arr = np.array([int(v) for v in word_ls])
98 mv_mode_arr = mv_mode_arr.reshape(mv_rows, mv_cols)
102 def read_frame_dpl_stats(fp):
104 word_ls = line.split()
105 frame_idx = int(word_ls[1])
106 mi_rows = int(word_ls[3])
107 mi_cols = int(word_ls[5])
109 ref_frame_idx = int(word_ls[9])
110 rf_idx = int(word_ls[11])
111 gf_frame_offset = int(word_ls[13])
112 ref_gf_frame_offset = int(word_ls[15])
115 mv_rows = int((math.ceil(mi_rows * 1. / mi_size)))
116 mv_cols = int((math.ceil(mi_cols * 1. / mi_size)))
117 for i in range(mv_rows * mv_cols):
119 word_ls = line.split()
120 row = int(word_ls[0]) * 8.
121 col = int(word_ls[1]) * 8.
122 mv_row = int(word_ls[2]) / 8.
123 mv_col = int(word_ls[3]) / 8.
124 mv_ls.append([col, row, mv_col, mv_row])
125 mv_ls = np.array(mv_ls)
126 feature_score = read_feature_score(fp, mv_rows, mv_cols)
127 mv_mode_arr = read_mv_mode_arr(fp, mv_rows, mv_cols)
128 img = yuv_to_rgb(read_frame(fp))
129 ref = yuv_to_rgb(read_frame(fp))
130 return rf_idx, frame_idx, ref_frame_idx, gf_frame_offset, ref_gf_frame_offset, mv_ls, img, ref, bs, feature_score, mv_mode_arr
133 def read_dpl_stats_file(filename, frame_num=0):
141 data_ls.append(read_frame_dpl_stats(fp))
143 if frame_num > 0 and len(data_ls) == frame_num:
148 if __name__ == '__main__':
149 filename = sys.argv[1]
150 data_ls = read_dpl_stats_file(filename, frame_num=5)
151 for rf_idx, frame_idx, ref_frame_idx, gf_frame_offset, ref_gf_frame_offset, mv_ls, img, ref, bs, feature_score, mv_mode_arr in data_ls:
152 fig, axes = plt.subplots(2, 2)
154 axes[0][0].imshow(img)
155 draw_mv_ls(axes[0][0], mv_ls)
156 draw_pred_block_ls(axes[0][0], mv_ls, bs, mode=0)
157 #axes[0].grid(color='k', linestyle='-')
158 axes[0][0].set_ylim(img.shape[0], 0)
159 axes[0][0].set_xlim(0, img.shape[1])
162 axes[0][1].imshow(ref)
163 draw_mv_ls(axes[0][1], mv_ls, mode=1)
164 draw_pred_block_ls(axes[0][1], mv_ls, bs, mode=1)
165 #axes[1].grid(color='k', linestyle='-')
166 axes[0][1].set_ylim(ref.shape[0], 0)
167 axes[0][1].set_xlim(0, ref.shape[1])
169 axes[1][0].imshow(feature_score)
170 #feature_score_arr = feature_score.flatten()
171 #feature_score_max = feature_score_arr.max()
172 #feature_score_min = feature_score_arr.min()
173 #step = (feature_score_max - feature_score_min) / 20.
174 #feature_score_bins = np.arange(feature_score_min, feature_score_max, step)
175 #axes[1][1].hist(feature_score_arr, bins=feature_score_bins)
176 im = axes[1][1].imshow(mv_mode_arr)
177 #axes[1][1].figure.colorbar(im, ax=axes[1][1])
179 print rf_idx, frame_idx, ref_frame_idx, gf_frame_offset, ref_gf_frame_offset, len(mv_ls)