文章目录
需求:在图像上将检测到的线段转换为过图像边界的直线
def myline(x1,x2,y1,y2,w,h):
"""
线段的端点转换为图像的边界点
:param x1: 已知线段的2个端点
:param x2:
:param y1:
:param y2:
:param w: 图像的宽和高
:param h:
:return:
"""
if x1 == x2: # 竖线的情况
x3 = x1
y3 = 0
x4 = x1
y4 = h
else:
k = int((y1-y2)/(x1-x2))
b = int((x1*y2-x2*y1)/(x1-x2))
if k == 0: # 横线的情况
x3 = 0
y3 = y1
x4 = w
y4 = y1
else:
x3 = int(-b/k)
y3 = 0 # (x3,y3)在图上边
y4 = h
x4 = int((h-b)/k) # (x4,y4)在图下边
if x4 > w:
x4 = w
y4 = int(k*w+b) # (x4,y4)在图右边
elif x4 < 0:
x4 = 0
y4 = b # (x4,y4)在图左边
return (x3,y3),(x4,y4)