non_greedy_mv.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import sys
  2. import matplotlib.pyplot as plt
  3. from matplotlib.collections import LineCollection
  4. from matplotlib import colors as mcolors
  5. import numpy as np
  6. import math
  7. def draw_mv_ls(axis, mv_ls, mode=0):
  8. colors = np.array([(1., 0., 0., 1.)])
  9. segs = np.array([
  10. np.array([[ptr[0], ptr[1]], [ptr[0] + ptr[2], ptr[1] + ptr[3]]])
  11. for ptr in mv_ls
  12. ])
  13. line_segments = LineCollection(
  14. segs, linewidths=(1.,), colors=colors, linestyle='solid')
  15. axis.add_collection(line_segments)
  16. if mode == 0:
  17. axis.scatter(mv_ls[:, 0], mv_ls[:, 1], s=2, c='b')
  18. else:
  19. axis.scatter(
  20. mv_ls[:, 0] + mv_ls[:, 2], mv_ls[:, 1] + mv_ls[:, 3], s=2, c='b')
  21. def draw_pred_block_ls(axis, mv_ls, bs, mode=0):
  22. colors = np.array([(0., 0., 0., 1.)])
  23. segs = []
  24. for ptr in mv_ls:
  25. if mode == 0:
  26. x = ptr[0]
  27. y = ptr[1]
  28. else:
  29. x = ptr[0] + ptr[2]
  30. y = ptr[1] + ptr[3]
  31. x_ls = [x, x + bs, x + bs, x, x]
  32. y_ls = [y, y, y + bs, y + bs, y]
  33. segs.append(np.column_stack([x_ls, y_ls]))
  34. line_segments = LineCollection(
  35. segs, linewidths=(.5,), colors=colors, linestyle='solid')
  36. axis.add_collection(line_segments)
  37. def read_frame(fp, no_swap=0):
  38. plane = [None, None, None]
  39. for i in range(3):
  40. line = fp.readline()
  41. word_ls = line.split()
  42. word_ls = [int(item) for item in word_ls]
  43. rows = word_ls[0]
  44. cols = word_ls[1]
  45. line = fp.readline()
  46. word_ls = line.split()
  47. word_ls = [int(item) for item in word_ls]
  48. plane[i] = np.array(word_ls).reshape(rows, cols)
  49. if i > 0:
  50. plane[i] = plane[i].repeat(2, axis=0).repeat(2, axis=1)
  51. plane = np.array(plane)
  52. if no_swap == 0:
  53. plane = np.swapaxes(np.swapaxes(plane, 0, 1), 1, 2)
  54. return plane
  55. def yuv_to_rgb(yuv):
  56. #mat = np.array([
  57. # [1.164, 0 , 1.596 ],
  58. # [1.164, -0.391, -0.813],
  59. # [1.164, 2.018 , 0 ] ]
  60. # )
  61. #c = np.array([[ -16 , -16 , -16 ],
  62. # [ 0 , -128, -128 ],
  63. # [ -128, -128, 0 ]])
  64. mat = np.array([[1, 0, 1.4075], [1, -0.3445, -0.7169], [1, 1.7790, 0]])
  65. c = np.array([[0, 0, 0], [0, -128, -128], [-128, -128, 0]])
  66. mat_c = np.dot(mat, c)
  67. v = np.array([mat_c[0, 0], mat_c[1, 1], mat_c[2, 2]])
  68. mat = mat.transpose()
  69. rgb = np.dot(yuv, mat) + v
  70. rgb = rgb.astype(int)
  71. rgb = rgb.clip(0, 255)
  72. return rgb / 255.
  73. def read_feature_score(fp, mv_rows, mv_cols):
  74. line = fp.readline()
  75. word_ls = line.split()
  76. feature_score = np.array([math.log(float(v) + 1, 2) for v in word_ls])
  77. feature_score = feature_score.reshape(mv_rows, mv_cols)
  78. return feature_score
  79. def read_mv_mode_arr(fp, mv_rows, mv_cols):
  80. line = fp.readline()
  81. word_ls = line.split()
  82. mv_mode_arr = np.array([int(v) for v in word_ls])
  83. mv_mode_arr = mv_mode_arr.reshape(mv_rows, mv_cols)
  84. return mv_mode_arr
  85. def read_frame_dpl_stats(fp):
  86. line = fp.readline()
  87. word_ls = line.split()
  88. frame_idx = int(word_ls[1])
  89. mi_rows = int(word_ls[3])
  90. mi_cols = int(word_ls[5])
  91. bs = int(word_ls[7])
  92. ref_frame_idx = int(word_ls[9])
  93. rf_idx = int(word_ls[11])
  94. gf_frame_offset = int(word_ls[13])
  95. ref_gf_frame_offset = int(word_ls[15])
  96. mi_size = bs / 8
  97. mv_ls = []
  98. mv_rows = int((math.ceil(mi_rows * 1. / mi_size)))
  99. mv_cols = int((math.ceil(mi_cols * 1. / mi_size)))
  100. for i in range(mv_rows * mv_cols):
  101. line = fp.readline()
  102. word_ls = line.split()
  103. row = int(word_ls[0]) * 8.
  104. col = int(word_ls[1]) * 8.
  105. mv_row = int(word_ls[2]) / 8.
  106. mv_col = int(word_ls[3]) / 8.
  107. mv_ls.append([col, row, mv_col, mv_row])
  108. mv_ls = np.array(mv_ls)
  109. feature_score = read_feature_score(fp, mv_rows, mv_cols)
  110. mv_mode_arr = read_mv_mode_arr(fp, mv_rows, mv_cols)
  111. img = yuv_to_rgb(read_frame(fp))
  112. ref = yuv_to_rgb(read_frame(fp))
  113. 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
  114. def read_dpl_stats_file(filename, frame_num=0):
  115. fp = open(filename)
  116. line = fp.readline()
  117. width = 0
  118. height = 0
  119. data_ls = []
  120. while (line):
  121. if line[0] == '=':
  122. data_ls.append(read_frame_dpl_stats(fp))
  123. line = fp.readline()
  124. if frame_num > 0 and len(data_ls) == frame_num:
  125. break
  126. return data_ls
  127. if __name__ == '__main__':
  128. filename = sys.argv[1]
  129. data_ls = read_dpl_stats_file(filename, frame_num=5)
  130. 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:
  131. fig, axes = plt.subplots(2, 2)
  132. axes[0][0].imshow(img)
  133. draw_mv_ls(axes[0][0], mv_ls)
  134. draw_pred_block_ls(axes[0][0], mv_ls, bs, mode=0)
  135. #axes[0].grid(color='k', linestyle='-')
  136. axes[0][0].set_ylim(img.shape[0], 0)
  137. axes[0][0].set_xlim(0, img.shape[1])
  138. if ref is not None:
  139. axes[0][1].imshow(ref)
  140. draw_mv_ls(axes[0][1], mv_ls, mode=1)
  141. draw_pred_block_ls(axes[0][1], mv_ls, bs, mode=1)
  142. #axes[1].grid(color='k', linestyle='-')
  143. axes[0][1].set_ylim(ref.shape[0], 0)
  144. axes[0][1].set_xlim(0, ref.shape[1])
  145. axes[1][0].imshow(feature_score)
  146. #feature_score_arr = feature_score.flatten()
  147. #feature_score_max = feature_score_arr.max()
  148. #feature_score_min = feature_score_arr.min()
  149. #step = (feature_score_max - feature_score_min) / 20.
  150. #feature_score_bins = np.arange(feature_score_min, feature_score_max, step)
  151. #axes[1][1].hist(feature_score_arr, bins=feature_score_bins)
  152. im = axes[1][1].imshow(mv_mode_arr)
  153. #axes[1][1].figure.colorbar(im, ax=axes[1][1])
  154. print rf_idx, frame_idx, ref_frame_idx, gf_frame_offset, ref_gf_frame_offset, len(mv_ls)
  155. flatten_mv_mode = mv_mode_arr.flatten()
  156. zero_mv_count = sum(flatten_mv_mode == 0);
  157. new_mv_count = sum(flatten_mv_mode == 1);
  158. ref_mv_count = sum(flatten_mv_mode == 2) + sum(flatten_mv_mode == 3);
  159. print zero_mv_count, new_mv_count, ref_mv_count
  160. plt.show()