Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. def colorize_channels(image_tensor):
  2. """
  3. Colorizes each channel of an image tensor.
  4.  
  5. Outputs numpy array that can be easily displayed using opencv.
  6.  
  7. Parameters
  8. ----------
  9. image_tensor : numpy.ndarray, required
  10. (height, width, channels)
  11.  
  12. Raises
  13. ------
  14. AssertionError
  15. Raised if image tensor contains wrong number of dimensions.
  16. """
  17. assert(len(image_tensor.shape) == 3)
  18.  
  19. # initialize image
  20. image_shape = list(image_tensor[:,:,0].shape)
  21. image_shape.append(3)
  22. image = np.zeros(image_shape)
  23.  
  24. for channel_num in range(len(image_tensor[0,0])):
  25. random_color = list(np.random.choice(range(256), size=3))
  26. channel = image_tensor[:,:,channel_num]
  27.  
  28. # iterate through channel values
  29. for idx in np.argwhere(channel > 0.5):
  30. image[idx[0], idx[1]] = random_color
  31.  
  32. return image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement