使用opencv将多个perspectiveTransforms组合成一个转换

问题描述 投票:0回答:2

我有一个应用程序有两个透视变换从两个findHomography调用获得连续应用到一组点(python):

pts = np.float32([ [758,141],[769,141],[769,146],[758,146] ]).reshape(-1,1,2)
pts2 = cv2.perspectiveTransform(pts, trackingM)
dst = cv2.perspectiveTransform(pts2, updateM)

我想将它组合成一个单一的转换。我尝试过以下但转换不正确:

M = trackingM * updateM
dst = cv2.perspectiveTransform(pts, M)

如何将两个矩阵变换组合成一个变换?现在我在python中进行原型设计。除了python之外的C ++解决方案将是一个奖励。

python numpy opencv image-processing
2个回答
0
投票

numpynp.multiply(M, N)elementwise-product,而np.dot(M,N)dot-product。我认为,在你的情况下,你应该选择np.dot


例如:

>>> import numpy as np 
>>> x = np.array([[1,2],[3,4]])

## elementwise-product 
>>> x*x
array([[ 1,  4],
       [ 9, 16]])
>>> np.multiply(x,x)
array([[ 1,  4],
       [ 9, 16]])

## dot-product
>>> x.dot(x)
array([[ 7, 10],
       [15, 22]])
>>> np.dot(x,x)
array([[ 7, 10],
       [15, 22]])

0
投票

我在this post找到答案的线索。从本质上讲,我的原始代码是按照@Silencer指出的逐元素乘法。诀窍是在进行乘法之前将变换转换为矩阵:

M = np.matrix(updateM) * np.matrix(trackingM)
dst = cv2.perspectiveTransform(pts, M)

操作数的顺序很重要。可以认为上述内容是将updateM变换应用于trackingM变换以匹配原始问题中所述的顺序。

© www.soinside.com 2019 - 2024. All rights reserved.