def zz(matrix, nb):
r"""Zig-zag traversal of the input matrix
:param matrix: input matrix
:param nb: number of coefficients to keep
:return: an array of nb coefficients
"""
flipped = np.fliplr(matrix)
rows, cols = flipped.shape # nb of columns
coefficient_list = []
for loop, i in enumerate(range(cols - 1, -rows, -1)):
anti_diagonal = np.diagonal(flipped, i)
# reversing even diagonals prioritizes the X resolution
# reversing odd diagonals prioritizes the Y resolution
# for square matrices, the information content is the same only when nb covers half of the matrix
# e.g. [ nb = n*(n+1)/2 ]
if loop % 2 == 0:
anti_diagonal = anti_diagonal[::-1] # reverse anti_diagonal
coefficient_list.extend([x for x in anti_diagonal])
# flattened = [val for sublist in coefficient_list for val in sublist]
return coefficient_list[:nb]
评论列表
文章目录