有没有办法使用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()
关于python - 使用图像处理检测近水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70476773/