选择图像上的边界框并进行注释

我正在一个项目中,我想要一个已经在主题上绘制的边界框并选择它(通过鼠标单击),以便可以在图像上方悬停一个类似文本对话框的内容,这样我就可以然后输入标签。我已经在使用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)

请多多指教。谢谢。

pacheco 回答:选择图像上的边界框并进行注释

您可以将鼠标的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
本文链接:https://www.f2er.com/3142979.html

大家都在问