কম্পিউটার

কিভাবে PyTorch ইমেজ চ্যানেল জুড়ে মানে খুঁজে পেতে?


RGB ইমেজে তিনটি চ্যানেল আছে, লাল, সবুজ এবং নীল। আমাদের এই ইমেজ চ্যানেল জুড়ে ইমেজ পিক্সেল মানের গড় গণনা করতে হবে। এই উদ্দেশ্যে, আমরা torch.mean() পদ্ধতি ব্যবহার করি . কিন্তু এই পদ্ধতিতে ইনপুট পরামিতি হল একটি PyTorch টেনসর। সুতরাং, আমরা প্রথমে চিত্রটিকে PyTorch টেনসরে রূপান্তর করি এবং তারপর এই পদ্ধতিটি প্রয়োগ করি। এটি টেনসরের সমস্ত উপাদানের গড় মান প্রদান করে। ছবি চ্যানেল জুড়ে গড় খুঁজে পেতে, আমরা প্যারামিটার সেট করি dim =[1,2] .

পদক্ষেপ

  • প্রয়োজনীয় লাইব্রেরি আমদানি করুন। নিম্নলিখিত সমস্ত পাইথন উদাহরণে, প্রয়োজনীয় পাইথন লাইব্রেরি হল টর্চ, টর্চভিশন, বালিশ এবং 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" ছবির পিক্সেলের।

ইনপুট ছবি

আমরা উভয় উদাহরণে ইনপুট হিসাবে নিম্নলিখিত চিত্রটি ব্যবহার করব।

কিভাবে PyTorch ইমেজ চ্যানেল জুড়ে মানে খুঁজে পেতে?

উদাহরণ 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)

  1. কিভাবে Tkinter এ উইন্ডোর আকারে ব্যাকগ্রাউন্ড ইমেজ রিসাইজ করবেন?

  2. পাইথনে স্কিট-লার্ন ব্যবহার করে একটি চিত্রের কনট্যুরগুলি কীভাবে খুঁজে পাবেন?

  3. আপনার নেটওয়ার্কের জন্য কীভাবে সেরা Wi-Fi চ্যানেল খুঁজে পাবেন

  4. Windows 11 PC এ IP ঠিকানা কীভাবে খুঁজে পাবেন