Segmenting image with a mask
With the binary mask ready, you can use it to segment the object, that is the cat, out of the image.
To do so, you will need to load the original image and transform it to a tensor. Next, you will create the object tensor by masking the original image. Finally, you will display the result.
transforms from torchvision have been imported, and the binary_mask you created in the previous exercise is available to you.
Questo esercizio fa parte del corso
Deep Learning for Images with PyTorch
Istruzioni dell'esercizio
- Create
image_tensorby applying theToTensor()transform, defined astransform, to the raw image. - Segment the image by masking the
image_tensorwith thebinary_mask, assigning the result toobject_tensor. - Apply the already defined
to_pil_imagetransform to theobject_tensorin order to display it.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Load image and transform to tensor
image = Image.open("images/Egyptian_Mau_123.jpg")
transform = transforms.Compose([transforms.ToTensor()])
image_tensor = ____
# Segment object out of the image
object_tensor = ____
# Convert segmented object to image and display
to_pil_image = ____
object_image = to_pil_image(object_tensor)
plt.imshow(object_image)
plt.show()