1

我已经通过使用下面的方法成功了,但我确信必须有其他更省时的替代方法来提供精确的旋转角度,而不是像下面的方法那样提供近似值。我很高兴听到您的反馈。

该过程基于以下步骤:

  1. 导入模板图像(即:方向为 0º)
  2. 创建相同图像的离散数组,但每个图像与其最近的邻居相比以 360º/rotatesteps 旋转(即:30 到 50 个旋转图像)

# python 3 / opencv 3

# Settings:
rotate_steps = 36
step_angle = round((360/rotate_steps), 0) # one image at each 10º

# Rotation function
def rotate_image(image, angle):
# ../..
return rotated_image

# Importing a sample image and creating a n-dimension array where to store images in:
image = cv2.imread('sample_image.png')
image_Array = np.zeros((image.shape[1], image.shape[0], 1), dtype='uint8')

# Rotating sample image and saving it into the array as a new channel:
while rotation_angle <= (360 - step_angle):
                           angles.append(rotation_angle)
                           image_array[:,:,channel] = rotate_image(image.copy(), rotation_angle)
# ../..

所以我得到:

角度 = [0, 10.0, 20.0, 30.0, .../..., 340.0, 350.0]

image_array = [image_1, image_2, image_3, ...] 其中 image_i 是 numpy 数组上的不同通道。

  1. 检索与我们之前旋转并存储到数组中的示例图像相比,我正在查看的角度的“test_image”
  2. 遵循一系列 cv2.matchTemplate() 和 cv2.minMaxLoc() 来找到旋转图像的角度与“test_image”最匹配

for i in range(len(angles)):
  res = cv2.matchTemplate(test_image, image_array[:,:,i], cv2.TM_CCOEFF_NORMED)
  min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
  # ../..

  1. 最后,我选择与样本图像匹配的离散化角度作为与“max_val”最大值的模板图像相对应的角度。

考虑到结果精度基于旋转模板图像的数量而具有更高/更低精度的近似值,以及旋转模板数量增加时所花费的上升时间,这已证明效果很好......

我确信必须有其他基于不同方法的更智能的替代方案,例如生成图像的一种“方向矢量”,因此只需将结果数字与样本模板中先前已知的数字进行比较......

您的反馈将不胜感激。

4

1 回答 1

0

我认为您的问题没有简单的解决方案。这实际上是一个配准问题,扭曲(在这种情况下,旋转)图像以适应另一个图像。这是一个众所周知的难题,就像分割一样。

我听图像处理研究人员说“谁掌握了分割和配准,谁就掌握了图像处理”,这可能有点夸张,但它给出了大致的想法。

无论如何,你的技术就是我会采用的方式。看着研究门,https://www.researchgate.net/post/How_can_one_determine_the_rotation_angle_between_two_images,很多答案也如你所愿。另一种方法是使用特征匹配,但我不确定它会比您的解决方案更快。

也许你可以看看OpenCV注册方法http://docs.opencv.org/trunk/db/d61/group__reg.html(这个链接中的方法使用像素匹配而不是特征匹配,也许更快)

于 2016-12-13T08:34:41.737 回答