简体   繁体   English

给定一个向量场 (dx, dy),将位置 (Row, Col) 处的矩阵值移动到新位置 (Row + dx, Column + dy)

[英]Given a vector field (dx, dy), move matrix value at position (Row, Col) to new position (Row + dx, Column + dy)

Given a matrix给定一个矩阵

[   a   b   -  ]
[   -   e   f  ]
[   g   h   -  ]

where, for the sake of demonstration, - denotes a zero entry.其中,为了演示起见, - 表示零条目。

We also work with a vector field我们还使用向量场

[   (0,1)   (0,1)   (0,0)  ]
[   (0,0)  (0,-1)  (0,-1)  ]
[   (0,1)   (0,1)   (0,0)  ]

where each tuple specifies how many (rows, columns) to move the corresponding element in the matrix.其中每个元组指定移动矩阵中相应元素的数量(行、列)。 What is a Pythonic/efficient way to move each element by its corresponding vector to achieve:什么是通过其相应的向量移动每个元素以实现的 Pythonic/有效方法:

[   -   a   b  ]
[   e   f   -  ]
[   -   g   h  ]

This was inspired by a coregistration problem, but I haven't found an elegant solution to this problem besides looping through element wise.这是受一个核心注册问题的启发,但除了明智地循环遍历之外,我还没有找到解决这个问题的优雅方法。 I'm new to image processing, and also programming in Python - what is an efficient/accepted way to do this?我是图像处理的新手,也是 Python 编程的新手 - 什么是有效/可接受的方法来做到这一点?

This can be done using np.add.at :这可以使用np.add.at来完成:

A = np.array([["a","b",""],["","c","d"],["e","f",""]])
l,n,r = [[0,-1],[0,0],[0,1]]
B = np.array([[r,r,n],[n,l,l],[r,r,n]])

out = np.zeros_like(A)
i,j = np.ogrid[:3,:3]
np.add.at(out.view('u4'),(i+B[...,0],j+B[...,1]),A.view('u4'))

out
# array([['', 'a', 'b'],
#        ['c', 'd', ''],
#        ['', 'e', 'f']], dtype='<U1')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM