A
A
Alexey Belov2019-05-15 05:40:34
Python
Alexey Belov, 2019-05-15 05:40:34

How to keep the crop from going beyond the image?

Hello everyone, there are x, y, h, w, with them I can crop images, they are always within the image, but I need to add 20% to each side, but sometimes this 20% is outside the image, and I get an error, how to do cropping so that it crops, but does not go beyond?

x, y, w, h = cv2.boundingRect(cnt)
portrait_image = image[y:y+h, x:x+w]

5cdb7c19bbee3048942443.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2019-05-15
@Alenorze

Shaw, pitonolov, did not learn to enclose?

center_y = y + h // 2
dh = max(min(h // 10, center_y - h * 3 // 5, image.height - center_y - h * 3 // 5), 0)
new_y, new_h = y - dh, h + 2 * dh

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question