# 圆形 --> 椭圆 孙赢利
##########################################################################################################################
##########################################################################################################################
# # 判断点是否在椭圆内部
def point_inside_ellipse(point, center, axes, rotation_angle):
x, y = point[0] - center[0], point[1] - center[1] # 将坐标系转换为相对于椭圆中心的新坐标系
angle = math.radians(rotation_angle) # 将旋转角度转换为弧度
x_rotated = x * math.cos(angle) + y * math.sin(angle) # 根据旋转角度计算旋转后的x坐标
y_rotated = -x * math.sin(angle) + y * math.cos(angle) # 根据旋转角度计算旋转后的y坐标
a, b = axes[0], axes[1] # 提取椭圆的半长轴a和半短轴b
return (x_rotated / a) ** 2 + (y_rotated / b) ** 2 <= 1 # 判断点是否在旋转椭圆的内部
# 接待判断模块
if len(new_frame_tracks_results) > 0 and len(new_frame_tracks_results[0]) > 0:
for a in range(len(new_frame_tracks_results[0])):
output = new_frame_tracks_results[0][a]
bbox = output[0:4]
id = output[4]
cls = output[5]
conf = output[6]
bbox_x = int((output[0] + output[2]) / 2)
bbox_y = int((output[1] + output[3]) / 2)
bbox_w = int(output[2] - output[0])
bbox_h = int(output[3] - output[1])
##########################################################################################################################
# 设置椭圆的中心坐标和轴长度, 旋转角度
center = (bbox_x, bbox_y - int(bbox_h/2) + int(bbox_h/12)) # 中心点
if bbox_w > bbox_h:
axes = (int(bbox_w), int(bbox_w/2)) # 轴长度
if bbox_w < bbox_h or bbox_w == bbox_h:
axes = (int(2.25 * bbox_w), int(1.175 * bbox_w)) # 轴长度
w_distance = abs(bbox_x - im0s_w/2) + 0.000001
h_distance = abs((bbox_y - bbox_h/2 + bbox_h/12) - im0s_h) + 0.000001
tan_h_w = w_distance / h_distance
x = math.atan(tan_h_w) # 旋转角度
if x < 0:
x = x + math.pi
if int(bbox_x) > int(im0s_w/2):
angle = math.degrees(x)
elif int(bbox_x) < int(im0s_w/2):
angle = 90 - math.degrees(x)
else:
angle = 0
# 旋转角度
angle = int(angle / 4)
# 设置椭圆的起始和终止角度
startAngle = 0
endAngle = 360
# 设置椭圆的颜色和线宽
color = (0, 255, 0) # BGR颜色格式,这里是绿色
thickness = 2
# 在图像上绘制椭圆
cv2.ellipse(im0, center, axes, angle, startAngle, endAngle, color, thickness)
for b in range(len(new_frame_tracks_results[0])):
# 这里注意,除了自己不和自己比,要重新开始
if b == a:
continue
output = new_frame_tracks_results[0][b]
b_bbox = output[0:4]
b_id = output[4]
b_cls = output[5]
b_conf = output[6]
b_bbox_x = int((output[0] + output[2]) / 2)
b_bbox_y = int((output[1] +output[3]) / 2)
b_bbox_w = int(output[2] - output[0])
b_bbox_h = int(output[3] - output[1])
# 判断点是否在椭圆内,即判断距离是否相近
if point_inside_ellipse([b_bbox_x, int(b_bbox_y - b_bbox_h/2)], center, axes, angle):
start_point = (b_bbox_x, int(b_bbox_y - b_bbox_h/2))
end_point = (bbox_x, bbox_y - int(bbox_h/2))
color = (255, 0, 0) # 线条颜色(BGR格式)
thickness = 2 # 线条宽度
cv2.line(im0, start_point, end_point, color, thickness)
##########################################################################################################################
##############################################################################################################################
###########################################################################################################################
二维平面 以椭圆模拟某点附近2米的范围
最新推荐文章于 2024-07-06 17:45:57 发布