在图像上选择一个边界框并加注。

3

我正在开发一个项目,想要在已经在目标物体上绘制了边界框的情况下(通过鼠标点击)选择它,这样我就可以在图像上方悬浮一个文本对话框,然后输入标签。 我已经使用OpenCV检测对象并使用Haar级联分类器在其上绘制了初始边界框,但到目前为止,我找不到正确的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分类器来检测人脸,并且我已经从函数中得到了坐标。 - Azmat
1个回答

0
你可以获取鼠标的x/y位置并将其与边界框进行比较。下面的代码描述了如何实现这一点。
首先,为了能够处理鼠标输入,您必须创建一个namedWindow。然后,您可以将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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接