如何从一个裁剪的numpy矩形中定义顶部和左侧的点?

2024-04-24 15:29:15 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,在下面的代码中,我可以从第一帧裁剪出一个矩形的ROI。 while循环的最终结果提供了一个存储为numpy数组的ROI,名为“monitor\u region”

video = cv2.VideoCapture("Rob.mp4")       
ret, frame = video.read()
roi_status = False

while(roi_status == False):
    roi = cv2.selectROI("Region Selection by ROI", frame, False)
    if(not all(roi)):
        print("Undefined monitor region.")
        continue
    monitor_region = frame[int(roi[1]):int(roi[1]+roi[3]),int(roi[0]):int(roi[0]+roi[2])]
    cv2.imshow("Selected Region", monitor_region)
    if(cv2.waitKey(0) & 0xFF == 8): #backspace to save
        print("Monitor region has been saved.")
        roi_status = True
cv2.destroyAllWindows()

因为这个“监视区域”是整个帧的一部分,也是一个矩形。因此,我正在寻找一个可行的解决方案来定义它的左侧和顶部点,以便定义一个检查范围(如图所示)。在下面的代码中,我能够定义roi的宽度和高度

monitor_width = monitor_region.shape[1]
monitor_height = monitor_region.shape[0]

enter image description here

然而,我仍然缺乏左上角的得分。一旦我获得了顶部和左侧的ROI点。我可以执行x和y点检查,如下所示,我用它来确定对象是否存在于ROI中

if((monitor_left < Point_x < (monitor_left + monitor_width)) and (monitor_top < Point_y < (monitor_top + monitor_height))):

Tags: 代码falseif定义videostatuscv2frame
1条回答
网友
1楼 · 发布于 2024-04-24 15:29:15

selectROI返回一个正好包含您所搜索的值的元组:左边界、上边界、所选ROI的宽度和高度

如果你print(roi),你会得到类似(100,150,300,200)

您可以像这样解压值:

x,y,width,height = roi

其中x是左边界,y是上边界

相关问题 更多 >