首先进行图像的预处理
这里我没有课本上的图, 所以自己画了一个三角。
好的预处理完成[手动狗头]
直线拟合
minLineLength = 120
maxLineGap = 100
threshold =200
lines = cv2.HoughLinesP(img_poly,1,np.pi/180,threshold, minLineLength, maxLineGap)
print("this is len",len(lines))
识别出了7段直线。 7段!!!, 从图中可以看到很多直线都重叠了, minLineLength这个参数作用不大
于是乎我自行使用直线之间的距离为标准再筛选一遍
# 随随便便写一个距离判别都比官方的好.
# update_lines.append(lines[0][0])
# too_close=False
# threshold_=10
# for i in range(len(lines)):
# for j in range(len(update_lines)):
# ret = calcdistance(lines[i][0], update_lines[j])
# if(ret[0]<threshold_ and ret[1]<threshold_):
# too_close=True
#
# if(not too_close):
# update_lines.append(lines[i][0])
# too_close=False
# print("this is len, after update",len(update_lines))
# print(update_lines)
结果是5 段。 还可以再使用夹角大小来继续筛选
求交点以及角度
使用余弦定理。 如果不记得余弦定理的话,那就滚回去读高中…(开玩笑的,我自己也忘了)
看看这篇文章把