我已经通过使用下面的方法成功了,但我确信必须有其他更省时的替代方法来提供精确的旋转角度,而不是像下面的方法那样提供近似值。我很高兴听到您的反馈。
该过程基于以下步骤:
- 导入模板图像(即:方向为 0º)
- 创建相同图像的离散数组,但每个图像与其最近的邻居相比以 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 数组上的不同通道。
- 检索与我们之前旋转并存储到数组中的示例图像相比,我正在查看的角度的“test_image”
- 遵循一系列 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)
# ../..
- 最后,我选择与样本图像匹配的离散化角度作为与“max_val”最大值的模板图像相对应的角度。
考虑到结果精度基于旋转模板图像的数量而具有更高/更低精度的近似值,以及旋转模板数量增加时所花费的上升时间,这已证明效果很好......
我确信必须有其他基于不同方法的更智能的替代方案,例如生成图像的一种“方向矢量”,因此只需将结果数字与样本模板中先前已知的数字进行比较......
您的反馈将不胜感激。