在我通过相机校准生成基本和基本矩阵之后,我正在尝试获取极线,并在我的图像中绘制它们以测试我生成的矩阵,遵循 python-opencv tutorial
这是实现绘制极线功能的代码:
def drawlines(img1,img2,lines,pts1,pts2):
''' img1 - image on which we draw the epilines for the points in img2
lines - corresponding epilines
'''
r,c = img1.shape
img1 = cv2.cvtColor(img1,cv2.COLOR_GRAY2BGR)
img2 = cv2.cvtColor(img2,cv2.COLOR_GRAY2BGR)
for r,pt1,pt2 in zip(lines,pts1,pts2):
color = tuple(np.random.randint(0,255,3).tolist())
x0,y0 = map(int, [0, -r[2]/r[1] ])
x1,y1 = map(int, [c, -(r[2]+r[0]*c)/r[1] ])
img1 = cv2.line(img1, (x0,y0), (x1,y1), color,1)
img1 = cv2.circle(img1,tuple(pt1),5,color,-1)
img2 = cv2.circle(img2,tuple(pt2),5,color,-1)
return img1,img2
但是当我运行以下代码来生成核线时,我收到了这个错误:
Traceback (most recent call last): File "FundMat.py", line 124,
in img5,img6 = drawlines(img1,img2,lines1,pts1,pts2) File "FundMat.py", line 21, in drawlines img1 = cv2.circle(img1,tuple(pt1),5,color,-1)
TypeError: function takes exactly 2 arguments (1 given)
那么,为什么会出现此错误以及如何解决?
最佳答案
您的积分格式与 OpenCV 想要的格式不同。您的 pt1
和 pt2
可能看起来像 np.array([[x, y]])
,但是看看当你转换它时元组的样子:
>>> pt1 = np.array([[50, 50]])
>>> tuple(pt1)
(array([50, 50]),)
这是一个具有单个元素的元组,而不是您期望的两个项目。显然,对于绘图,它需要一个长度为 2 的元组(即
x
和 y
坐标)。因此错误;内部函数需要两个参数,但元组中只有一个值。展示:>>> pt1 = np.array([[50, 50]])
>>> cv2.circle(img, tuple(pt1), 5, 255, -1)
Traceback (most recent call last): File "...", line 10, in <module> cv2.circle(img, tuple(pt1), 5, 255, -1) TypeError: function takes exactly 2 arguments (1 given)
相反,这是您希望元组看起来的样子:
>>> tuple(pt1[0])
(50, 50)
假设您希望能够处理以任何一种格式传递的点,只需先 reshape 该点。无论哪种方式,
pt1
中只有两个值或 pt2
数组,所以 reshape 不会影响它。例如你可以使用 flatten()
来自 numpy:>>> tuple(pt1.flatten())
(50, 50)
这应该可以解决您的问题。
>>> pt1 = np.array([[50, 50]])
>>> cv2.circle(img, tuple(pt1.flatten()), 5, 255, -1)
>>>
关于python - 无法使用 opencv 和 python 在相机校准和 3D 重建中绘制极线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47429580/