|
|
|
@ -6,6 +6,8 @@ import cameramodel
|
|
|
|
|
import os
|
|
|
|
|
import math
|
|
|
|
|
import calc_way
|
|
|
|
|
import measure_lib
|
|
|
|
|
import time
|
|
|
|
|
def needs_correction(dist_coeffs, error_threshold=0.5):
|
|
|
|
|
k1, k2, p1, p2, k3 = dist_coeffs.ravel()
|
|
|
|
|
if (abs(k1) > 0.1 or abs(k2) > 0.01 or abs(p1) > 0.005 or
|
|
|
|
@ -216,24 +218,24 @@ def fun(x,cameraModel,corners):
|
|
|
|
|
d2 = math.sqrt((Xw1 - Xw) ** 2 + (Yw1 - Yw) ** 2)
|
|
|
|
|
print(f"两点距离为:{d2:.2f}")
|
|
|
|
|
error = error + abs(d2 - 60)
|
|
|
|
|
d_y = abs(Yw - (-90 - 60 * k + 53))
|
|
|
|
|
d_y = abs(Yw - (-90 - 60 * k + cameraModel.position_offset_y))
|
|
|
|
|
error_y = error_y + d_y
|
|
|
|
|
d_x = abs(Xw - (40 + 60 * k1 + 540))
|
|
|
|
|
d_x = abs(Xw - (40 + 60 * k1 + cameraModel.position_offset_x))
|
|
|
|
|
error_x = error_x + d_x
|
|
|
|
|
print(f"d_x: {Xw - (40 + 60 * k1 + 540)}, d_y: {Yw - (-90 - 60 * k + 53)}")
|
|
|
|
|
print(f"d_x: {Xw - (40 + 60 * k1 + 507)}, d_y: {Yw - (-90 - 60 * k + 32)}")
|
|
|
|
|
print(f"平均误差为:{error/80:.2f}")
|
|
|
|
|
print(f"errpr_y:{error_y/80:.2f}")
|
|
|
|
|
print(f"errpr_x:{error_x/80:.2f}")
|
|
|
|
|
print(gamma)
|
|
|
|
|
# print(gamma)
|
|
|
|
|
return 1*error/80 + 1*error_y/80 + 1*error_x/80
|
|
|
|
|
|
|
|
|
|
def get_result(cls, corners):
|
|
|
|
|
params = cls, corners
|
|
|
|
|
def get_result(cameraModel, corners):
|
|
|
|
|
params = cameraModel, corners
|
|
|
|
|
bounds = [(0.1, 1.7), (0.1, 1.7), [-0.5, 0.5],[1000,1020]]
|
|
|
|
|
# bounds = [(0.1, 1.7), (0.1, 1.7)]
|
|
|
|
|
result = minimize(
|
|
|
|
|
fun,
|
|
|
|
|
x0=[1.0, 0.7, 0,1007],
|
|
|
|
|
x0=[cameraModel.rotation_alpha, cameraModel.rotation_beta, 0, cameraModel.height],
|
|
|
|
|
args=params,
|
|
|
|
|
# method='Nelder-Mead', # 或 'trust-constr'
|
|
|
|
|
method='L-BFGS-B', bounds=bounds,
|
|
|
|
@ -284,6 +286,7 @@ def calibrate_Extrinsic_Parameters(image_path, config, columns, rows):
|
|
|
|
|
result = get_result(cameraModel, find_corners(image_path, columns, rows))
|
|
|
|
|
print(result)
|
|
|
|
|
if result.success:
|
|
|
|
|
print(f"Success to Extrinsic_Parameters {image_path}")
|
|
|
|
|
cameraModel.update_parameter("rotation_alpha", result.x[0])
|
|
|
|
|
cameraModel.update_parameter("rotation_beta", result.x[1])
|
|
|
|
|
cameraModel.update_parameter("rotation_camera", result.x[2])
|
|
|
|
@ -295,7 +298,7 @@ def calibrate_Extrinsic_Parameters(image_path, config, columns, rows):
|
|
|
|
|
|
|
|
|
|
def check_Extrinsic_Parameters(image_path, output_path, config):
|
|
|
|
|
cameraModel = cameramodel.CameraModel(config)
|
|
|
|
|
x_zeros, y_zeros = calc_way.calc_zeros_yto0(cameraModel, 53)
|
|
|
|
|
x_zeros, y_zeros = calc_way.calc_zeros_yto0(cameraModel, 32)
|
|
|
|
|
|
|
|
|
|
# 读取图像
|
|
|
|
|
img = cv2.imread(image_path)
|
|
|
|
@ -305,9 +308,9 @@ def check_Extrinsic_Parameters(image_path, output_path, config):
|
|
|
|
|
pt2 = (int(x_zeros[-1]), int(960 - y_zeros[-1]))
|
|
|
|
|
|
|
|
|
|
# 画红色线条,粗细为3
|
|
|
|
|
cv2.line(img, pt1, pt2, (0, 0, 255), 3)
|
|
|
|
|
cv2.line(img, pt1, pt2, (0, 255, 255), 3)
|
|
|
|
|
|
|
|
|
|
x_zeros, y_zeros = calc_way.calc_zeros_xto0(cameraModel, 540)
|
|
|
|
|
x_zeros, y_zeros = calc_way.calc_zeros_xto0(cameraModel, 507)
|
|
|
|
|
# slope_Xw, intercept_Xw, r2_Xw = calc_slope_line.linear_regression(x_zeros,y_zeros)
|
|
|
|
|
# print(f"slope_Xw: {slope_Xw}")
|
|
|
|
|
#
|
|
|
|
@ -319,7 +322,7 @@ def check_Extrinsic_Parameters(image_path, output_path, config):
|
|
|
|
|
cv2.line(img, pt1, pt2, (0, 0, 255), 3)
|
|
|
|
|
# 保存结果
|
|
|
|
|
|
|
|
|
|
x_zeros, y_zeros = calc_way.calc_zeros_yto0(cameraModel, -600 + 53)
|
|
|
|
|
x_zeros, y_zeros = calc_way.calc_zeros_yto0(cameraModel, -600 + 32)
|
|
|
|
|
# 定义两点坐标
|
|
|
|
|
pt1 = (int(x_zeros[0]), int(960 - y_zeros[0]))
|
|
|
|
|
pt2 = (int(x_zeros[-1]), int(960 - y_zeros[-1]))
|
|
|
|
@ -327,17 +330,34 @@ def check_Extrinsic_Parameters(image_path, output_path, config):
|
|
|
|
|
# 画红色线条,粗细为3
|
|
|
|
|
cv2.line(img, pt1, pt2, (0, 255, 255), 3)
|
|
|
|
|
|
|
|
|
|
x_zeros, y_zeros = calc_way.calc_zeros_xto0(cameraModel, 540 + 800)
|
|
|
|
|
x_zeros, y_zeros = calc_way.calc_zeros_xto0(cameraModel, 507 + 800)
|
|
|
|
|
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, 255, 255), 3)
|
|
|
|
|
cv2.line(img, pt1, pt2, (0, 0, 255), 3)
|
|
|
|
|
#
|
|
|
|
|
# # x_zero, y_zero = calc_way.calc_zeros_xandyto0(cameraModel, 784, 53)
|
|
|
|
|
# # print(x_zero, y_zero)
|
|
|
|
|
cv2.circle(img, (int(188.70974194838968), 960-int(179.17183436687336)), radius=10, color=(0, 0, 255), thickness=-1)
|
|
|
|
|
|
|
|
|
|
cv2.imwrite(output_path, img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# image_path = r"C:\Users\Administrator\Desktop\BYD\Visual measurement_model\Visual measurement\img\calibration\origin_img\*.jpg"
|
|
|
|
|
# output_fold =r"C:\Users\Administrator\Desktop\BYD\Visual measurement_model\Visual measurement\img\calibration\undistor_img"
|
|
|
|
|
# config = r"updated_config.json"
|
|
|
|
|
# # calibrate_and_undistort(image_path,output_fold,config,11,8,60)
|
|
|
|
|
# calibrate_and_undistort(image_path,output_fold,config,11,8,60)
|
|
|
|
|
# calibrate_Extrinsic_Parameters("corrected.jpg", config, 11, 8)
|
|
|
|
|
# # check_Extrinsic_Parameters("corrected.jpg","output.jpg",config)
|
|
|
|
|
# check_Extrinsic_Parameters("corrected.jpg", "output.jpg", config)
|
|
|
|
|
|
|
|
|
|
# t = time.time()
|
|
|
|
|
# cameraModel = cameramodel.CameraModel(config)
|
|
|
|
|
# x_zero, y_zero = calc_way.calc_zeros_xandyto0(cameraModel, 784, 32)
|
|
|
|
|
# print(x_zero, y_zero)
|
|
|
|
|
# print(time.time() - t)
|
|
|
|
|
# result = measure_lib.vs_measurement(r"C:\Users\Administrator\Desktop\BYD\0718\new415.379489173224.txt", position=784, config_path=config )
|
|
|
|
|
# print(result)
|
|
|
|
|
# img = cv2.imread(r"C:\Users\Administrator\Desktop\BYD\0718\new415.379489173224.jpg")
|
|
|
|
|
# cv2.line(img, (int(229.67793558711742), 960-int(151.71034206841367)), (result[3],result[4]), (0, 255, 255), 3)
|
|
|
|
|
# cv2.imwrite(r"test.jpg", img)
|