如何在OpenCV(Python)中选择视频输入中的ROI?
我正在使用OpenCV进行手语识别(手势)。我想从整个帧中选择一个特定区域(ROI)作为输入,并将其提供给模型。现在,我可以对整个帧执行相同的操作,但无法获得ROI。这是我迄今为止尝试过的代码:如何在OpenCV(Python)中选择视频输入中的ROI?,python,opencv,keras,Python,Opencv,Keras,我正在使用OpenCV进行手语识别(手势)。我想从整个帧中选择一个特定区域(ROI)作为输入,并将其提供给模型。现在,我可以对整个帧执行相同的操作,但无法获得ROI。这是我迄今为止尝试过的代码: _, image_frame = cam_capture.read() r = cv2.rectangle(image_frame, upper_left, bottom_right, (100, 50, 200), 5) rect_img = image_frame[upper_left[1] :
_, image_frame = cam_capture.read()
r = cv2.rectangle(image_frame, upper_left, bottom_right, (100, 50, 200), 5)
rect_img = image_frame[upper_left[1] : bottom_right[1], upper_left[0] : bottom_right[0]]
sketcher_rect = rect_img
sketcher_rect = sketch_transform(sketcher_rect)
cv2.resize(sketcher_rect, (28,28), interpolation = cv2.INTER_AREA)
pred_probab, pred_class = keras_predict(model, sketcher_rect)
print(pred_class, pred_probab)
cv2.imshow('image_frame',sketcher_rect)
有什么帮助吗?如果我理解正确,您只需要从图像中剪切一个区域,对吗? 这应该起作用:
def crop_image(image, x, y, width, height):
"""
image: a cv2 frame
x, y, width, height: the region to cut out
"""
return image[y:y + height, x:x + width]
请记住,cv2图像只是numpy阵列。剪切图像区域与从数组中提取索引相同。请不要将Python代码作为Javascript片段发布(这次编辑)您想让用户用鼠标选择相应的ROI,还是只想通过编程从图像中选择一个矩形区域?我想从视频中进行选择。谢谢,但我想在视频输入中进行选择。只需选择特定区域,而不是整个视频。是的,您必须通过此函数传递相机的每一帧(image\u frame变量)。我自己不用照相机,但我肯定它能用。