misc.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import cv2
  2. import numpy as np
  3. from ..utils_numpy import minmax_normalize as _minmax_normalize
  4. def normalize_image_dtype(image, keep_num_channels=False):
  5. """Normalize image dtype to uint8 (usually for visualization).
  6. Args:
  7. image : ndarray
  8. Input image.
  9. keep_num_channels : bool, optional
  10. If this is set to True, the result is an array which has
  11. the same shape as input image, otherwise the result is
  12. an array whose channels number is 3.
  13. Returns:
  14. out: ndarray
  15. Image whose dtype is np.uint8.
  16. """
  17. assert (image.ndim == 3 and image.shape[-1] in [1, 3]) or (image.ndim == 2)
  18. image = image.astype(np.float32)
  19. image = _minmax_normalize(image, axis=None, copy=False)
  20. image = np.array(image * 255, dtype=np.uint8)
  21. if not keep_num_channels:
  22. if image.ndim == 2:
  23. image = np.expand_dims(image, -1)
  24. if image.shape[-1] == 1:
  25. image = np.tile(image, (1,1,3))
  26. return image
  27. def stack_image_list(image_list, dtype=np.float32):
  28. """Join a sequence of image along a new axis before first axis.
  29. References:
  30. `im_list_to_blob` in `py-faster-rcnn-master/lib/utils/blob.py`
  31. """
  32. assert isinstance(image_list, (tuple, list))
  33. max_dimension = np.array([image.ndim for image in image_list]).max()
  34. assert max_dimension in [2, 3]
  35. max_shape = np.array([image.shape[:2] for image in image_list]).max(axis=0)
  36. num_channels = []
  37. for image in image_list:
  38. if image.ndim == 2:
  39. num_channels.append(1)
  40. else:
  41. num_channels.append(image.shape[-1])
  42. assert len(set(num_channels) - set([1])) in [0, 1]
  43. max_num_channels = np.max(num_channels)
  44. blob = np.empty((len(image_list), max_shape[0], max_shape[1], max_num_channels), dtype=dtype)
  45. for k, image in enumerate(image_list):
  46. blob[k, :image.shape[0], :image.shape[1], :] = np.atleast_3d(image).astype(dtype, copy=False)
  47. if max_dimension == 2:
  48. blob = np.squeeze(blob, axis=-1)
  49. return blob
  50. def is_numpy_image(image):
  51. return isinstance(image, np.ndarray) and image.ndim in {2, 3}
  52. def is_gray_image(image, tol=3):
  53. assert is_numpy_image(image)
  54. if image.ndim == 2:
  55. return True
  56. elif image.ndim == 3:
  57. num_channels = image.shape[-1]
  58. if num_channels == 1:
  59. return True
  60. elif num_channels == 4:
  61. rgb = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
  62. gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
  63. gray3 = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
  64. mae = np.mean(cv2.absdiff(rgb, gray3))
  65. return mae <= tol
  66. elif num_channels == 3:
  67. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  68. gray3 = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
  69. mae = np.mean(cv2.absdiff(image, gray3))
  70. return mae <= tol
  71. else:
  72. return False
  73. else:
  74. return False