python - 将变换矩阵应用于 opencv (Python) 中的点列表

标签 python opencv

我没看懂the documentation for cv2.transform .请有人怜悯并解释一下(用 Python)?

我有绘制多边形、填充它并旋转图像的代码

import numpy as np  
import cv2

dx,dy = 400,400
centre = dx//2,dy//2
img = np.zeros((dy,dx),np.uint8)

# construct a long thin triangle with the apex at the centre of the image
polygon = np.array([(0,0),(100,10),(100,-10)],np.int32)
polygon += np.int32(centre)

# draw the filled-in polygon and then rotate the image
cv2.fillConvexPoly(img,polygon,(255))
M = cv2.getRotationMatrix2D(centre,20,1) # M.shape =  (2, 3)
rotatedimage = cv2.warpAffine(img,M,img.shape)

我想先旋转多边形再画

# this is clumsy, I have to extend each vector,
# apply the matrix to each extended vector,
# and turn the list of transformed vectors into an numpy array
extendedpolygon = np.hstack([polygon,np.ones((3,1))])
rotatedpolygon = np.int32([M.dot(x) for x in extendedpolygon])
cv2.fillConvexPoly(img,rotatedpolygon,(127))

我想在一个函数调用中完成,但我做不对

# both of these calls produce the same error
cv2.transform(polygon,M)
cv2.transform(polygon.T,M)
# OpenCV Error: Assertion failed (scn == m.cols || scn + 1 == m.cols) in transform, file /home/david/opencv/modules/core/src/matmul.cpp, line 1947

我怀疑是短语“src – 输入数组必须具有与 m.cols 或 m.cols-1 一样多的 channel (1 到 4)”让我费解。我认为 polygon.T 作为三个坐标,x 和 y 部分作为单独的 channel 是合格的,但遗憾的是不是。

我知道有人对 C++ 提出了同样的问题,我想用 Python 回答。

最佳答案

感谢 Micka 向我指出了一个可行的示例。这是提供我的问题答案的代码。

import numpy as np  
import cv2

dx,dy = 400,400
centre = dx//2,dy//2
img = np.zeros((dy,dx),np.uint8)

# construct a long thin triangle with the apex at the centre of the image
polygon = np.array([(0,0),(100,10),(100,-10)],np.int32)
polygon += np.int32(centre)

# draw the filled-in polygon and then rotate the image
cv2.fillConvexPoly(img,polygon,(255))
M = cv2.getRotationMatrix2D(centre,20,1) # M.shape =  (2, 3)
rotatedimage = cv2.warpAffine(img,M,img.shape)

# as an alternative, rotate the polygon first and then draw it

# these are alternative ways of coding the working example
# polygon.shape is 3,2

# magic that makes sense if one understands numpy arrays
poly1 = np.reshape(polygon,(3,1,2))
# slightly more accurate code that doesn't assumy the polygon is a triangle
poly2 = np.reshape(polygon,(polygon.shape[0],1,2))
# turn each point into an array of points
poly3 = np.array([[p] for p in polygon])
# use an array of array of points 
poly4 = np.array([polygon])
# more magic 
poly5 = np.reshape(polygon,(1,3,2))

for poly in (poly1,poly2,poly3,poly4,poly5):
    newimg = np.zeros((dy,dx),np.uint8)
    rotatedpolygon = cv2.transform(poly,M)
    cv2.fillConvexPoly(newimg,rotatedpolygon,(127))

老实说,我不明白为什么 cv2.fillConvexPoly() 接受前三个答案,但它似乎很宽容,我仍然不明白这些答案如何映射到文档。

关于python - 将变换矩阵应用于 opencv (Python) 中的点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43157092/

相关文章:

python - 从 Python 中的时区感知日期时间对象获取 UTC 时间戳

python - OpenCV 错误 : Assertion failed (size. 宽度>0 && size.height>0) python

python - 如何使用 openCV 使用广角镜头正确校准我的相机?

c++ - Xamarin.Forms + native 库 OpenCV

python - 更多维度的广播函数中的 Numpy ValueError

python - 如何在不重新加载页面的情况下刷新 Selenium Webdriver DOM 数据?

python - 为什么要安装包而不是仅链接到特定环境?

python - 在 Python 中将 5A2B4C11G 字符串转换为 [(5 ,"A"),(2 ,"B"),(4 ,"C"),(11 ,"G")]

python - 检测帧特定区域中的对象

python - 当矩形与另一个矩形重叠时,OpenCV 连接轮廓