misc.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_gray_image(image, tol=3):
  51. if image.ndim == 2:
  52. return True
  53. elif image.ndim == 3:
  54. num_channels = image.shape[-1]
  55. if num_channels == 1:
  56. return True
  57. elif num_channels == 4:
  58. rgb = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
  59. gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
  60. gray3 = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
  61. mae = np.mean(cv2.absdiff(rgb, gray3))
  62. return mae <= tol
  63. elif num_channels == 3:
  64. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  65. gray3 = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
  66. mae = np.mean(cv2.absdiff(image, gray3))
  67. return mae <= tol
  68. else:
  69. return False
  70. else:
  71. return False