Bananaware

cut

Oct 3rd, 2022
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. # cut nasal pattern
  2. def cutNasalPattern(linha, image):
  3. linha = linha.split(",")
  4. if len(linha) == 9:
  5. x1 = int(linha[1])
  6. y1 = int(linha[2])
  7. w1 = int(linha[3])
  8. h1 = int(linha[4])
  9.  
  10. x2 = int(linha[5])
  11. y2 = int(linha[6])
  12. w2 = int(linha[7])
  13. h2 = int(linha[8])
  14.  
  15. x_esq = min(x1 + w1, x2 + w2)
  16. x_dir = max(x1, x2)
  17.  
  18. ye = y1+0.4*h1
  19. yd = y2+0.4*h2
  20.  
  21. if x1 > x2:
  22. ye, yd = yd, ye
  23.  
  24. # rotacao
  25. if abs(ye-yd) > 5:
  26. v1 = [1, 0]
  27. v2 = [x_dir-x_esq, yd-ye]
  28. dim = int(np.linalg.norm(v2))
  29. v2 = v2/np.linalg.norm(v2)
  30. theta = np.arctan2(np.linalg.norm(np.cross(v1, v2)), np.dot(v1,v2))
  31. if yd < ye:
  32. theta = -theta
  33. ct = np.cos(theta)
  34. st = np.sin(theta)
  35. M = np.array([[ct, st, (1-ct)*x_esq - st*ye],
  36. [-st, ct, st*x_esq + (1-ct)*ye]])
  37. sy, sx = np.shape(image)[:2]
  38. image = cv2.warpAffine(image, M, dsize=(sx, sy))
  39.  
  40. y_top = int(ye)
  41. y_bot = y_top+dim
  42. x_dir = x_esq+dim
  43. else:
  44. dim = x_dir - x_esq
  45. y_top = int((y1 + 0.4 * h1 + y2 + 0.4 * h2) / 2)
  46. y_bot = y_top + dim
  47.  
  48. if y_top < y_bot and x_esq < x_dir:
  49. roi = image[y_top : y_bot, x_esq : x_dir]
  50. cv2.imwrite("results/YOLO_nasalpattern/" + linha[0], roi)
  51. coords_file.write("%s,%d,%d,%d,%d\n" % (linha[0], x_esq, y_top, x_dir, y_bot))
  52.  
Advertisement
Add Comment
Please, Sign In to add comment