Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract arbitrary rectangular patches from image in python

Is there a way to extract a rectangle of my choice from an image, maybe using numpy arrays? Most implementations available seem to be for regular sliding window solutions, but those always include steps, or rectangles of the same aspect ratio, or something like that.

Is it possible to provide the beginning x and y coordinates and the width and height (or ending x and y coordinates), and extract exactly that rectangle? Can this be done using numpy arrays alone? Or is there another way to do this?

like image 748
user961627 Avatar asked Aug 29 '26 19:08

user961627


1 Answers

The best way to do this would be slicing i.e.

rect = np.copy(img[y1:y1+height,x1:x1+width])

where (x1, y1) is the upper left corner of your rectangle.

like image 74
jrsm Avatar answered Sep 01 '26 14:09

jrsm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!