逆向透视变换贴图数据增强

短命女 2022-10-15 04:50 246阅读 0赞
  1. import numpy as np
  2. import cv2
  3. def rad(x):
  4. return x * np.pi / 180
  5. def order_points(pts):
  6. # initialzie a list of coordinates that will be ordered
  7. # such that the first entry in the list is the top-left,
  8. # the second entry is the top-right, the third is the
  9. # bottom-right, and the fourth is the bottom-left
  10. rect = np.zeros((4, 2), dtype="float32")
  11. # the top-left point will have the smallest sum, whereas
  12. # the bottom-right point will have the largest sum
  13. s = pts.sum(axis=1)
  14. rect[0] = pts[np.argmin(s)]
  15. rect[2] = pts[np.argmax(s)]
  16. # now, compute the difference between the points, the
  17. # top-right point will have the smallest difference,
  18. # whereas the bottom-left will have the largest difference
  19. diff = np.diff(pts, axis=1)
  20. rect[1] = pts[np.argmin(diff)]
  21. rect[3] = pts[np.argmax(diff)]
  22. # return the ordered coordinates
  23. return rect
  24. def four_point_transform(image, pts):
  25. # obtain a consistent order of the points and unpack them
  26. # individually
  27. rect = order_points(pts)
  28. (tl, tr, br, bl) = rect
  29. # compute the width of the new image, which will be the
  30. # maximum distance between bottom-right and bottom-left
  31. # x-coordiates or the top-right and top-left x-coordinates
  32. widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
  33. widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
  34. maxWidth = max(int(widthA), int(widthB))
  35. # compute the height of the new image, which will be the
  36. # maximum distance between the top-right and bottom-right
  37. # y-coordinates or the top-left and bottom-left y-coordinates
  38. heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
  39. heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
  40. maxHeight = max(int(heightA), int(heightB))
  41. # now that we have the dimensions of the new image, construct
  42. # the set of destination points to obtain a "birds eye view",
  43. # (i.e. top-down view) of the image, again specifying points
  44. # in the top-left, top-right, bottom-right, and bottom-left
  45. # order
  46. dst = np.array([
  47. [0, 0],
  48. [maxWidth - 1, 0],
  49. [maxWidth - 1, maxHeight - 1],
  50. [0, maxHeight - 1]], dtype="float32")
  51. # compute the perspective transform matrix and then apply it
  52. M = cv2.getPerspectiveTransform(rect, dst)
  53. M = np.linalg.inv(M)
  54. # warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
  55. # return the warped image
  56. return M,(maxWidth, maxHeight)
  57. def split(L,n):
  58. List = []
  59. for j in range(0, len(L), n):
  60. b = L[j:j + n]
  61. List.append(b)
  62. return List
  63. from math import *
  64. def rota(img, degree):
  65. height, width = img.shape[:2]
  66. # 旋转后的尺寸
  67. heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
  68. widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))
  69. matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)
  70. matRotation[0, 2] += (widthNew - width) / 2
  71. matRotation[1, 2] += (heightNew - height) / 2
  72. imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))
  73. return imgRotation
  74. def getpaste(pasteimgpath,warpR,size,backsize):
  75. img = cv2.imread(pasteimgpath)
  76. h,w,_ = img.shape
  77. if size[0] < size[1] and w > h:
  78. img = rota(img,90)
  79. if size[0] > size[1] and w < h:
  80. img = rota(img, 90)
  81. img = cv2.resize(img,size)
  82. result = cv2.warpPerspective(img, warpR, backsize)
  83. return result
  84. import random
  85. for i in range(35):
  86. image = cv2.imread('/home/lhq/xg/{}.jpg'.format(i))
  87. H,W,_ = image.shape
  88. backsize = (W, H)
  89. with open('/home/lhq/xg/{}.txt'.format(i)) as f:
  90. datas = f.read().strip().split(' ')
  91. datas = split(datas,8)
  92. for data in datas:
  93. try:
  94. data = [int(x) for x in data]
  95. List = split(data,2)
  96. pts = np.array(List, dtype="float32")
  97. # apply the four point tranform to obtain a "birds eye view" of
  98. # the image
  99. warpR,size = four_point_transform(image, pts)
  100. transimg = getpaste('/home/lhq/mb/{}.jpg'.format(random.randint(0,31)),warpR,size,backsize)
  101. index = np.where(transimg > 0)
  102. image[index] = transimg[index]
  103. cv2.imshow('',image)
  104. cv2.waitKey(0)
  105. except:
  106. pass

发表评论

表情:
评论列表 (有 0 条评论,246人围观)

还没有评论,来说两句吧...

相关阅读

    相关 OpenCV 透视变换

    透视变换是将图像从一个视平面投影到另外一个视平面的过程,所以透视变换也被称为投影映射(Projection Mapping)。在图像的仿射变换中需要变换矩阵是一个2x3的两维平

    相关 OpenCV 透视变换

    透视变换是将图像从一个视平面投影到另外一个视平面的过程,所以透视变换也被称为投影映射(Projection Mapping)。在图像的仿射变换中需要变换矩阵是一个2x3的两维平