imgproc_utils.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import numpy as np
  2. import cv2
  3. import random
  4. from typing import List
  5. def hex2bgr(hex):
  6. gmask = 254 << 8
  7. rmask = 254
  8. b = hex >> 16
  9. g = (hex & gmask) >> 8
  10. r = hex & rmask
  11. return np.stack([b, g, r]).transpose()
  12. def union_area(bboxa, bboxb):
  13. x1 = max(bboxa[0], bboxb[0])
  14. y1 = max(bboxa[1], bboxb[1])
  15. x2 = min(bboxa[2], bboxb[2])
  16. y2 = min(bboxa[3], bboxb[3])
  17. if y2 < y1 or x2 < x1:
  18. return -1
  19. return (y2 - y1) * (x2 - x1)
  20. def get_yololabel_strings(clslist, labellist):
  21. content = ''
  22. for cls, xywh in zip(clslist, labellist):
  23. content += str(int(cls)) + ' ' + ' '.join([str(e) for e in xywh]) + '\n'
  24. if len(content) != 0:
  25. content = content[:-1]
  26. return content
  27. # 4 points bbox to 8 points polygon
  28. def xywh2xyxypoly(xywh, to_int=True):
  29. xyxypoly = np.tile(xywh[:, [0, 1]], 4)
  30. xyxypoly[:, [2, 4]] += xywh[:, [2]]
  31. xyxypoly[:, [5, 7]] += xywh[:, [3]]
  32. if to_int:
  33. xyxypoly = xyxypoly.astype(np.int64)
  34. return xyxypoly
  35. def xyxy2yolo(xyxy, w: int, h: int):
  36. if xyxy == [] or xyxy == np.array([]) or len(xyxy) == 0:
  37. return None
  38. if isinstance(xyxy, list):
  39. xyxy = np.array(xyxy)
  40. if len(xyxy.shape) == 1:
  41. xyxy = np.array([xyxy])
  42. yolo = np.copy(xyxy).astype(np.float64)
  43. yolo[:, [0, 2]] = yolo[:, [0, 2]] / w
  44. yolo[:, [1, 3]] = yolo[:, [1, 3]] / h
  45. yolo[:, [2, 3]] -= yolo[:, [0, 1]]
  46. yolo[:, [0, 1]] += yolo[:, [2, 3]] / 2
  47. return yolo
  48. def yolo_xywh2xyxy(xywh: np.array, w: int, h: int, to_int=True):
  49. if xywh is None:
  50. return None
  51. if len(xywh) == 0:
  52. return None
  53. if len(xywh.shape) == 1:
  54. xywh = np.array([xywh])
  55. xywh[:, [0, 2]] *= w
  56. xywh[:, [1, 3]] *= h
  57. xywh[:, [0, 1]] -= xywh[:, [2, 3]] / 2
  58. xywh[:, [2, 3]] += xywh[:, [0, 1]]
  59. if to_int:
  60. xywh = xywh.astype(np.int64)
  61. return xywh
  62. def letterbox(im, new_shape=(640, 640), color=(0, 0, 0), auto=False, scaleFill=False, scaleup=True, stride=128):
  63. # Resize and pad image while meeting stride-multiple constraints
  64. shape = im.shape[:2] # current shape [height, width]
  65. if not isinstance(new_shape, tuple):
  66. new_shape = (new_shape, new_shape)
  67. # Scale ratio (new / old)
  68. r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
  69. if not scaleup: # only scale down, do not scale up (for better val mAP)
  70. r = min(r, 1.0)
  71. # Compute padding
  72. ratio = r, r # width, height ratios
  73. new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
  74. dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
  75. if auto: # minimum rectangle
  76. dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding
  77. elif scaleFill: # stretch
  78. dw, dh = 0.0, 0.0
  79. new_unpad = (new_shape[1], new_shape[0])
  80. ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
  81. # dw /= 2 # divide padding into 2 sides
  82. # dh /= 2
  83. dh, dw = int(dh), int(dw)
  84. if shape[::-1] != new_unpad: # resize
  85. im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
  86. top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
  87. left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
  88. im = cv2.copyMakeBorder(im, 0, dh, 0, dw, cv2.BORDER_CONSTANT, value=color) # add border
  89. return im, ratio, (dw, dh)
  90. def resize_keepasp(im, new_shape=640, scaleup=True, interpolation=cv2.INTER_LINEAR, stride=None):
  91. shape = im.shape[:2] # current shape [height, width]
  92. if new_shape is not None:
  93. if not isinstance(new_shape, tuple):
  94. new_shape = (new_shape, new_shape)
  95. else:
  96. new_shape = shape
  97. # Scale ratio (new / old)
  98. r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
  99. if not scaleup: # only scale down, do not scale up (for better val mAP)
  100. r = min(r, 1.0)
  101. new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
  102. if stride is not None:
  103. h, w = new_unpad
  104. if new_shape[0] % stride != 0:
  105. new_h = (stride - (new_shape[0] % stride)) + h
  106. else:
  107. new_h = h
  108. if w % stride != 0:
  109. new_w = (stride - (w % stride)) + w
  110. else:
  111. new_w = w
  112. new_unpad = (new_h, new_w)
  113. if shape[::-1] != new_unpad: # resize
  114. im = cv2.resize(im, new_unpad, interpolation=interpolation)
  115. return im
  116. def enlarge_window(rect, im_w, im_h, ratio=2.5, aspect_ratio=1.0) -> List:
  117. assert ratio > 1.0
  118. x1, y1, x2, y2 = rect
  119. w = x2 - x1
  120. h = y2 - y1
  121. # https://numpy.org/doc/stable/reference/generated/numpy.roots.html
  122. coeff = [aspect_ratio, w+h*aspect_ratio, (1-ratio)*w*h]
  123. roots = np.roots(coeff)
  124. roots.sort()
  125. delta = int(round(roots[-1] / 2 ))
  126. delta_w = int(delta * aspect_ratio)
  127. delta_w = min(x1, im_w - x2, delta_w)
  128. delta = min(y1, im_h - y2, delta)
  129. rect = np.array([x1-delta_w, y1-delta, x2+delta_w, y2+delta], dtype=np.int64)
  130. return rect.tolist()
  131. def draw_connected_labels(num_labels, labels, stats, centroids, names="draw_connected_labels", skip_background=True):
  132. labdraw = np.zeros((labels.shape[0], labels.shape[1], 3), dtype=np.uint8)
  133. max_ind = 0
  134. if isinstance(num_labels, int):
  135. num_labels = range(num_labels)
  136. # for ind, lab in enumerate((range(num_labels))):
  137. for lab in num_labels:
  138. if skip_background and lab == 0:
  139. continue
  140. randcolor = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
  141. labdraw[np.where(labels==lab)] = randcolor
  142. maxr, minr = 0.5, 0.001
  143. maxw, maxh = stats[max_ind][2] * maxr, stats[max_ind][3] * maxr
  144. minarea = labdraw.shape[0] * labdraw.shape[1] * minr
  145. stat = stats[lab]
  146. bboxarea = stat[2] * stat[3]
  147. if stat[2] < maxw and stat[3] < maxh and bboxarea > minarea:
  148. pix = np.zeros((labels.shape[0], labels.shape[1]), dtype=np.uint8)
  149. pix[np.where(labels==lab)] = 255
  150. rect = cv2.minAreaRect(cv2.findNonZero(pix))
  151. box = np.int0(cv2.boxPoints(rect))
  152. labdraw = cv2.drawContours(labdraw, [box], 0, randcolor, 2)
  153. labdraw = cv2.circle(labdraw, (int(centroids[lab][0]),int(centroids[lab][1])), radius=5, color=(random.randint(0,255), random.randint(0,255), random.randint(0,255)), thickness=-1)
  154. cv2.imshow(names, labdraw)
  155. return labdraw