|
|
|
@ -101,29 +101,40 @@ def find_corners(image_path, columns, rows):
|
|
|
|
|
|
|
|
|
|
# 方法:固定值 - 列值
|
|
|
|
|
corners_reshaped[:, column_index] = fixed_value - corners_reshaped[:, column_index]
|
|
|
|
|
# print(corners_reshaped)
|
|
|
|
|
print(corners_reshaped)
|
|
|
|
|
return corners_reshaped
|
|
|
|
|
|
|
|
|
|
def fun_test(x,cls,corners):
|
|
|
|
|
print("标定开始")
|
|
|
|
|
# print(corners)
|
|
|
|
|
error = 0
|
|
|
|
|
error_y = 0
|
|
|
|
|
model = cls()
|
|
|
|
|
f = model.f
|
|
|
|
|
H = model.H - 9
|
|
|
|
|
gamma = math.atan(1 / (math.tan(x[0]) * math.tan(x[1])))
|
|
|
|
|
seta = math.atan(1 / math.sqrt(pow(math.tan(x[1]), 2) + 1 / pow(math.tan(x[0]), 2)))
|
|
|
|
|
column = 0
|
|
|
|
|
k = 0
|
|
|
|
|
for index, value in enumerate(corners):
|
|
|
|
|
if index % 11 == 10:
|
|
|
|
|
column += 1
|
|
|
|
|
index += 1
|
|
|
|
|
k += 1
|
|
|
|
|
continue
|
|
|
|
|
Xw, Yw = calc_way.calc_distance(value[0], value[1], x[0], x[1])
|
|
|
|
|
Xw1, Yw1 = calc_way.calc_distance(corners[index+1][0], corners[index+1][1], x[0], x[1])
|
|
|
|
|
print(f"第{index}个点")
|
|
|
|
|
print(f"Xw: {Xw}, Yw: {Yw}")
|
|
|
|
|
print(f"Xw1: {Xw1}, Yw1: {Yw1}")
|
|
|
|
|
d2 = math.sqrt((Xw1 - Xw) ** 2 + (Yw1 - Yw) ** 2)
|
|
|
|
|
print(f"两点距离为:{d2:.2f}")
|
|
|
|
|
error = error + abs(d2 - 60)
|
|
|
|
|
return error/80
|
|
|
|
|
d_y = abs(570-60*k-50+Yw)
|
|
|
|
|
error_y = error_y + d_y
|
|
|
|
|
print(f"平均误差为:{error/80:.2f}")
|
|
|
|
|
print(f"errpr_y:{error/80:.2f}")
|
|
|
|
|
return error/80 + error_y/80
|
|
|
|
|
|
|
|
|
|
def get_result_test(cls,corners):
|
|
|
|
|
params = cls,corners
|
|
|
|
@ -134,10 +145,57 @@ def get_result_test(cls,corners):
|
|
|
|
|
args=params,
|
|
|
|
|
# method='Nelder-Mead', # 或 'trust-constr'
|
|
|
|
|
method='L-BFGS-B', bounds=bounds,
|
|
|
|
|
tol=1e-12, # 高精度容差
|
|
|
|
|
options={'gtol': 1e-12, 'maxiter': 1000}
|
|
|
|
|
tol=1e-6, # 高精度容差
|
|
|
|
|
options={'gtol': 1e-6, 'maxiter': 1000}
|
|
|
|
|
)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def undistort_image(image_path, camera_matrix, dist_coeffs):
|
|
|
|
|
# 读取图像
|
|
|
|
|
img = cv2.imread(image_path)
|
|
|
|
|
|
|
|
|
|
# 获取图像尺寸
|
|
|
|
|
h, w = img.shape[:2]
|
|
|
|
|
|
|
|
|
|
# 优化相机矩阵
|
|
|
|
|
new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(
|
|
|
|
|
camera_matrix, dist_coeffs, (w, h), 1, (w, h))
|
|
|
|
|
|
|
|
|
|
# 使用undistort
|
|
|
|
|
dst = cv2.undistort(img, camera_matrix, dist_coeffs, None, new_camera_matrix)
|
|
|
|
|
|
|
|
|
|
# 裁剪图像(使用roi)
|
|
|
|
|
x, y, w, h = roi
|
|
|
|
|
dst = dst[y:y + h, x:x + w]
|
|
|
|
|
|
|
|
|
|
return dst
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 示例使用
|
|
|
|
|
# 假设你已经通过相机标定获得了相机矩阵和畸变系数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# result = calibrate(r"C:\Users\Administrator\Desktop\BYD\20250711\*.jpg",11,8,60)
|
|
|
|
|
# camera_matrix = result[0]
|
|
|
|
|
# dist_coeffs = result[1]
|
|
|
|
|
# corrected_img = undistort_image(r"C:\Users\Administrator\Desktop\BYD\20250711\frame_2100_3.jpg", camera_matrix, dist_coeffs)
|
|
|
|
|
# cv2.imwrite("corrected.jpg", corrected_img)
|
|
|
|
|
# result = get_result_test(model.Model,find_corners("corrected.jpg",11,8))
|
|
|
|
|
# print(result)
|
|
|
|
|
# find_corners("corrected.jpg",11,8)
|
|
|
|
|
# x_zeros,y_zeros = calc_way.calc_zeros_yto0(-200)
|
|
|
|
|
# print(x_zeros,y_zeros)
|
|
|
|
|
# # 读取图像
|
|
|
|
|
# img = cv2.imread("corrected.jpg")
|
|
|
|
|
#
|
|
|
|
|
# # 定义两点坐标
|
|
|
|
|
# pt1 = (int(x_zeros[0]), int(960-y_zeros[0]))
|
|
|
|
|
# pt2 = (int(x_zeros[-1]), int(960-y_zeros[-1]))
|
|
|
|
|
#
|
|
|
|
|
# # 画红色线条,粗细为3
|
|
|
|
|
# cv2.line(img, pt1, pt2, (0, 0, 255), 3)
|
|
|
|
|
#
|
|
|
|
|
# # 保存结果
|
|
|
|
|
# cv2.imwrite("output.jpg", img)
|