如何在PyTorch中對影像進行全方位填充?
為了對影像進行全方位填充,我們可以使用**torchvision.transforms**模組提供的**Pad()**變換。此模組包含許多重要的變換,可用於對影像資料執行不同型別的操作。
**Pad()**變換接受PIL影像和張量影像或一批張量影像。張量影像是一個形狀為**[C, H, W]**的torch張量,其中C是通道數,H是影像高度,W是影像寬度。
一批張量影像也是一個形狀為**[B, C, H, W]**的torch張量。B是批次中的影像數量。如果影像既不是PIL影像也不是張量影像,則我們首先將其轉換為張量影像,然後應用變換。
語法
torchvision.transforms.Pad(padding)(img)
引數
Padding –所需的填充大小。**padding**是一個類似於**(l, t, r, b)**的序列,其中l、r、t和b分別是左、上、右和下**padding**大小。填充可以是長度為2的序列。在這種情況下,左填充和右填充相同,上填充和下填充也相同。如果**padding**是整數,則所有側面的填充都相同。
它返回一個用給定填充大小填充的影像。
步驟
我們可以使用以下步驟對影像進行全方位填充:
匯入所需的庫。在以下所有示例中,所需的Python庫是**torch、Pillow**和**torchvision**。確保您已安裝它們。
import torch import torchvision import torchvision.transforms as transforms from PIL import Image
讀取輸入影像。輸入影像是PIL影像或torch張量。
img = Image.open('dove.jpg')定義一個變換來對影像進行全方位填充。根據您的需要更改填充大小。
# padding same for all sides transform = transforms.Pad(50) # to pad 50 -> left/right, 100-> top/bottom transform = transforms.Pad((50,100)) # to pad 0->left, 50->top, 100-> right, 150-> bottom transform = transforms.Pad((0,50,100,150))
將上述定義的變換應用於輸入影像,以對影像進行全方位填充。
img = transform(img)
視覺化填充後的影像
img.show()
輸入影像
以下影像是所有示例中使用的輸入影像。

示例1
# Python program to pad an image on all sides
# import required libraries
import torch
import torchvision.transforms as transforms
from PIL import Image
# Read the image
img = Image.open('dove.jpg')
# compute width and height of image
width, height = img.size
# define a transform to pad an image on all sides
transform = transforms.Pad(50)
# apply the above transform on the image
img = transform(img)
# resize the image to its original dimension
img = img.resize((width, height))
# dispaly the image
img.show()輸出
它將產生以下輸出:
.jpg)
請注意,輸出影像中的填充在所有側面上都是均勻的。
示例2
# Python program to pad an image on all sides
# import required libraries
import torch
import torchvision.transforms as transforms
from PIL import Image
# Read the image
img = Image.open('dove.jpg')
width, height = img.size
# define a transform to pad an image
transform = transforms.Pad([50,100])
# here 50 -> left/right, 100-> top/bottom
# apply the above transforms on the image
img = transform(img)
# resize the image to its original dimension
img = img.resize((width, height))
# dispaly the image
img.show()輸出
它將產生以下輸出:
.jpg)
請注意,左右填充相同。同樣,頂部和底部的填充也是均勻的。
示例3
# Python program to pad an image on all sides
# import required libraries
import torch
import torchvision.transforms as transforms
from PIL import Image
# Read the image
img = Image.open('dove.jpg')
width, height = img.size
# define a transform to pad an image
transform = transforms.Pad([0,50,100,150])
# here 0->left, 50->top, 100-> right, 150-> bottom
# apply the above transforms on the image
img = transform(img)
# resize the image to its original dimension
img = img.resize((width, height))
# display the image
img.show()它將產生以下輸出:
.jpg)
請注意,輸出影像中的填充在各個側面是不同的。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP