我正在做一个项目,我想在一个主题上绘制一个边界框并选择它(通过鼠标单击),这样我就可以在图像上方悬停类似文本对话框的东西,这样我就可以然后输入标签。我已经在使用 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)
希望得到一些好的建议。谢谢。
最佳答案
您可以获取鼠标的 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
关于python - 选择图像上的边界框并进行注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58753442/