我有一组不同尺寸的图片(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_CONSTANTcv2.BORDER_REFLECTcv2.BORDER_REFLECT_101cv2.BORDER_DEFAULTcv2.BORDER_REPLICATEcv2.BORDER_WRAPcv2.copyMakeBorder 教程
这篇关于向图像添加填充以使它们具有相同的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在python中的感兴趣区域周围绘制一个矩形How to draw a rectangle around a region of interest in python(如何在python中的感兴趣区域周围绘制一个矩形)
如何使用 OpenCV 检测和跟踪人员?How can I detect and track people using OpenCV?(如何使用 OpenCV 检测和跟踪人员?)
如何在图像的多个矩形边界框中应用阈值?How to apply threshold within multiple rectangular bounding boxes in an image?(如何在图像的多个矩形边界框中应用阈值?)
如何下载 Coco Dataset 的特定部分?How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
根据文本方向检测图像方向角度Detect image orientation angle based on text direction(根据文本方向检测图像方向角度)
使用 Opencv 检测图像中矩形的中心和角度Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 检测图像中矩形的中心和角度)