master
Paul597 3 months ago
parent 33e1954f10
commit 3fd2b82a79

@ -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)

@ -118,9 +118,9 @@ def calc_zeros_yto0(val):
unique_solutions.append(sol)
x = []
y = []
# print("找到的解:")
print("找到的解:")
for sol in unique_solutions:
# print(f"x = {sol[0]:.4f}, y = {sol[1]:.4f}")
print(f"x = {sol[0]:.4f}, y = {sol[1]:.4f}")
x.append(sol[0])
y.append(sol[1])
return x, y

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 KiB

@ -2,23 +2,47 @@ import numpy as np
from scipy.optimize import minimize
#保存相机内参、外参等参数
# class Model:
# def __init__(self):
# # 内参
# self.K = np.array([[801.8319, 0, 647.8920],
# [0, 801.7619, 532],
# [0, 0, 1]])
# self.f = 3.6
# self.H = 1019.0000170167332
# self.dx = self.f / self.K[0, 0]
# self.dy = self.f / self.K[1, 1]
# self.u0 = self.K[0, 2]
# self.v0 = self.K[1, 2]
# # 外参
# self.alpha = 0.7072338025822084
# self.beta = 0.9077237961986776
# # 位置修正
# self.y = -70
# self.x = 22
# # 数据截断线
# self.limit_slope = 0.3259949467095897
# self.limit_intercept = 452.86565535382374
class Model:
def __init__(self):
# 内参
self.K = np.array([[801.8319, 0, 647.8920],
[0, 801.7619, 532],
[0, 0, 1]])
self.K = np.array([[1.08632711e+03, 0.00000000e+00, 6.57410789e+02],
[0.00000000e+00, 1.08644329e+03, 960-4.83660145e+02],
[0.00000000e+00, 0.00000000e+00, 1.00000000e+00]])
self.f = 3.6
self.H = 1019.0000170167332
self.H = 1009.0000170167332
self.dx = self.f / self.K[0, 0]
self.dy = self.f / self.K[1, 1]
self.u0 = self.K[0, 2]
self.v0 = self.K[1, 2]
# 外参
self.alpha = 0.7072338025822084
self.beta = 0.9077237961986776
# self.alpha = 8.749e-01
# self.beta = 7.664e-01
self.alpha = 9.478e-01
self.beta = 8.111e-01
# 位置修正
self.y = -70
self.y = -50
self.x = 22
# 数据截断线
self.limit_slope = 0.3259949467095897

Binary file not shown.

After

Width:  |  Height:  |  Size: 307 KiB

Loading…
Cancel
Save