I have an image of size 1200 x 800
.
I want to extract a square patch from this image given a patch center 500, 450
and a patch scale 23%
of the smaller dimension of the image.
Is there any function in opencv
, skimage
or any library that allows me to do it in python
?
Using OpenCV python, you can do this:
import cv2
import numpy as np
# load the image
image = cv2.imread('path/to/your_image.jpg')
# define some values
patch_center = np.array([500, 450])
patch_scale = 0.23
# calc patch position and extract the patch
smaller_dim = np.min(image.shape[0:2])
patch_size = int(patch_scale * smaller_dim)
patch_x = int(patch_center[0] - patch_size / 2.)
patch_y = int(patch_center[1] - patch_size / 2.)
patch_image = image[patch_x:patch_x+patch_size, patch_y:patch_y+patch_size]
# show image and patch
cv2.imshow('image', image)
cv2.imshow('patch_image', patch_image)
cv2.waitKey()
Notice that this was done step-by-step just to be easier to understand. You can do many of these steps directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With