已知三角形的中点[row, col](高的中点),高与水平轴的夹角angle_label类别(0-35,每一类比前一类多10°),高width_label,底边长bottom。计算三角形的三个角点的坐标。
def triangle_loc(row, col, angle, height, bottom):
"""
根据 row, col, angle_label, width_label, bottom获取三角坐标
:param row: 抓取点坐标
:param col:
:param angle: 抓取角类别 0-35
:param height: 抓取宽度
:param bottom: 抓取三角的底
:return: [[row1, col1], [row2, col2], [row3, col4]]
"""
assert angle <= 35, 'angle out of index'
angle *= (10 / 180) * math.pi # 弧度
k = math.tan(angle) # 斜率
dx_abs = height / 2 / pow(k**2 + 1, 0.5)
dy_abs = abs(k * dx_abs)
# 计算三角形定点的坐标
if angle <= math.pi:
if k >= 0: # 一 象限
pt1_x = col + dx_abs
pt1_y = row - dy_abs
else: # 二象限
pt1_x = col - dx_abs
pt1_y = row - dy_abs
else:
if k >= 0: # 三 象限
pt1_x = col - dx_abs
pt1_y = row + dy_abs
else: # 四 象限
pt1_x = col + dx_abs
pt1_y = row + dy_abs
# 底边中点的坐标
pt_b_x = 2 * col - pt1_x
pt_b_y = 2 * row - pt1_y
# 底边两个端点的坐标
if k == 0:
dx_b_abs = 0
dy_b_abs = bottom / 2
else:
k_b = -1 / k # 底边的斜率
dx_b_abs = bottom / 2 / pow(k_b ** 2 + 1, 0.5)
dy_b_abs = dx_b_abs * k_b
pt2_x = pt_b_x + dx_b_abs
pt2_y = pt_b_y - dy_b_abs
pt3_x = pt_b_x - dx_b_abs
pt3_y = pt_b_y + dy_b_abs
triangle = np.array([
[pt1_y, pt1_x],
[pt2_y, pt2_x],
[pt3_y, pt3_x]
])
return triangle.astype(int)