RGB 이미지에는 Red, Green 및 Blue의 세 가지 채널이 있습니다. 이러한 이미지 채널에서 이미지 픽셀 값의 평균을 계산해야 합니다. 이를 위해 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)