Complete the code to read an image using OpenCV.
import cv2 image = cv2.[1]('image.jpg')
The function cv2.imread is used to read an image file into an array.
Complete the code to convert a PIL image to a tensor using torchvision.
from torchvision import transforms transform = transforms.ToTensor() tensor_image = transform([1])
The ToTensor() transform converts a PIL Image or numpy.ndarray to a tensor. Here, the input must be a PIL image.
Fix the error in the code to convert an OpenCV image to RGB format.
import cv2 image_bgr = cv2.imread('image.jpg') image_rgb = cv2.[1](image_bgr, cv2.COLOR_BGR2RGB)
The correct OpenCV function to convert color spaces is cvtColor.
Fill both blanks to create a PIL image from a numpy array and save it.
from PIL import Image import numpy as np array = np.zeros((100, 100, 3), dtype=np.uint8) img = Image.[1](array) img.[2]('output.png')
Image.fromarray creates a PIL image from a numpy array. save saves the image to a file.
Fill all three blanks to normalize a tensor image using torchvision transforms.
from torchvision import transforms normalize = transforms.Normalize(mean=[[1]], std=[[2]]) transform = transforms.Compose([transforms.ToTensor(), normalize]) # Apply transform to PIL image normalized_tensor = transform(pil_image) print(normalized_tensor.mean().item() [3] 0)
The standard ImageNet mean and std values are used for normalization. After normalization, the mean of the tensor should be greater than 0.