您误解了cv2.multiply()的目的。它用于将图像相乘并进行逐点相乘,因此如果A = cv2.multiply( B , C ) 则 a i,j = b i,j * c i,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 转换从左侧开始,因此如果您想应用A然后B您必须应用B .dot( A ) 作为组合。