【发布时间】:2017-09-28 15:46:42
【问题描述】:
尝试在 OpenCV 中对单应矩阵进行前向扭曲。不过,您不必知道这意味着什么就能理解这个问题。
假设有 2 个图像(一个图像是像素值的 2D Numpy 数组),A 和 B,以及一个数组 match,看起来像
[[ 6.96122642e+01 -1.06556338e+03 1.02251944e+00]
[ 6.92265938e+01 -1.06334423e+03 1.02246589e+00]
[ 6.88409234e+01 -1.06112508e+03 1.02241234e+00]
... ]
第一列为 X,第二列为 Y,第三列为标量。这些 XY 值是图像 A 的像素索引,对应于图像 B 的索引
[[0,0],
[0,1],
[0,2]
... ]
我想使用此信息从 imageA 快速设置 imageB 值。我有这个工作,但它没有我想要的那么快
yAs = np.int32(np.round( match[:, 0] / match[:, 2] )
xAs = np.int32(np.round( match[:, 1] / match[:, 2] )
it = np.nditer(pixelsImageB[0], flags=['f_index'])
while not it.finished:
i = it.index
xA = xAs[i]
yA = yAs[i]
if in_bounds(xA, yA, imageA):
yB = pixB[0][i]
xB = pixB[1][i]
imageB[xB,yB] = imageA[xA,yA]
it.iternext()
但我不确定如何在 Numpy 中快速实现这一点,天真地执行此循环非常慢。我是高级索引、广播等方面的专家。有什么想法吗?
【问题讨论】:
-
x和y看起来像是退出循环的第一批候选人。例如x = np.int32(np.round(match[:,0] / match[:,2])) -
in_bounds是什么? -
x 或 y >0 并且
-
in_bounds看起来像一个函数。愿意分享源代码吗? -
in_bounds = x >= 0 and y >=0 and x < imageA.width and y < imageA.height
标签: python arrays opencv numpy array-broadcasting