问题描述
我有一组不同尺寸的图片(45,50,3), (69,34,3), (34,98,3).我想为这些图像添加填充,如下所示:
l have a set of images of different sizes (45,50,3), (69,34,3), (34,98,3). l want to add padding to these images as follows:
取整张图片的最大宽度和长度,然后把图片放在那个尺寸
Take the max width and length of the whole images then put the image in that size
import os import glob import cv2 input_path="/home/images" os.chdir(indput_path) images=glob.glob("*.png") Length=[] Width=[] for img in images: img=cv2.imread(img) width,length=img.shape[0:2] Length.append(length) Width.append(width) W=max(Width) L=max(Length)
如何在 opencv 中添加填充以使所有图像具有相同的大小?在示例中 l 给出的图像将得到 (69,98,3)
How can l add padding in opencv so that all the images will have the same size? In the example l gave the images will get the shape of (69,98,3)
推荐答案
你可以使用:
image = cv2.copyMakeBorder(src, top, bottom, left, right, borderType)
src 是你的源图片,top、bottom、left、right 是图像周围的填充.
Where src is your source image and top, bottom, left, right are the padding around the image.
您可以在 while 循环中使用 max(sizes) - 图像的大小值来为每个图像添加填充.边框类型可以是以下之一:
You can use max(sizes) - size value of the image in a while loop to add the padding to each image. The bordertype can be one of these:
- cv2.BORDER_CONSTANT
- cv2.BORDER_REFLECT
- cv2.BORDER_REFLECT_101
- cv2.BORDER_DEFAULT
- cv2.BORDER_REPLICATE
- cv2.BORDER_WRAP
cv2.copyMakeBorder 教程