【问题标题】:OpenCV cv2 perspective transformation matrix multiplicationOpenCV cv2 透视变换矩阵乘法
【发布时间】:2013-10-27 02:12:21
【问题描述】:

我试图通过组合 getPerspectiveTransform 生成的矩阵将一系列 warpPerspective 组合成一个。如果我使用 cv2.multiply 将它们相乘,则生成的矩阵不起作用。仅两个转换的示例:

src = np.array([[0,0],[0,480],[640,480],[640,0]],np.float32)
dst = np.array([[-97,-718],[230,472],[421,472],[927,-717]],np.float32) 

retval = cv2.getPerspectiveTransform(src, dst);
test = cv2.multiply(retval.copy(),retval.copy())

img1 = cv2.warpPerspective(img1,test,(640,480))

img2 = cv2.warpPerspective(img2,retval,(640,480))
img2 = cv2.warpPerspective(img2,retval,(640,480))

为什么 img1 和 img2 不一样? 如何组合透视变换矩阵?

谢谢

【问题讨论】:

  • 您不能乘以 3x3 矩阵。您必须将矩阵设为 4x4: [x,x,x,0] [x,x,x,0] [x,x,x,0] [0,0,0,1] 然后乘以然后返回到一个 3x3 矩阵。注意 numpy 选择各个部分的格式,使用 m.item。不要使用 np.resize,它会弄乱矩阵。

标签: python opencv


【解决方案1】:

你误解了 cv2.multiply() 的目的。它用于图像相乘并逐点相乘,因此如果 A = cv2.multiply(B,C) 那么 ai,j = bi,j * ci,j 对于所有 i,j。

要进行正确的矩阵乘法,您需要使用强大但复杂的 cv2.gemm() 或使用生成的转换是 numpy 数组这一事实并使用内置的 dot() 函数

import numpy as np  
import cv2

# test images
img1 = np.zeros((600,600,3),np.uint8)
img1[:] = (255,255,255)
cv2.fillConvexPoly( img1,np.array([(250,50),(350,50),(350,550),(250,550)],np.int32), (0,0,255) )
img2 = img1.copy()

# source and destination coordinates
src = np.array([[0,0],[0,480],[640,480],[640,0]],np.float32)
dst = np.array([[-97,-718],[230,472],[421,472],[927,-717]],np.float32) 
# transformation matrix
retval = cv2.getPerspectiveTransform(src, dst);

# test1 is wrong, test2 is the application of the transform twice
test1 = cv2.multiply(retval.copy(),retval.copy())
test2 = cv2.gemm(retval,retval,1,None,0) 
# test3 is using matrix-multiplication using numpy
test3 = retval.dot(retval)

img2 = cv2.warpPerspective(img1,test2,(640,480))
img3 = cv2.warpPerspective(img1,test3,(640,480))

img4 = cv2.warpPerspective(img1,retval,(640,480))
img4 = cv2.warpPerspective(img4,retval,(640,480))

cv2.imshow( "one application of doubled transform", img2 )
cv2.imshow( "one applications using numpy", img3 )
cv2.imshow( "two applications of single transform", img4 )
cv2.waitKey()

注意 cv2 转换是从 left 开始的,所以如果你想应用 A 然后是 B 你必须应用 B.dot(A) 作为组合。

【讨论】:

    猜你喜欢
    • 2013-01-25
    • 2011-03-09
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    相关资源
    最近更新 更多