Created
March 10, 2023 19:23
-
-
Save gsoykan/369df298de35ecd9ec8253e28cd4ddbf to your computer and use it in GitHub Desktop.
pt tensor to cv2 image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
import torch | |
# Create a random torch tensor | |
tensor = torch.randn(3, 256, 256) | |
# Convert the tensor to a numpy array | |
numpy_image = tensor.numpy() | |
# Convert the numpy array to a cv2 image | |
cv2_image = np.transpose(numpy_image, (1, 2, 0)) | |
cv2_image = cv2.cvtColor(cv2_image, cv2.COLOR_BGR2RGB) | |
# Display the image using cv2 | |
cv2.imshow("Image", cv2_image) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment