KalelPark's LAB

[CODE] Masking imaging 코드 구현 본문

Data Science/CODE

[CODE] Masking imaging 코드 구현

kalelpark 2023. 3. 29. 20:05

import torch
from PIL import Image
import numpy as np
from torchvision.transforms import transforms
tf = transforms.ToPILImage()

# Load image
image = Image.open("/content/img.jpeg")
image = np.array(image)     

tensor_image = torch.tensor(image)       
tensor_image = tensor_image.float()

mask = torch.zeros_like(tensor_image)   # Create binary mask
mask[50:300, 200:300, :] = 1.0 			# column, row
mask[50:300, 420:500, :] = 1.0

masked_image = tensor_image * mask		# Apply mask to image

masked_image = (masked_image * 255.0)
masked_image = masked_image.permute(2, 0, 1)
imgg = tf(masked_image)
imgg.show()							# Convert masked image back to PIL image and save

 
 
Comments