augment.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import numpy as np
  15. from .warp_mls import WarpMLS
  16. def tia_distort(src, segment=4):
  17. img_h, img_w = src.shape[:2]
  18. cut = img_w // segment
  19. thresh = cut // 3
  20. src_pts = list()
  21. dst_pts = list()
  22. src_pts.append([0, 0])
  23. src_pts.append([img_w, 0])
  24. src_pts.append([img_w, img_h])
  25. src_pts.append([0, img_h])
  26. dst_pts.append([np.random.randint(thresh), np.random.randint(thresh)])
  27. dst_pts.append(
  28. [img_w - np.random.randint(thresh), np.random.randint(thresh)])
  29. dst_pts.append(
  30. [img_w - np.random.randint(thresh), img_h - np.random.randint(thresh)])
  31. dst_pts.append(
  32. [np.random.randint(thresh), img_h - np.random.randint(thresh)])
  33. half_thresh = thresh * 0.5
  34. for cut_idx in np.arange(1, segment, 1):
  35. src_pts.append([cut * cut_idx, 0])
  36. src_pts.append([cut * cut_idx, img_h])
  37. dst_pts.append([
  38. cut * cut_idx + np.random.randint(thresh) - half_thresh,
  39. np.random.randint(thresh) - half_thresh
  40. ])
  41. dst_pts.append([
  42. cut * cut_idx + np.random.randint(thresh) - half_thresh,
  43. img_h + np.random.randint(thresh) - half_thresh
  44. ])
  45. trans = WarpMLS(src, src_pts, dst_pts, img_w, img_h)
  46. dst = trans.generate()
  47. return dst
  48. def tia_stretch(src, segment=4):
  49. img_h, img_w = src.shape[:2]
  50. cut = img_w // segment
  51. thresh = cut * 4 // 5
  52. src_pts = list()
  53. dst_pts = list()
  54. src_pts.append([0, 0])
  55. src_pts.append([img_w, 0])
  56. src_pts.append([img_w, img_h])
  57. src_pts.append([0, img_h])
  58. dst_pts.append([0, 0])
  59. dst_pts.append([img_w, 0])
  60. dst_pts.append([img_w, img_h])
  61. dst_pts.append([0, img_h])
  62. half_thresh = thresh * 0.5
  63. for cut_idx in np.arange(1, segment, 1):
  64. move = np.random.randint(thresh) - half_thresh
  65. src_pts.append([cut * cut_idx, 0])
  66. src_pts.append([cut * cut_idx, img_h])
  67. dst_pts.append([cut * cut_idx + move, 0])
  68. dst_pts.append([cut * cut_idx + move, img_h])
  69. trans = WarpMLS(src, src_pts, dst_pts, img_w, img_h)
  70. dst = trans.generate()
  71. return dst
  72. def tia_perspective(src):
  73. img_h, img_w = src.shape[:2]
  74. thresh = img_h // 2
  75. src_pts = list()
  76. dst_pts = list()
  77. src_pts.append([0, 0])
  78. src_pts.append([img_w, 0])
  79. src_pts.append([img_w, img_h])
  80. src_pts.append([0, img_h])
  81. dst_pts.append([0, np.random.randint(thresh)])
  82. dst_pts.append([img_w, np.random.randint(thresh)])
  83. dst_pts.append([img_w, img_h - np.random.randint(thresh)])
  84. dst_pts.append([0, img_h - np.random.randint(thresh)])
  85. trans = WarpMLS(src, src_pts, dst_pts, img_w, img_h)
  86. dst = trans.generate()
  87. return dst