1

I'm working on sign language recognition (hand gestures) using OpenCV. I want to select a particular region (ROI) as input from the entire frame and give it to the model. Right now, I'm able to do the same with the entire frame but can't have ROI. This is the code I've tried so far :

_, 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)

Any help ?

3
  • Please do not post Python code as Javascript snippets (edited this time)
    – desertnaut
    Commented Mar 7, 2019 at 13:24
  • You want user to select corresponding ROI with mouse or you simply want a rectangular area from image programmatically?
    – unlut
    Commented Mar 7, 2019 at 13:51
  • I want to select from the video. Commented Mar 7, 2019 at 14:17

1 Answer 1

1

If I understand you correctly you simply want to cut a region from the image right? This should work:

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]

Remember that cv2 images are simply numpy arrays. Cutting a region of the image is the same as extracting the indexes from the array.

2
  • Thanks but I want to do it in the video input. Just want to select the specific region instead of entire video. Commented Mar 7, 2019 at 14:15
  • Yes, you would have to pass every frame from the camera (the image_frame variable) through this function. I don't use a camera myself but I'm sure it works.
    – Martin S
    Commented Mar 7, 2019 at 14:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.