【问题标题】:Select a bounding box on an image and annotate选择图像上的边界框并进行注释
【发布时间】:2019-11-07 16:46:04
【问题描述】:

我正在做一个项目,我想获取一个已经在主题上绘制的边界框并选择它(通过鼠标单击),这样我就可以在图像上方悬停一个文本对话框之类的东西,这样我就可以然后输入标签。我已经在使用 OpenCV 来检测对象并使用 Haar Cascade 分类器在其上绘制初始边界框,但到目前为止,我找不到能够选择该边界框然后对其进行注释的 OpenCV 指令的正确组合.相关代码如下。

faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
)

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

希望得到一些好的指点。谢谢。

【问题讨论】:

  • 是的。我正在使用 Haar Cascade 分类器来检测人脸,并且我已经从函数中获得了坐标。

标签: python opencv


【解决方案1】:

您可以获取鼠标的 x/y 位置并将其与边界框进行比较。 下面的代码描述了如何做到这一点。

首先,为了能够处理鼠标输入,您必须创建一个命名窗口。然后,您可以将 mouseCallback 附加到该窗口:

# create window
cv2.namedWindow("Frame") 
# attach a callback to the window, that calls 'getFace'
cv2.setMouseCallback("Frame", getFace) 

在 getFace 方法中,您检查按钮是否按下,然后遍历面并检查鼠标的 x/y 是否在面的边界框的范围内。如果是,则返回人脸的索引。

def getFace(event, x,y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
                # if mousepressed
                for i in range(len(faces)): 
                        # loop through faces
                        (face_x,face_y,w,h) = faces[i]
                        # unpack variables
                        if x > face_x and x < face_x + w:
                                # if x is within x-range of face
                                if y > face_y and y < face_y + h:
                                        # if y is also in y-range of face
                                        return i
                                        # then return the index of the face

【讨论】:

    猜你喜欢
    • 2020-04-02
    • 1970-01-01
    • 2021-08-30
    • 2020-08-12
    • 2021-04-16
    • 2020-11-23
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多