当前位置:   article > 正文

目标检测透视变换数据增强包含label(yolov5格式)_数据增强 透视变换

数据增强 透视变换
  1. # -*- coding:utf-8 -*-
  2. import cv2
  3. import numpy as np
  4. import sys
  5. def rad(x):
  6. return x * np.pi / 180
  7. def get_warpR():
  8. global anglex, angley, anglez, fov, W, H, r
  9. # 镜头与图像间的距离,21为半可视角,算z的距离是为了保证在此可视角度下恰好显示整幅图像
  10. z = np.sqrt(H ** 2 + W ** 2) / 2 / np.tan(rad(fov / 2))
  11. # 齐次变换矩阵
  12. rx = np.array([[1, 0, 0, 0],
  13. [0, np.cos(rad(anglex)), -np.sin(rad(anglex)), 0],
  14. [0, -np.sin(rad(anglex)), np.cos(rad(anglex)), 0, ],
  15. [0, 0, 0, 1]], np.float32)
  16. ry = np.array([[np.cos(rad(angley)), 0, np.sin(rad(angley)), 0],
  17. [0, 1, 0, 0],
  18. [-np.sin(rad(angley)), 0, np.cos(rad(angley)), 0, ],
  19. [0, 0, 0, 1]], np.float32)
  20. rz = np.array([[np.cos(rad(anglez)), np.sin(rad(anglez)), 0, 0],
  21. [-np.sin(rad(anglez)), np.cos(rad(anglez)), 0, 0],
  22. [0, 0, 1, 0],
  23. [0, 0, 0, 1]], np.float32)
  24. r = rx.dot(ry).dot(rz)
  25. # 四对点的生成
  26. pcenter = np.array([W / 2, H / 2, 0, 0], np.float32)
  27. p1 = np.array([0, 0, 0, 0], np.float32) - pcenter
  28. p2 = np.array([H, 0, 0, 0], np.float32) - pcenter
  29. p3 = np.array([0, W, 0, 0], np.float32) - pcenter
  30. p4 = np.array([H, W, 0, 0], np.float32) - pcenter
  31. dst1 = r.dot(p1)
  32. dst2 = r.dot(p2)
  33. dst3 = r.dot(p3)
  34. dst4 = r.dot(p4)
  35. list_dst = [dst1, dst2, dst3, dst4]
  36. org = np.array([[0, 0],
  37. [H, 0],
  38. [0, W],
  39. [H, W]], np.float32)
  40. dst = np.zeros((4, 2), np.float32)
  41. # 投影至成像平面
  42. for i in range(4):
  43. dst[i, 0] = list_dst[i][0] * z / (z - list_dst[i][2]) + pcenter[0]
  44. dst[i, 1] = list_dst[i][1] * z / (z - list_dst[i][2]) + pcenter[1]
  45. warpR = cv2.getPerspectiveTransform(org, dst)
  46. return warpR
  47. # ccc = 0
  48. # for i in range(1,8):
  49. # for angley in [i*10,-i*10]:
  50. # labels = []
  51. # with open('/home/lixuan/Arduino/sku_recogniztion/imgs/000368.txt') as f:
  52. # boxs = []
  53. # datas = f.readlines()
  54. # for data in datas:
  55. # label = []
  56. # data = data.strip()
  57. # data = data.split(' ')[1:]
  58. # x = float(data[0]) * W
  59. # y = float(data[1]) * H
  60. # w = float(data[2]) * W
  61. # h = float(data[3]) * H
  62. # x1 = int(x - w / 2)
  63. # y1 = int(y - h / 2)
  64. # x2 = int(x + w / 2)
  65. # y2 = int(y + h / 2)
  66. # label.append([x1, y1])
  67. # label.append([x2, y1])
  68. # label.append([x2, y2])
  69. # label.append([x1, y2])
  70. # xs = []
  71. # ys = []
  72. # for xys in label:
  73. # xs.append(xys[0])
  74. # ys.append(xys[1])
  75. # contour = np.vstack((xs, ys)) # 将x,y竖着拼接
  76. # contour = np.array(contour.T, dtype=np.float32)
  77. # box = cv2.boundingRect(contour)
  78. # boxs.append([int(box[0]), int(box[1]), int(box[0] + box[2]), int(box[1] + box[3])])
  79. # bboxes = []
  80. # for box in boxs:
  81. # bboxes.append([[box[0],box[1]],[box[2],box[3]]])
  82. # # # img = cv2.rectangle(img, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2)
  83. #
  84. # warpR = get_warpR()
  85. #
  86. # result = cv2.warpPerspective(img, warpR, (W, H))
  87. #
  88. # matrix = warpR
  89. # for label in bboxes:
  90. # box = []
  91. # for p in label:
  92. # px = (matrix[0][0] * p[0] + matrix[0][1] * p[1] + matrix[0][2]) / (
  93. # (matrix[2][0] * p[0] + matrix[2][1] * p[1] + matrix[2][2]))
  94. # py = (matrix[1][0] * p[0] + matrix[1][1] * p[1] + matrix[1][2]) / (
  95. # (matrix[2][0] * p[0] + matrix[2][1] * p[1] + matrix[2][2]))
  96. # p_after = (int(px), int(py)) # after transformation
  97. # box.append(list(p_after))
  98. # if box[0][0] < 0:
  99. # box[0][0] = 0
  100. # if box[0][1] < 0:
  101. # box[0][1] = 0
  102. # if box[1][0] < 0:
  103. # box[1][0] = 0
  104. # if box[1][1] < 0:
  105. # box[1][1] = 0
  106. # if box[0][0] > W:
  107. # box[0][0] = W
  108. # if box[1][0] > W:
  109. # box[1][0] = W
  110. # if box[0][1] > H:
  111. # box[0][1] = H
  112. # if box[1][1] > H:
  113. # box[1][1] = H
  114. # result = cv2.rectangle(result, (box[0][0], box[0][1]), (box[1][0], box[1][1]), (0, 255, 0), 2)
  115. # # result = cv2.circle(result, p_after, 10, (0, 0, 255), -1)
  116. #
  117. # # cv2.namedWindow('result', 2)
  118. # # cv2.imshow("result", result)
  119. # # c = cv2.waitKey(0)
  120. # cv2.imwrite('/home/lixuan/sku_data/cailiao/{}.jpg'.format(ccc),result)
  121. # ccc += 1
  122. import os
  123. ccc = 0
  124. anglex = 0
  125. angley = 0
  126. anglez = 0 # 是旋转
  127. fov = 42
  128. r = 0
  129. for imgfile in os.listdir('/home/lixuan/sku_data/local/images'):
  130. img = cv2.imread('/home/lixuan/sku_data/local/images/{}'.format(imgfile))
  131. H, W = img.shape[0:2]
  132. for i in range(1,8):
  133. for angley in [i*10,-i*10]:
  134. labels = []
  135. with open(os.path.join('/home/lixuan/sku_data/local/labels',imgfile.replace('jpg','txt'))) as f:
  136. boxs = []
  137. datas = f.readlines()
  138. for data in datas:
  139. label = []
  140. data = data.strip()
  141. data = data.split(' ')[1:]
  142. x = float(data[0]) * W
  143. y = float(data[1]) * H
  144. w = float(data[2]) * W
  145. h = float(data[3]) * H
  146. x1 = int(x - w / 2)
  147. y1 = int(y - h / 2)
  148. x2 = int(x + w / 2)
  149. y2 = int(y + h / 2)
  150. label.append([x1, y1])
  151. label.append([x2, y1])
  152. label.append([x2, y2])
  153. label.append([x1, y2])
  154. xs = []
  155. ys = []
  156. for xys in label:
  157. xs.append(xys[0])
  158. ys.append(xys[1])
  159. contour = np.vstack((xs, ys)) # 将x,y竖着拼接
  160. contour = np.array(contour.T, dtype=np.float32)
  161. box = cv2.boundingRect(contour)
  162. boxs.append([int(box[0]), int(box[1]), int(box[0] + box[2]), int(box[1] + box[3])])
  163. bboxes = []
  164. for box in boxs:
  165. bboxes.append([[box[0],box[1]],[box[2],box[3]]])
  166. # # img = cv2.rectangle(img, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2)
  167. warpR = get_warpR()
  168. result = cv2.warpPerspective(img, warpR, (W, H))
  169. matrix = warpR
  170. fw = open('/home/lixuan/sku_data/local/labelscopy/sku_{}.txt'.format(ccc),'w')
  171. for label in bboxes:
  172. box = []
  173. for p in label:
  174. px = (matrix[0][0] * p[0] + matrix[0][1] * p[1] + matrix[0][2]) / (
  175. (matrix[2][0] * p[0] + matrix[2][1] * p[1] + matrix[2][2]))
  176. py = (matrix[1][0] * p[0] + matrix[1][1] * p[1] + matrix[1][2]) / (
  177. (matrix[2][0] * p[0] + matrix[2][1] * p[1] + matrix[2][2]))
  178. p_after = (int(px), int(py)) # after transformation
  179. box.append(list(p_after))
  180. if box[0][0] < 0:
  181. box[0][0] = 0
  182. if box[0][1] < 0:
  183. box[0][1] = 0
  184. if box[1][0] < 0:
  185. box[1][0] = 0
  186. if box[1][1] < 0:
  187. box[1][1] = 0
  188. if box[0][0] > W:
  189. box[0][0] = W
  190. if box[1][0] > W:
  191. box[1][0] = W
  192. if box[0][1] > H:
  193. box[0][1] = H
  194. if box[1][1] > H:
  195. box[1][1] = H
  196. x1 = box[0][0] / W
  197. y1 = box[0][1] / H
  198. x2 = box[1][0] / W
  199. y2 = box[1][1] / H
  200. x = (x1+x2)/2
  201. y = (y1+y2)/2
  202. w = x2-x1
  203. h = y2-y1
  204. fw.write('0 ' + str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h) + '\n')
  205. # result = cv2.rectangle(result, (box[0][0], box[0][1]), (box[1][0], box[1][1]), (0, 255, 0), 2)
  206. # result = cv2.circle(result, p_after, 10, (0, 0, 255), -1)
  207. # cv2.namedWindow('result', 2)
  208. # cv2.imshow("result", result)
  209. # c = cv2.waitKey(0)
  210. fw.close()
  211. cv2.imwrite('/home/lixuan/sku_data/local/imagescopy/sku_{}.jpg'.format(ccc),result)
  212. ccc += 1

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号