简体   繁体   English

hough_line 和 hough_line_peaks 的代码说明

[英]code explanation about hough_line and hough_line_peaks

I was able to find this link: Calculating the angle between two lines in an image in Python which I then only took the code part that allows to calculate the angle:我能够找到这个链接: 用 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)

can anyone please explain me the code of the for loop and the angle_reel?谁能解释一下for循环和angle_reel的代码? because I couldn't understand how are there multiple angles and those multiple angles are formed between what line and what other object inside the image?因为我不明白怎么会有多个角度,这些多个角度是在图像内的哪条线和其他什么物体之间形成的? It would be really appreciated.真的很感激。

Your image has two lines, I'll call them line a and line b .您的图像有两行,我将它们称为line aline b Each of those lines will have and angle.这些线中的每一条都会有 和 角度。 The angle between those lines will be the angle of line a - angle of line b.这些线之间的角度将是angle of line a - angle of line b.

When your code iterates through the hough_line_peaks in the, it is actually iterating though the data for each line.当您的代码遍历 中的hough_line_peaks时,它实际上是遍历每一行的数据。 Each line has a distance, and an angle.每条线都有一个距离和一个角度。

for _, a, d in zip(*hough_line_peaks(h, theta, d)):
    angle.append(a)
    dist.append(d)

If there are two lines in your image, you will end up with a list of angles that has two values.如果图像中有两条线,则最终会得到一个包含两个值的角度列表。 Those two values will be the angles of the lines in reference to the edge of the image.这两个值将是线条相对于图像边缘的角度。 To find the angle of the lines in reference to each other, subtract the values.要找到相互参考的线的角度,请减去这些值。

Here is an example image:这是一个示例图像:

两条线的图像

The angles of the lines are: [1.3075343725834614, 0.48264691605429766] .线的角度是: [1.3075343725834614, 0.48264691605429766] That's in radians, so they are converted to degrees with the code: angle = [a*180/np.pi for a in angle] .这是以弧度为单位,因此使用以下代码将它们转换为度数: angle = [a*180/np.pi for a in angle] In degrees the angles are [74.91620111731844, 27.65363128491619] .以度为单位的角度是[74.91620111731844, 27.65363128491619] This seems pretty reasonable, one is a little more than 45 degrees and one is a little less.这似乎很合理,一个比 45 度多一点,一个少一点。 The angle between the lines is max(angles) - min(angles) or 47.262 degrees.线之间的角度是max(angles) - min(angles)或 47.262 度。

This image shows the angles drawn on the image:此图像显示了在图像上绘制的角度:

图像上的角度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM