diff --git a/Visual measurement-straight/py/Calibration.py b/Visual measurement-straight/py/Calibration.py index 639982f..3acf218 100644 --- a/Visual measurement-straight/py/Calibration.py +++ b/Visual measurement-straight/py/Calibration.py @@ -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) \ No newline at end of file diff --git a/Visual measurement-straight/py/__pycache__/calc_way.cpython-312.pyc b/Visual measurement-straight/py/__pycache__/calc_way.cpython-312.pyc index 5c13dfc..53b9b8f 100644 Binary files a/Visual measurement-straight/py/__pycache__/calc_way.cpython-312.pyc and b/Visual measurement-straight/py/__pycache__/calc_way.cpython-312.pyc differ diff --git a/Visual measurement-straight/py/__pycache__/model.cpython-312.pyc b/Visual measurement-straight/py/__pycache__/model.cpython-312.pyc index 1d30cdc..de20a22 100644 Binary files a/Visual measurement-straight/py/__pycache__/model.cpython-312.pyc and b/Visual measurement-straight/py/__pycache__/model.cpython-312.pyc differ diff --git a/Visual measurement-straight/py/calc_way.py b/Visual measurement-straight/py/calc_way.py index c5d7f4c..b6025fc 100644 --- a/Visual measurement-straight/py/calc_way.py +++ b/Visual measurement-straight/py/calc_way.py @@ -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 diff --git a/Visual measurement-straight/py/corrected.jpg b/Visual measurement-straight/py/corrected.jpg new file mode 100644 index 0000000..bed735b Binary files /dev/null and b/Visual measurement-straight/py/corrected.jpg differ diff --git a/Visual measurement-straight/py/model.py b/Visual measurement-straight/py/model.py index a7dd362..eb1b4db 100644 --- a/Visual measurement-straight/py/model.py +++ b/Visual measurement-straight/py/model.py @@ -2,24 +2,48 @@ 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 - self.limit_intercept = 452.86565535382374 + self.limit_intercept = 452.86565535382374 \ No newline at end of file diff --git a/Visual measurement-straight/py/output.jpg b/Visual measurement-straight/py/output.jpg new file mode 100644 index 0000000..02ebaa1 Binary files /dev/null and b/Visual measurement-straight/py/output.jpg differ