简体   繁体   English

Select 图像上的边界框和注释

[英]Select a bounding box on an image and annotate

I'm working on an project where I would like to take a bounding box already drawn on a subject and select it (via mouse click) so I can have something like a text dialogue box hover above the image, so I can then type in a label.我正在做一个项目,我想在一个主题上画一个边界框和 select 它(通过鼠标单击),所以我可以在图像上方有一个文本对话框 hover 之类的东西,然后我可以输入一个 label。 I'm already using OpenCV to detect the object and draw an initial bounding box on it using the Haar Cascade classifier, but so far I can't find the right combination of OpenCV directives to be able to select that bounding box and then annotate it. I'm already using OpenCV to detect the object and draw an initial bounding box on it using the Haar Cascade classifier, but so far I can't find the right combination of OpenCV directives to be able to select that bounding box and then annotate it . The relevant code is below.相关代码如下。

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)

Would appreciate some good pointers.将不胜感激一些好的指针。 Thanks.谢谢。

You can take the x/y position of the mouse and compare that to the bounding boxes.您可以将鼠标的 x/y position 与边界框进行比较。 The code below descibes how you could do that.下面的代码描述了如何做到这一点。

First, to be able to process mouse input, you have to crate a namedWindow.首先,为了能够处理鼠标输入,您必须创建一个 namedWindow。 You can then attach a mouseCallback to that window:然后,您可以将 mouseCallback 附加到该 window:

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

In the getFace method, you check for button press, then loop through the faces and check if the x/y of the mouse is within the bounds of the bounding box of a face.在 getFace 方法中,您检查按钮是否按下,然后循环遍历面并检查鼠标的 x/y 是否在面的边界框的范围内。 If so, return the index of the face.如果是,则返回人脸的索引。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM