我已經通過使用下面的方法成功了,但我確定必須有其他更具時間效率的替代方法來提供精確的旋轉角度,而不是作爲下面的方法的近似。我很樂意聽到您的反饋。如何測量圖像與已知模板相比的旋轉角度
該程序是基於以下步驟:
- 導入的模板圖像(即:與在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'
- 最後,我選取與樣本圖像匹配的離散化角度作爲與'max_val'最高值相對應的模板圖像。
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)
# ../..
考慮到精度的高低取決於旋轉的模板圖像的數量,以及旋轉後的模板數量增加時的上升時間,這已被證明可以正常工作。
我確定必須有其他更智能的替代方法,例如根據不同的方法生成一種圖像的「方向矢量」,然後將結果數字與樣本模板中以前已知的數字進行比較。 ..
您的反饋將高度讚賞。