你误会cv2.multiply()的目的。它是乘以图像和确实指向逐点相乘,所以如果甲 = cv2.multiply(乙,Ç)然后 一个 I,J = B I,J * C 我,对于所有i,j,j 。
做一个适当的矩阵乘法,你需要或者使用功能强大,但复杂的cv2.gemm()或使用的事实,你生成的转换是一个numpy的数组,并使用内置的点()功能
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)作为组合。
您不能乘以3x3矩阵。您必须进行矩阵吉普车: [X,X,X,0] [X,X,X,0] [X,X,X,0] [0,0,0,1] 你然后相乘,然后设置回3x3矩阵。小心numpy的格式来选择各个部分,使用m.item。不要使用np.resize,它会混淆矩阵。 – user736358