要将OpenCV的Mat灰度图像转换为PyTorch张量,可以按照以下步骤进行操作:
import cv2
image = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)
import cv2
import numpy as np
image = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
numpy_array = np.asarray(gray_image)
import cv2
import numpy as np
import torch
image = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
numpy_array = np.asarray(gray_image)
tensor = torch.from_numpy(numpy_array)
通过以上步骤,你可以将OpenCV的Mat灰度图像成功转换为PyTorch张量。
领取专属 10元无门槛券
手把手带您无忧上云