我想在一些单独的 (x,y) 点上使用相同的仿射矩阵 M,就像我在图像上使用 cv2.warpAffine 一样。似乎 cv2.transform 是要走的路。当我尝试发送一个 Nx2 点矩阵时,我被否定了 (
src = np.array([
[x1,y1],[x2,y2],[x3,y3],[x4,y4]], dtype = "float32")
print('source shape '+str(src.shape))
dst=cv2.transform(src,M)
cv2.error: /home/jeremy/sw/opencv-3.1.0/modules/core/src/matmul.cpp:1947: error: (-215) scn == m.cols || scn + 1 == m.cols in function transform
我可以使用 numpy 算术得到我想要的转换:
dst = np.dot(src,M[:,0:2]) +M[:,2]
print('dest:{}'.format(dst))
但想了解是怎么回事。文档说 cv2.transform 想要的 channel 数等于 M 中的列数,但我不清楚 channel 是什么——可能是“x” channel 和“y” channel ,但第三个 channel 会是,不同的行代表什么?
最佳答案
Python 上的 OpenCV 通常需要以下形式的点
np.array([ [[x1, y1]], ..., [[xn, yn]] ])
这在documentation for cv2.transform()
中并不清楚但在其他使用点的函数的文档中更清楚,比如 cv2.perspectiveTransform()
他们提到坐标在不同的 channel 上:
src – input two-channel or three-channel floating-point array
变换也可以用于 3D(使用 4x4
透视变换矩阵),这样就可以解释在 中使用两个 或 三 channel 数组的能力>cv2.transform()
.
关于python - 无法让 cv.transform 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44378098/