crop_or_pad.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import cv2
  2. import numpy as np
  3. def crop_or_pad(image, x_min, y_min, x_max, y_max, pad_val=None):
  4. """
  5. References:
  6. tf.image.resize_image_with_crop_or_pad
  7. """
  8. assert image.ndim in [2, 3]
  9. assert isinstance(x_min, int) and isinstance(y_min, int)
  10. assert isinstance(x_max, int) and isinstance(y_max, int)
  11. assert (x_min <= x_max) and (y_min <= y_max)
  12. src_height, src_width = image.shape[:2]
  13. dst_height, dst_width = y_max - y_min + 1, x_max - x_min + 1
  14. channels = 1 if image.ndim == 2 else image.shape[2]
  15. if pad_val is not None:
  16. if isinstance(pad_val, (int, float)):
  17. pad_val = [pad_val for _ in range(channels)]
  18. assert len(pad_val) == channels
  19. src_x_begin = max(x_min, 0)
  20. src_y_begin = max(y_min, 0, )
  21. src_x_end = min(x_max + 1, src_width)
  22. src_y_end = min(y_max + 1, src_height)
  23. dst_x_begin = src_x_begin - x_min
  24. dst_y_begin = src_y_begin - y_min
  25. dst_x_end = src_x_end - x_min
  26. dst_y_end = src_y_end - y_min
  27. if image.ndim == 2:
  28. dst_image_shape = (dst_height, dst_width)
  29. else:
  30. dst_image_shape = (dst_height, dst_width, channels)
  31. if pad_val is None:
  32. dst_image = np.zeros(dst_image_shape, image.dtype)
  33. else:
  34. dst_image = np.full(dst_image_shape, pad_val, dtype=image.dtype)
  35. dst_image[dst_y_begin: dst_y_end, dst_x_begin: dst_x_end, ...] = \
  36. image[src_y_begin: src_y_end, src_x_begin: src_x_end, ...]
  37. return dst_image
  38. def crop_or_pad_coords(boxes, image_width, image_height):
  39. """
  40. References:
  41. `mmcv.impad`
  42. `pad` in https://github.com/kpzhang93/MTCNN_face_detection_alignment
  43. `MtcnnDetector.pad` in https://github.com/AITTSMD/MTCNN-Tensorflow
  44. """
  45. x_mins = boxes[:, 0]
  46. y_mins = boxes[:, 1]
  47. x_maxs = boxes[:, 2]
  48. y_maxs = boxes[:, 3]
  49. src_x_begin = np.maximum(x_mins, 0)
  50. src_y_begin = np.maximum(y_mins, 0)
  51. src_x_end = np.minimum(x_maxs + 1, image_width)
  52. src_y_end = np.minimum(y_maxs + 1, image_height)
  53. dst_x_begin = src_x_begin - x_mins
  54. dst_y_begin = src_y_begin - y_mins
  55. dst_x_end = src_x_end - x_mins
  56. dst_y_end = src_y_end - y_mins
  57. coords = np.stack([src_x_begin, src_y_begin, src_x_end, src_y_end,
  58. dst_x_begin, dst_y_begin, dst_x_end, dst_y_end], axis=1)
  59. return coords