問題描述
我有一組不同尺寸的圖片(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
教程
這篇關于向圖像添加填充以使它們具有相同的形狀的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!