Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to crop OpenCV Image from center

How can I crop an image using cv2 from the center?

I think it has something to do with this line, but if there is a better way please inform me.

crop_img = img[y:y+h, x:x+w]
like image 538
Juicestus Avatar asked May 21 '20 05:05

Juicestus


1 Answers

Just an additional comment for the Lenik's answer (It is the first time I want to contribute in StackOverflow and don't have enough reputation to comment the answer), you need to be sure x and y are integers.

Probably in this case x and y would always be integers as most of resolutions are even, but is a good practice to keep the values inside an int().

center = image.shape / 2
x = center[1] - w/2
y = center[0] - h/2

crop_img = img[int(y):int(y+h), int(x):int(x+w)]

like image 98
Juan Esteban Fonseca Avatar answered Sep 18 '22 23:09

Juan Esteban Fonseca