Most of the function in OpenCV require src1 and src2 must be equal size. Does anyone know if I got two image file with different dimension, how to make the size equally?
how to make two different image size equally
17k views Asked by 望熙貴 At
4
There are 4 answers
0
On
Of course you can crop: https://stackoverflow.com/a/8268062/3076403 or resize the images: How to resize an image to a specific size in OpenCV?
But do you really need to do this? Please specify, what you want to achieve?
Most of the OpenCV functions processes one image to another doing a spatial pixel-to-pixel operations (e.g. filtering, morphological operations). So that it produces the output image in the same size as original and this is a desired behavior.
0
On
2020 update: This library handles it really nicely, including expanding and cropping if needed: https://pypi.org/project/image-tools/
from PIL import Image
from image_tools.sizes import resize_and_crop
img_target_size = Image.open('img1.png')
img_to_resize = resize_and_crop(
'img2.png',
(img_origin_size.size[0],img_origin_size.size[1]), #set width and height to match img1
crop_origin="middle"
)
output = img_to_resize.save('resized_img2.png')
You can scale them to match, typically by making the larger image smaller. The issue becomes that they have to have the same aspect ratio, and if they don't you'll have to crop the image without croping out the important part.
Scaling an image in opencv: