向图像添加填充以使其具有相同的形状 [英] Add padding to images to get them into the same shape

查看:55
本文介绍了向图像添加填充以使其具有相同的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

l有一组不同大小的图像(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是您的源图像,顶部,底部,左侧,右侧是图像周围的填充.

where src is your source image and top, botto, left, right are paddings around the image.

您可以在while循环中使用max(sizes)-图片的大小值将填充添加到每个图片中. bordertype可以是以下类型之一:

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

copyMakeBorder教程

这篇关于向图像添加填充以使其具有相同的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆