1 回答

TA貢獻1860條經驗 獲得超8個贊
一種方法是使用霍夫變換來檢測線并獲得每條線的角度。然后可以通過減去兩條線之間的差來找到兩條線之間的角度。
我們首先使用算術平均來np.mean對導致此結果的圖像進行閾值處理。
image = cv2.imread('2.png')
# Compute arithmetic mean
image = np.mean(image, axis=2)
現在我們執行skimage.transform.hough_line檢測線
# Perform Hough Transformation to detect lines
hspace, angles, distances = hough_line(image)
# Find angle
angle=[]
for _, a , distances in zip(*hough_line_peaks(hspace, angles, distances)):
angle.append(a)
接下來我們獲得每條線的角度并找到差異以獲得我們的結果
# Obtain angle for each line
angles = [a*180/np.pi for a in angle]
# Compute difference between the two lines
angle_difference = np.max(angles) - np.min(angles)
print(angle_difference)
16.08938547486033
完整代碼
from skimage.transform import (hough_line, hough_line_peaks)
import numpy as np
import cv2
image = cv2.imread('2.png')
# Compute arithmetic mean
image = np.mean(image, axis=2)
# Perform Hough Transformation to detect lines
hspace, angles, distances = hough_line(image)
# Find angle
angle=[]
for _, a , distances in zip(*hough_line_peaks(hspace, angles, distances)):
angle.append(a)
# Obtain angle for each line
angles = [a*180/np.pi for a in angle]
# Compute difference between the two lines
angle_difference = np.max(angles) - np.min(angles)
print(angle_difference)
添加回答
舉報