关于hough_line和hough_line_peaks的代码说明

问题描述 投票:2回答:1

我能够找到这个链接: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的代码吗?因为我无法理解如何有多个角度,并且在图像内部的哪一行与其他对象之间形成多个角度?真的很感激。

python image numpy scikit-image hough-transform
1个回答
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)

如果图像中有两行,则最终会得到一个具有两个值的角度列表。这两个值将是参考图像边缘的线的角度。要找到彼此相对的线的角度,请减去这些值。

这是一个示例图像:

image with two lines

线条的角度是:[1.3075343725834614, 0.48264691605429766]。那是弧度,所以它们用代码转换为度:angle = [a*180/np.pi for a in angle]。以度为单位,角度为[74.91620111731844, 27.65363128491619]。这似乎很合理,一个是45度多一点,一个少一点。线之间的角度是max(angles) - min(angles)或47.262度。

此图像显示图像上绘制的角度:

angles on image

© www.soinside.com 2019 - 2024. All rights reserved.