Skip to content

Instantly share code, notes, and snippets.

@gsoykan
Created March 10, 2023 19:23
Show Gist options
  • Save gsoykan/369df298de35ecd9ec8253e28cd4ddbf to your computer and use it in GitHub Desktop.
Save gsoykan/369df298de35ecd9ec8253e28cd4ddbf to your computer and use it in GitHub Desktop.
pt tensor to cv2 image
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