我能够找到以下链接:Calculating the angle between two lines in an image in Python,然后我只使用了允许计算角度的代码部分:
import numpy as np
from skimage.transform import (hough_line, hough_line_peaks, probabilistic_hough_line)
from pylab import imread, gray, mean
import matplotlib.pyplot as plt
image = imread('D:\\Pictures\\PyTestPics\\oo.tiff')
image = np.mean(image, axis=2)
h, theta, d = hough_line(image)
angle = []
dist = []
for _, a, d in zip(*hough_line_peaks(h, theta, d)):
angle.append(a)
dist.append(d)
angle = [a*180/np.pi for a in angle]
angle_reel = np.max(angle) - np.min(angle)
print(angle_reel)
有人可以向我解释一下for循环和angle_reel的代码吗?因为我不明白如何在图像中的哪条线和其他物体之间形成多个角度?会非常感激。
答案 0 :(得分:2)
您的图片有两行,我称它们为 a行和 b行。这些线中的每条线都有一定的角度。这些线之间的角度将为angle of line a - angle of line b.
当您的代码遍历其中的hough_line_peaks
时,实际上是遍历每一行的数据。每条线都有一个距离和一个角度。
for _, a, d in zip(*hough_line_peaks(h, theta, d)):
angle.append(a)
dist.append(d)
如果图像中有两条线,则最终将得到一个具有两个值的角度列表。这两个值将是线条相对于图像边缘的角度。要找到直线相对于彼此的角度,请减去这些值。
这是示例图片:
线的角度为:[1.3075343725834614, 0.48264691605429766]
。那是弧度,因此它们用代码angle = [a*180/np.pi for a in angle]
转换为度。角度为[74.91620111731844, 27.65363128491619]
。这似乎很合理,一个大于45度,另一个小于45度。线之间的角度为max(angles) - min(angles)
或47.262度。
此图像显示了在图像上绘制的角度: