我收到TypeError:尝试在裁剪的窗口中绘制圆时需要一个整数(获得元组类型)

时间:2019-04-24 19:15:32

标签: python opencv

我浏览了其他帖子,似乎没什么能解决我的问题。到目前为止,我可以使用我的mouse_draw_rect函数在视频流上绘制一个矩形并创建一个裁剪的窗口。然后,我想在此裁剪的窗口中进一步绘制形状,但是当我尝试调用select_point函数时,程序崩溃了,并且得到了以下内容:TypeError: an integer is required (got type tuple)。错误发生在代码cv2.circle(cropped_window,point,(0,0,255),2)的行上。

这是我的以下代码,可以使用opencv来运行:

import cv2

cap = cv2.VideoCapture(0)
_, first_frame = cap.read()


def mouse_draw_rect(event, x, y, flags, params):
    global point1, point2, drawing, first_frame
    if event == cv2.EVENT_LBUTTONDOWN:
        if drawing is False:
            drawing = True
            point1 = ()
            point2 = ()
            point1 = (x, y)
            print(point1)
            print(point2)
        else:
            drawing = False

    elif event == cv2.EVENT_LBUTTONUP:
        if drawing is True:
            point2 = (x, y)
            drawing = False

def select_point(event, x, y, flags, params):
    global point, point_selected
    if event == cv2.EVENT_MBUTTONDOWN:
        point = (x, y)
        point_selected = True
        print(point)

drawing = False
point_selected = False

point1 = ()
point2 = ()
point = ()

cv2.namedWindow('Original')

cv2.setMouseCallback("Original", mouse_draw_rect)

while True:

    ret, frame = cap.read( )

    if point1 and point2:
        cv2.rectangle(frame, point1, point2, (0, 0, 0), 2)
        first_frame_roi = first_frame[point1[1]:point2[1], point1[0]:point2[0]]
        cropped_window = frame[point1[1]:point2[1], point1[0]:point2[0], :]

        cv2.imshow('Cropped Window', cropped_window)

        cv2.namedWindow('Cropped Window')
        cv2.setMouseCallback("Cropped Window", select_point)
        if point_selected is True:
            cv2.circle(cropped_window,point,(0,0,255),2)

    cv2.imshow('Original', frame)


    key = cv2.waitKey(30) & 0xff
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()

如果您需要更多详细信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

根据文档:

  

cv2.circle(img,中心,半径,颜色[,厚度[,lineType [,shift]]])→img

第三个参数是半径,该半径应为数字(在这种情况下为整数)而不是元组。我认为您在通话中使用了错误的参数。