Advertisement
makut

Untitled

Sep 22nd, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import numpy as np
  2. import itertools
  3.  
  4.  
  5. def get_borders(image1, image2, shift_x, shift_y):
  6. if shift_x >= 0:
  7. x_left_first = shift_x
  8. x_left_second = 0
  9. else:
  10. x_left_first = 0
  11. x_left_second = -shift_x
  12.  
  13. x_length = min(image1.shape[1] - x_left_first, image2.shape[1] - x_left_second)
  14.  
  15. if shift_y >= 0:
  16. y_up_first = shift_y
  17. y_up_second = 0
  18. else:
  19. y_up_first = 0
  20. y_up_second = -shift_y
  21.  
  22. y_length = min(image1.shape[0] - y_up_first, image2.shape[0] - y_up_second)
  23.  
  24. return x_left_first, x_left_second, y_up_first, y_up_second, x_length, y_length
  25.  
  26.  
  27.  
  28. def mse(image1, image2, shift_x, shift_y):
  29. (x_left_first, x_left_second,
  30. y_up_first, y_up_second, x_length, y_length) = get_borders(image1, image2, shift_x, shift_y)
  31.  
  32. subimage1 = image1[y_up_first: y_up_first + y_length,
  33. x_left_first: x_left_first + x_length]
  34. subimage2 = image2[y_up_second: y_up_second + y_length,
  35. x_left_second: x_left_second + x_length]
  36. return np.mean((subimage1 - subimage2) ** 2)
  37.  
  38.  
  39. def find_optimal_shift(image1, image2, shift_variants_x, shift_variants_y, criterion):
  40. return min(itertools.product(shift_variants_x, shift_variants_y),
  41. key=lambda x: criterion(image1, image2, *x))
  42.  
  43.  
  44. def pyramidal_alignment(image1, image2, criterion=mse):
  45. image1 = image1.astype(float)
  46. image2 = image2.astype(float)
  47. images = [(image1, image2)]
  48. while max(*image1.shape, *image2.shape) > 500:
  49. image1 = image1[::2, ::2]
  50. image2 = image2[::2, ::2]
  51. images.append((image1, image2))
  52. shift_x, shift_y = find_optimal_shift(image1, image2, range(-15, 16),
  53. range(-15, 16), criterion)
  54. images.pop()
  55. while images:
  56. image1, image2 = images.pop()
  57. shift_x *= 2
  58. shift_y *= 2
  59. shift_x, shift_y = find_optimal_shift(
  60. image1, image2,
  61. range(shift_x - 15, shift_x + 16),
  62. range(shift_y - 15, shift_y + 16),
  63. criterion,
  64. )
  65. return shift_x, shift_y
  66.  
  67.  
  68. def cut(image):
  69. x_border = int(image.shape[1] * 0.05)
  70. y_border = int(image.shape[0] * 0.05)
  71. return image[y_border: -y_border, x_border: -x_border]
  72.  
  73.  
  74. def align(image, green1):
  75. blue = cut(image[:len(image) // 3])
  76. green = cut(image[len(image) // 3: len(image) // 3 * 2])
  77. red = cut(image[len(image) // 3 * 2:])
  78. blue_x, blue_y = pyramidal_alignment(green, blue)
  79. red_x, red_y = pyramidal_alignment(green, red)
  80.  
  81. (green_x1, blue_x, green_y1,
  82. blue_y, x_length_blue, y_length_blue) = get_borders(green, blue, blue_x, blue_y)
  83. (green_x2, red_x, green_y2,
  84. red_y, x_length_red, y_length_red) = get_borders(green, red, blue_x, blue_y)
  85. if green_x1 >= green_x2:
  86. green_x = green_x1
  87. red_x += green_x1 - green_x2
  88. x_length_red -= green_x1 - green_x2
  89. else:
  90. green_x = green_x2
  91. blue_x += green_x2 - green_x1
  92. x_length_blue -= green_x2 - green_x1
  93.  
  94. if green_y1 >= green_y2:
  95. green_y = green_y1
  96. red_y += green_y1 - green_y2
  97. y_length_red -= green_y1 - green_y2
  98. else:
  99. green_y = green_y2
  100. blue_y += green_y2 - green_y1
  101. y_length_blue -= green_y2 - green_y1
  102.  
  103. x_length = min(x_length_blue, x_length_red)
  104. y_length = min(y_length_blue, y_length_red)
  105.  
  106. return (
  107. np.stack((red[red_y: red_y + y_length, red_x: red_x + x_length],
  108. green[green_y: green_y + y_length, green_x: green_x + x_length],
  109. blue[blue_y: blue_y + y_length, blue_x: blue_x + x_length]), axis=-1),
  110. (green1[0] + blue_y, green1[1] + blue_x), (green1[0] + red_y, green1[1] + red_x),
  111. )
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement