背景
在水滴检测中,目前的解决方案是选择一个检测区域,只检测这个区域内,这样可以规避其他区域的影响。一开始是手动确定要检测的区域,计算出bbox后,只输出存在与这个区域内的bbox。后来经提醒,cv2.selectROI可以在屏幕上手动画出一个bbox,返回它的(minx, miny, w, h)
cv2.selectROI的使用
cv2.selectROI可以让用户框出感兴趣的区域,以便对这个区域进行截取和后续处理。
使用:
if num == 1:
r = cv2.selectROI(frame, False, False)
print(r)
# (998, 587, 130, 92)
crop = frame[r[1]:r[1]+r[3], r[0]:r[0]+r[2]]
cv2.imshow('cropped', crop)
cv2.selectROI输出选定区域的bbox:(minx, miny, w, h)。即左上角坐标和w、h。
得到选定区域的(minx, miny, w, h)后,就可以从原图中截取出这一段图片。由于图片是numpy array,直接切片即可。
参数解读
opencv文档:https://docs.opencv.org/3.4.14/d7/dfc/group__highgui.html#ga8daf4730d3adf7035b6de9be4c469af5
Python:
retval = cv.selectROI( windowName, img[, showCrosshair[, fromCenter]] )
retval = cv.selectROI( img[, showCrosshair[, fromCenter]]
Parameters
windowName name of the window where selection process will be shown.
img image to select a ROI.
showCrosshair if true crosshair of selection rectangle will be shown.
fromCenter if true center of selection will match initial mouse position. In opposite case a corner of selection rectangle will correspont to the initial mouse position.
Returns
selected ROI or empty rect if selection canceled.
后两个默认False不用修改,一般传入图片进去即可。输出选定区域的bbox信息:(minx, miny, w, h)
效果展示