You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.4 KiB

4 months ago
import numpy as np
import matplotlib.pyplot as plt
import calc_way
import get_data
import calc_slope_line
import cv2
import model
model = model.Model()
alpha = model.alpha
beta = model.beta
img_path = r'C:\Users\Administrator\Desktop\BYD\20250520\frame__9600_2_yolo.jpg'
txt_name = "C:\\Users\\Administrator\\Desktop\\BYD\\20250520\\frame_9600_2.jpg_zuobiao.txt"
output_image = "9600_2.jpg"
x_bot, y_bot, x_top, y_top = get_data.get_data(txt_name)
x_bot = np.array(x_bot)
y_bot = np.array(y_bot)
x_top = np.array(x_top)
y_top = np.array(y_top)
slope_top, intercept_top ,r2 = calc_slope_line.linear_regression(x_top,y_top)
x_zero, y_zero = calc_way.calc_zeros()
x_zero = np.array(x_zero)
y_zero = np.array(y_zero)
slope_zero, intercept_zero,r2_zero = calc_slope_line.linear_regression(x_zero,y_zero)
# 绘制原始数据
plt.scatter(x_zero,y_zero, color='blue', label='orgin')
# 绘制拟合线
y_pred = slope_zero * x_zero + intercept_zero
plt.plot(x_zero, y_pred, color='red', label='fix')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()
# # 绘制原始数据
# plt.scatter(x_top,y_top, color='blue', label='orgin')
#
# # 绘制拟合线
# y_pred = slope_top * x_top + intercept_top
# plt.plot(x_top, y_pred, color='red', label='fix')
#
# plt.xlabel('X')
# plt.ylabel('Y')
# plt.legend()
# plt.show()
# 1. 读取图像(替换为你的图片路径)
image = cv2.imread(img_path) # 默认读取BGR格式
# 检查图像是否成功加载
if image is None:
print("Error: 无法读取图像,请检查路径!")
exit()
point1 = (int(x_zero[0]), int(960-y_zero[0]))
point2 = (int(x_zero[-1]), int(960-y_zero[-1]))
cv2.line(image, point1, point2, (0, 255, 0), 1)
Z = 0
for i in range(len(x_bot)):
k = calc_slope_line.get_k(alpha,beta,x_bot[i],y_bot[i])
print(f"{i+1}个点的斜率为:{k}")
b = calc_slope_line.get_b(x_bot[i],y_bot[i],k)
print(f"{i+1}个点的截距为:{b}")
x = (intercept_top - b) / (k - slope_top)
y = k * x + b
print(f"{i+1}个点的坐标为为:{x_bot[i],y_bot[i]}")
print(f"{i+1}个点对应的上沿点为:{x,y}")
Zw = calc_way.calc_height(x_bot[i],y_bot[i], x, y, alpha, beta)
Xw, Yw = calc_way.calc_distance(x_bot[i],y_bot[i], alpha, beta)
print(f"{i+1}个点的坐标为为:{Xw, Yw }")
point1 = (x_bot[i], 960-y_bot[i])
point2 = (int(x), 960-int(y))
cv2.line(image, point1, point2, (0, 255, 0), 1)
# text = f"Xw,Yw:{int(Xw),int(Yw)},Zw:{int(Zw)}"
# position = (x_bot[i], 960-y_bot[i]) # 文字左下角坐标 (x, y)
# font = cv2.FONT_HERSHEY_SIMPLEX # 字体类型
# font_scale = 1 # 字体大小
# color = (0, 255, 255) # 黄色BGR格式
# thickness = 1 # 文字粗细
# cv2.putText(image, text, position, font, font_scale, color, thickness, cv2.LINE_AA)
print("路沿高度", Zw)
Z = Z + Zw
print(f"平均路沿高度为:{Z/len(x_bot)}")
text = f"avg_height:{Z/len(x_bot)}"
position = (50, 200) # 文字左下角坐标 (x, y)
font = cv2.FONT_HERSHEY_SIMPLEX # 字体类型
font_scale = 1 # 字体大小
color = (0, 255, 255) # 黄色BGR格式
thickness = 1 # 文字粗细
# 3. 在图像上绘制文字
cv2.putText(image, text, position, font, font_scale, color, thickness, cv2.LINE_AA)
cv2.imshow("Image with Line", image)
cv2.waitKey(0) # 按任意键关闭窗口
cv2.destroyAllWindows()
cv2.imwrite(output_image, image)