python - 使用图像处理检测近水平线

标签 python opencv image-processing

有没有办法使用opencv来检测接近水平的线?我对 How to detect lines in OpenCV? 中提到的一些概念感到困惑。 -- 我可以使用 canny 进行边缘检测,但我对如何使用霍夫变换并将其限制为水平线有点迷失。

我这里有一堆示例图像:https://gist.github.com/jason-s/df90e41e29f3ba46e6ccabad4516e916

包括:

特别是,每个图像都有一对水平边缘,长度约为 1200 像素,与水平方向的夹角在 3 度以内。 (这些是由我扫描的照片的角组成的。)

关于使用什么算法有什么建议吗?

最佳答案

通过线方向度进行线检测过滤器

import cv2
import numpy as np
import math

path='images/lines.png'
image = cv2.imread(path)

dst = cv2.Canny(image, 50, 200, None, 3)
linesP = cv2.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10)

if linesP is not None:
    for i in range(0, len(linesP)):
        l = linesP[i][0]

        #here l contains x1,y1,x2,y2  of your line
        #so you can compute the orientation of the line 
        p1 = np.array([l[0],l[1]])
        p2 = np.array([l[2],l[3]])

        p0 = np.subtract( p1,p1 ) #not used
        p3 = np.subtract( p2,p1 ) #translate p2 by p1

        angle_radiants = math.atan2(p3[1],p3[0])
        angle_degree = angle_radiants * 180 / math.pi

        print("line degree", angle_degree)

        if 0 < angle_degree < 15 or 0 > angle_degree > -15 :
            cv2.line(image,  (l[0], l[1]), (l[2], l[3]), (0,0,255), 1, cv2.LINE_AA)


cv2.imshow("Source", image)

print("Press any key to close")
cv2.waitKey(0)
cv2.destroyAllWindows()

enter image description here

关于python - 使用图像处理检测近水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70476773/

相关文章:

python - 如何在 Django 中为文件上传类编写单元测试?

python - python 网络应用程序如何在客户端计算机上打开程序?

python - 使用测试数据库在 "test-mode"中运行服务器

python - 如何使用 Python OpenCV 通过消除噪声来检测复选框?

c++ - dlib 的 dcd trainer 的 'warm start' 选项是否只用于 1 类分类?

linux - 我们如何比较两个程序的依赖关系图?

python - 自定义 python 对象的 pretty-print __str__

opencv - 检测图像中嵌入的 Blob 区域

python - 使用 OpenCV 读取图像并使用 Tkinter 显示它

visual-c++ - 使用 Visual c++ 2008 配置 OpenCV