ภาพ RGB มีสามช่องสัญญาณ สีแดง สีเขียว และสีน้ำเงิน เราจำเป็นต้องคำนวณค่าเฉลี่ยของค่าพิกเซลของภาพในช่องสัญญาณภาพเหล่านี้ เพื่อจุดประสงค์นี้ เราใช้เมธอด torch.mean() . แต่พารามิเตอร์อินพุตสำหรับวิธีนี้คือเทนเซอร์ PyTorch ดังนั้นเราจึงแปลงรูปภาพเป็นเทนเซอร์ PyTorch ก่อน แล้วจึงใช้วิธีนี้ ส่งกลับค่ากลางขององค์ประกอบทั้งหมดในเทนเซอร์ ในการหาค่าเฉลี่ยในช่องต่างๆ ของรูปภาพ เราตั้งค่าพารามิเตอร์ dim =[1,2] .
ขั้นตอน
-
นำเข้าไลบรารีที่จำเป็น ในตัวอย่าง Python ต่อไปนี้ทั้งหมด ไลบรารี Python ที่จำเป็นคือ torch, torchvision, Pillow และ OpenCV . ตรวจสอบให้แน่ใจว่าคุณได้ติดตั้งแล้ว
-
อ่านภาพที่ป้อนโดยใช้ image.open() และกำหนดให้กับตัวแปร "img" .
-
กำหนดการแปลงเพื่อแปลงภาพ PIL เป็น PyTorch Tensor
-
แปลงรูปภาพ "img " ให้กับเมตริกซ์ PyTorch โดยใช้การแปลงที่กำหนดไว้ด้านบนและกำหนดเมตริกซ์นี้ให้กับ "imgTensor" .
-
คำนวณ torch.mean(imgTensor, dim =[1,2]) . ส่งกลับเทนเซอร์ของค่าสามค่า ค่าทั้งสามนี้เป็นค่ากลางสำหรับ RGB สามช่องสัญญาณ คุณสามารถกำหนดค่าค่าเฉลี่ยทั้งสามนี้แยกกันให้กับตัวแปรใหม่สามตัว "R_mean", "G_mean" , และ "B_mean" .
-
พิมพ์ค่ากลางสามค่า "R_mean", "G_mean", และ "B_mean" ของพิกเซลภาพ
ใส่รูปภาพ
เราจะใช้รูปภาพต่อไปนี้เป็นอินพุตในทั้งสองตัวอย่าง

ตัวอย่างที่ 1
# Python program to find mean across the image channels
# import necessary libraries
import torch
from PIL import Image
import torchvision.transforms as transforms
# Read the input image
img = Image.open('opera.jpg')
# Define transform to convert the image to PyTorch Tensor
transform = transforms.ToTensor()
# Convert image to PyTorch Tensor (Image Tensor)
imgTensor = transform(img)
print("Shape of Image Tensor:\n", imgTensor.shape)
# Compute mean of the Image Tensor across image channels RGB
R_mean, G_mean ,B_mean = torch.mean(imgTensor, dim = [1,2])
# print mean across image channel RGB
print("Mean across Read channel:", R_mean)
print("Mean across Green channel:", G_mean)
print("Mean across Blue channel:", B_mean) ผลลัพธ์
Shape of Image Tensor: torch.Size([3, 447, 640]) Mean across Read channel: tensor(0.1487) Mean across Green channel: tensor(0.1607) Mean across Blue channel: tensor(0.2521)
ตัวอย่างที่ 2
นอกจากนี้เรายังสามารถอ่านภาพโดยใช้ OpenCV . ภาพที่อ่านโดยใช้ OpenCV เป็นประเภท numpy.ndarray . ในตัวอย่างนี้ เราใช้วิธีอื่นในการคำนวณค่าเฉลี่ย เราใช้ imgTensor.mean() , การทำงานพื้นฐานบนเทนเซอร์ ดูตัวอย่างต่อไปนี้
# Python program to find mean across the image channels
# import necessary libraries
import torch
import cv2
import torchvision.transforms as transforms
# Read the input image either using cv2 or PIL
img = cv2.imread('opera.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Define transform to convert the image to PyTorch Tensor
transform = transforms.ToTensor()
# Convert image to PyTorch Tensor (Image Tensor)
imgTensor = transform(img)
print("Shape of Image Tensor:\n", imgTensor.shape)
# compute mean of the Image Tensor across image channels RGB
# The other way to compute the mean
R_mean, G_mean ,B_mean = imgTensor.mean(dim = [1,2])
# print mean across image channel RGB
print("Mean across Read channel:", R_mean)
print("Mean across Green channel:", G_mean)
print("Mean across Blue channel:", B_mean) ผลลัพธ์
Shape of Image Tensor: torch.Size([3, 447, 640]) Mean across Read channel: tensor(0.1487) Mean across Green channel: tensor(0.1607) Mean across Blue channel: tensor(0.2521)