Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีหาค่าเฉลี่ยข้ามช่องสีของภาพใน PyTorch

ภาพ RGB มีสามช่องสี คือ แดง เขียว และน้ำเงิน เราต้องคำนวณค่าเฉลี่ยของค่าพิกเซลภาพข้ามช่องสีเหล่านี้ สำหรับวัตถุประสงค์นี้ เราใช้เมธอด torch.mean() แตพารามิเตอร์อินพุตของเมธอดนี้คือ PyTorch tensor ดังนั้น เราต้องแปลงภาพเป็น PyTorch tensor ก่อน แล้วจึงใช้เมธอดนี้ มันคืนค่าค่าเฉลี่ยขององค์ประกอบทั้งหมดใน tensor เพื่อหาค่าเฉลี่ยข้ามช่องสีของภาพ เราตั้งพารามิเตอร์ dim = [1,2]

ขั้นตอน

  • นำเข้าไลบรารีที่จำเป็น ในตัวอย่าง Python ทั้งหมดต่อไปนี้ ไลบรารี Python ที่จำเป็นคือ torch, torchvision, Pillow และ OpenCV ตรวจสอบให้แน่ใจว่าคุณติดตั้งแล้ว

  • อ่านภาพอินพุตโดยใช้ image.open() และกำหนดให้ตัวแปร "img"

  • กำหนด transform เพื่อแปลงภาพ PIL เป็น PyTorch Tensor

  • แปลงภาพ "img" เป็น PyTorch tensor โดยใช้ transform ที่กำหนดข้างต้น และกำหนด tensor นี้ให้ "imgTensor"

  • คำนวณ torch.mean(imgTensor, dim = [1,2]) มันคืนค่า tensor สามค่า ค่าเหล่านี้คือค่าเฉลี่ยสำหรับสามช่องสี RGB คุณสามารถกำหนดค่าเฉลี่ยสามค่านี้แยกให้ตัวแปรใหม่สามตัว "R_mean", "G_mean" และ "B_mean"

  • พิมพ์ค่าเฉลี่ยสามค่า "R_mean", "G_mean" และ "B_mean" ของพิกเซลภาพ

ภาพอินพุต

เราจะใช้ภาพต่อไปนี้เป็นอินพุตในทั้งสองตัวอย่าง

วิธีหาค่าเฉลี่ยข้ามช่องสีของภาพใน PyTorch

ตัวอย่างที่ 1

# โปรแกรม Python หาค่าเฉลี่ยข้ามช่องสีของภาพ
# นำเข้าไลบรารีที่จำเป็น
import torch
from PIL import Image
import torchvision.transforms as transforms

# อ่านภาพอินพุต
img = Image.open('opera.jpg')

# กำหนด transform แปลงภาพเป็น PyTorch Tensor
transform = transforms.ToTensor()

# แปลงภาพเป็น PyTorch Tensor (Image Tensor)
imgTensor = transform(img)
print("Shape of Image Tensor:\n", imgTensor.shape)

# คำนวณค่าเฉลี่ยของ Image Tensor ข้ามช่องสี RGB
R_mean, G_mean, B_mean = torch.mean(imgTensor, dim=[1, 2])

# พิมพ์ค่าเฉลี่ยข้ามช่องสี RGB
print("Mean across Red 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 Red 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() การดำเนินการพื้นฐานบน tensor ดูตัวอย่างต่อไปนี้

# โปรแกรม Python หาค่าเฉลี่ยข้ามช่องสีของภาพ
# นำเข้าไลบรารีที่จำเป็น
import torch
import cv2
import torchvision.transforms as transforms

# อ่านภาพอินพุตโดยใช้ cv2 หรือ PIL
img = cv2.imread('opera.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# กำหนด transform แปลงภาพเป็น PyTorch Tensor
transform = transforms.ToTensor()

# แปลงภาพเป็น PyTorch Tensor (Image Tensor)
imgTensor = transform(img)
print("Shape of Image Tensor:\n", imgTensor.shape)

# คำนวณค่าเฉลี่ยของ Image Tensor ข้ามช่องสี RGB
# อีกวิธีหนึ่งในการคำนวณค่าเฉลี่ย
R_mean, G_mean, B_mean = imgTensor.mean(dim=[1, 2])

# พิมพ์ค่าเฉลี่ยข้ามช่องสี RGB
print("Mean across Red 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 Red channel: tensor(0.1487)
Mean across Green channel: tensor(0.1607)
Mean across Blue channel: tensor(0.2521)