【问题标题】:Can't draw the epipolar lines in camera calibration and 3D- reconstruction using opencv & python无法使用 opencv 和 python 在相机校准和 3D 重建中绘制极线
【发布时间】:2017-11-22 08:04:58
【问题描述】:

在我通过相机校准生成基本矩阵和基本矩阵后,我正在尝试获取极线,并将它们绘制在我的图像中以测试我生成的矩阵,遵循 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

但是当我运行以下代码生成核线时,我得到了这个错误:

回溯(最近一次调用最后一次):文件“FundMat.py”,第 124 行,
在 img5,img6 = drawlines(img1,img2,lines1,pts1,pts2) 文件 “FundMat.py”,第 21 行,画线 img1 = cv2.circle(img1,tuple(pt1),5,color,-1)
TypeError:函数只需要 2 个参数(给定 1 个)

那么,为什么会出现这个错误以及如何解决呢?

【问题讨论】:

  • 您只包含了 OpenCV 的特定于版本的标签,而不是一般的 opencv 标签,这意味着您的问题在深渊中迷失了。 Stack 上的回答者订阅了某些标签,因此他们可以轻松找到问题,而特定版本的问题则不那么受欢迎。如果您认为问题可能与版本有关,请随意使用特定于版本的标签进行标记,但请始终包含一般标签以进行曝光,以便更多人真正看到您的问题。这就是为什么没有人回答这个问题,尽管这是一个简单的解决方法。

标签: python opencv computer-vision camera-calibration opencv3.1


【解决方案1】:

您的分数与 OpenCV 想要的格式不同。你的pt1pt2 可能看起来像np.array([[x, y]]),但是看看你投射它时元组的样子:

>>> pt1 = np.array([[50, 50]])
>>> tuple(pt1)
(array([50, 50]),)

这是一个具有单个元素的元组,而不是您期望的两个项目。显然,对于绘图,它需要一个长度为 2 的元组(即 xy 坐标)。因此错误;内部函数需要 两个 参数,但您的元组中只有一个值。演示:

>>> 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)

假设您希望能够处理以任何一种格式传递的点,只需先重塑该点。无论哪种方式,pt1pt2 数组中都只有两个值,因此整形不会影响它。例如你可以使用 numpy 中的flatten()

>>> tuple(pt1.flatten())
(50, 50)

这应该可以解决您的问题。

>>> pt1 = np.array([[50, 50]])
>>> cv2.circle(img, tuple(pt1.flatten()), 5, 255, -1)
>>> 

【讨论】:

    猜你喜欢
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2014-09-11
    • 2020-06-12
    • 2012-02-10
    相关资源
    最近更新 更多