I've been referred to How to crop an image in OpenCV using Python, but my question has a little difference.
This can be a numpy-slicing problem. I know I can do:
import cv2
img = cv2.imread("test_image.jpg")
crop_img = img[y:y+h, x:x+w]
But what if I need two rectangles with the same y range but non-consecutive x ranges of the original picture? I've tried:
crop_img = img[y:y+h, [x1:x1+w1, x2:x2+w2]]
What I expected was a rectangular having its height from y
to y+h
, and its width from x1
to x1+w1
plus x2
to x2+w2
, where x1+w1
doesn't have to be equal to x2
. Nevertheless I get a SyntaxError
with "invalid syntax". Is there a way to correctly achieve my goal?