Skip to content

Commit

Permalink
add new parameters in yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
mfxox committed Aug 29, 2018
1 parent 0caa0ec commit c1f2b7d
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 94 deletions.
42 changes: 26 additions & 16 deletions ILCC/LM_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ def back_project(r_t, img, corners_in_img_arr, corners_in_pcd_arr):

cv2.polylines(img, [proj_corners], 0, (0, 255, 255), 1, lineType=16)
for i in xrange(proj_corners.shape[0]):
1)
cv2.circle(img, (proj_corners[i][0], proj_corners[i][1]), s2[i], tuple(c2[i].tolist()), 1)
cv2.putText(img, str(i + 1), (proj_corners[i][0], proj_corners[i][1]), cv2.FONT_HERSHEY_SIMPLEX, .3,
(255, 165, 0),0)
Expand Down Expand Up @@ -306,16 +305,16 @@ def cal_ext_paras(ind_ls = (np.arange(1, 21)).tolist()):
if params['camera_type'] == "panoramic":
print "Optimize the extrinsic parameters with panoramic model."
for i in ls:
imgfile = "img/" + str(i).zfill(4) + "." + params['image_format']
cords_file = "output/img_corners/" + str(i).zfill(4) + "_img_corners.txt"
imgfile = "img/" + str(i).zfill(params['file_name_digits']) + "." + params['image_format']
cords_file = "output/img_corners/" + str(i).zfill(params['file_name_digits']) + "_img_corners.txt"
corners_in_img_arr = np.genfromtxt(cords_file, delimiter=",").astype(np.int32)
# make sure the corners are counted start from left lower
if np.linalg.norm(np.array(corners_in_img_arr[0]) - np.array([0, H])) > np.linalg.norm(
np.array(corners_in_img_arr[-1]) - np.array([0, H])):
print imgfile + " is counted in reversed order"
corners_in_img_arr = np.flipud(corners_in_img_arr)

pcd_result_file = "output/pcd_seg/" + str(i).zfill(4) + "_pcd_result.pkl"
pcd_result_file = "output/pcd_seg/" + str(i).zfill(params['file_name_digits']) + "_pcd_result.pkl"
# print imgfile
with open(os.path.abspath(pcd_result_file), "r") as f:
pcd_result_ls = cPickle.load(f)
Expand Down Expand Up @@ -367,15 +366,17 @@ def cal_ext_paras(ind_ls = (np.arange(1, 21)).tolist()):
if params['back_proj_corners']:
# for i in range(start, end):
for i in ls:
imgfile = "img/" + str(i).zfill(4) + "." + params['image_format']
cords_file = "output/img_corners/" + str(i).zfill(4) + "_img_corners.txt"
imgfile = os.path.join(params['base_dir'], "img/") + str(i).zfill(params['file_name_digits']) + "." + \
params['image_format']
cords_file = os.path.join(params['base_dir'], "output/img_corners/") + str(i).zfill(
params['file_name_digits']) + "_img_corners.txt"
corners_in_img_arr = np.genfromtxt(cords_file, delimiter=",").astype(np.int32)
# make sure the corners are counted start from left lower
if np.linalg.norm(np.array(corners_in_img_arr[0]) - np.array([0, H])) > np.linalg.norm(
np.array(corners_in_img_arr[-1]) - np.array([0, H])):
corners_in_img_arr = np.flipud(corners_in_img_arr)

pcd_result_file = "output/pcd_seg/" + str(i).zfill(4) + "_pcd_result.pkl"
pcd_result_file = "output/pcd_seg/" + str(i).zfill(params['file_name_digits']) + "_pcd_result.pkl"
# print imgfile
with open(os.path.abspath(pcd_result_file), "r") as f:
pcd_result_ls = cPickle.load(f)
Expand All @@ -391,21 +392,25 @@ def cal_ext_paras(ind_ls = (np.arange(1, 21)).tolist()):
t2 = pcd_result_ls[3].reshape(1, 3)
corners_in_pcd_arr = np.dot(np.dot(rot2.T, corner_arr.T).T - t2 + t1, rot1)
ret = back_project(res.x, cv2.imread(imgfile), corners_in_img_arr, corners_in_pcd_arr)
save_file = "output/" + str(i).zfill(4) + "_cal_backproj." + params['image_format']
save_file = "output/" + str(i).zfill(params['file_name_digits']) + "_cal_backproj." + params[
'image_format']
cv2.imwrite(save_file, ret)
elif params['camera_type'] == "perspective":
print "Optimize the extrinsic parameters with perspective model."
for i in ls:
imgfile = "img/" + str(i).zfill(4) + "." + params['image_format']
cords_file = "output/img_corners/" + str(i).zfill(4) + "_img_corners.txt"
imgfile = os.path.join(params['base_dir'], "img/") + str(i).zfill(params['file_name_digits']) + "." + \
params['image_format']
cords_file = os.path.join(params['base_dir'], "output/img_corners/") + str(i).zfill(
params['file_name_digits']) + "_img_corners.txt"
corners_in_img_arr = np.genfromtxt(cords_file, delimiter=",").astype(np.int32)
# make sure the corners are counted start from left lower
if np.linalg.norm(np.array(corners_in_img_arr[0]) - np.array([0, H])) > np.linalg.norm(
np.array(corners_in_img_arr[-1]) - np.array([0, H])):
print imgfile + " is counted in reversed order"
corners_in_img_arr = np.flipud(corners_in_img_arr)

pcd_result_file = "output/pcd_seg/" + str(i).zfill(4) + "_pcd_result.pkl"
pcd_result_file = os.path.join(params['base_dir'], "output/pcd_seg/") + str(i).zfill(
params['file_name_digits']) + "_pcd_result.pkl"
# print imgfile
with open(os.path.abspath(pcd_result_file), "r") as f:
pcd_result_ls = cPickle.load(f)
Expand Down Expand Up @@ -444,7 +449,7 @@ def cal_ext_paras(ind_ls = (np.arange(1, 21)).tolist()):
print "initial guess by UPnP:", initial_guess
print
print "final extrinsic parameters:"
cal_file_name = time.strftime("%Y%m%d_%H%M%S_cali_result.txt")
cal_file_name = os.path.join(params['base_dir'], time.strftime("%Y%m%d_%H%M%S_cali_result.txt"))
np.savetxt(cal_file_name, res.x, delimiter=',')
print "refined by LM : ", res.x, " unit: [rad,rad,rad,m,m,m]. The result is Saved to ", cal_file_name
print "unit converted : ", convert2_ang_cm(res.x), "unit: [deg,deg,deg,cm,cm,cm]"
Expand All @@ -459,15 +464,18 @@ def cal_ext_paras(ind_ls = (np.arange(1, 21)).tolist()):
if params['back_proj_corners']:
# for i in range(start, end):
for i in ls:
imgfile = "img/" + str(i).zfill(4) + "." + params['image_format']
cords_file = "output/img_corners/" + str(i).zfill(4) + "_img_corners.txt"
imgfile = os.path.join(params['base_dir'], "img/") + str(i).zfill(params['file_name_digits']) + "." + \
params['image_format']
cords_file = os.path.join(params['base_dir'], "output/img_corners/") + str(i).zfill(
params['file_name_digits']) + "_img_corners.txt"
corners_in_img_arr = np.genfromtxt(cords_file, delimiter=",").astype(np.int32)
# make sure the corners are counted start from left lower
if np.linalg.norm(np.array(corners_in_img_arr[0]) - np.array([0, H])) > np.linalg.norm(
np.array(corners_in_img_arr[-1]) - np.array([0, H])):
corners_in_img_arr = np.flipud(corners_in_img_arr)

pcd_result_file = "output/pcd_seg/" + str(i).zfill(4) + "_pcd_result.pkl"
pcd_result_file = os.path.join(params['base_dir'], "output/pcd_seg/") + str(i).zfill(
params['file_name_digits']) + "_pcd_result.pkl"
# print imgfile
with open(os.path.abspath(pcd_result_file), "r") as f:
pcd_result_ls = cPickle.load(f)
Expand All @@ -484,7 +492,9 @@ def cal_ext_paras(ind_ls = (np.arange(1, 21)).tolist()):
corners_in_pcd_arr = np.dot(np.dot(rot2.T, corner_arr.T).T - t2 + t1, rot1)
ret = back_project(res.x, cv2.imread(imgfile), corners_in_img_arr, corners_in_pcd_arr)
# ret = back_project(initial_guess, cv2.imread(imgfile), corners_in_img_arr, corners_in_pcd_arr)
save_file = "output/" + str(i).zfill(4) + "_cal_backproj." + params['image_format']
save_file = os.path.join(params['base_dir'], "output/") + str(i).zfill(
params['file_name_digits']) + "_cal_backproj." + params[
'image_format']
cv2.imwrite(save_file, ret)

else:
Expand Down
6 changes: 3 additions & 3 deletions ILCC/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def default_params():
'''
default_params_yaml = open("config.yaml", "r")
params = yaml.load(default_params_yaml)
params['image_format'] = get_img_format()
params['image_format'] = get_img_format(params['base_dir'])
return params


def get_img_format():
file_ls = os.listdir("img")
def get_img_format(base_dir):
file_ls = os.listdir(os.path.join(base_dir, "img"))
for file in file_ls:
ext = file.split(".")[-1]
if ext in ["png", "jpg"]:
Expand Down
19 changes: 12 additions & 7 deletions ILCC/img_corners_est.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def get_corner_coords(imagefilename, backend=params['backend'], size=make_tuple(
size = tuple((np.array(boardSize).astype(np.int32) - 1).flatten())
cv2.drawChessboardCorners(img, size, np_imagePoints.astype(np.float32), 1)
if save_figure:
save_imagefilename = "output/img_corners/" + (imagefilename.split("/")[-1]).split(".")[
0] + "_detected_corners" + "." + params['image_format']
save_imagefilename = os.path.join(params['base_dir'], "output")+"/img_corners/" + \
(imagefilename.split("/")[-1]).split(".")[
0] + "_detected_corners" + "." + params['image_format']
cv2.imwrite(save_imagefilename, img)
print "Image with detected_corners is saved in " + save_imagefilename
if show_figure:
Expand Down Expand Up @@ -82,16 +83,20 @@ def get_corner_coords(imagefilename, backend=params['backend'], size=make_tuple(
def detect_img_corners():
ls = np.arange(1, 21).tolist()
# ls = [20]
if os.path.isdir("output/img_corners"):
shutil.rmtree("output/img_corners")
os.makedirs("output/img_corners")
img_corner_path = os.path.join(base_dir, "output/img_corners/")
if os.path.isdir(img_corner_path):
shutil.rmtree(img_corner_path)
os.makedirs(img_corner_path)
for i in ls:
try:
imagefilename = "img/" + str(i).zfill(4) + "." + params['image_format']
imagefilename = os.path.join(base_dir,
"img", str(i).zfill(params['file_name_digits']) + "." + params['image_format'])
print imagefilename
corner_points = get_corner_coords(imagefilename)

# print corner_points
save_points_filename = "output/img_corners/" + str(i).zfill(4) + "_img_corners" + ".txt"
save_points_filename = img_corner_path + str(i).zfill(
params['file_name_digits']) + "_img_corners" + ".txt"
np.savetxt(save_points_filename, corner_points, delimiter=",")
except:
continue
Expand Down
17 changes: 9 additions & 8 deletions ILCC/pcd_corners_est.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def show_pcd_ndarray(array_data, color_arr=[0, 255, 0]):


# determine whether a segment is the potential chessboard's point cloud
def is_marker(file_full_path, range_res, points_num_th=400):
def is_marker(file_full_path, range_res, points_num_th=250):
# result = False
jdcs_collection = cPickle.load(open(file_full_path, 'rb'))

Expand Down Expand Up @@ -650,7 +650,7 @@ def cost_func_for_opt_mini(theta_t, transed_pcd, marker_full_data_arr, gray_zone
cost = 0
for row in arr:
if polygon_path.contains_point(row[:2]):
if gray_zone[0] < row[3] < gray_zone[1]:
if gray_zone[0] < row[params['intensity_col_ind']] < gray_zone[1]:
y.append(0.5)
continue
else:
Expand All @@ -667,7 +667,7 @@ def cost_func_for_opt_mini(theta_t, transed_pcd, marker_full_data_arr, gray_zone
else:
color = 0

estimated_color = (np.sign(row[3] - gray_zone[1]) + 1) / 2
estimated_color = (np.sign(row[params['intensity_col_ind']] - gray_zone[1]) + 1) / 2
if estimated_color != color:
cost += (min(abs(row[0] - x_grid_arr)) + min(abs(row[1] - y_grid_arr)))
y.append(color)
Expand Down Expand Up @@ -812,7 +812,7 @@ def opt_min(param_ls, initial_guess=np.zeros(3).tolist()):


# utilize the defined functions to get the chessboard's corners for single frame point cloud
def run(csv_path, save_folder_path="output/pcd_seg/", size=marker_size):
def run(csv_path, save_folder_path=os.path.join(params['base_dir'], "output/pcd_seg/"), size=marker_size):
if not_segmented:
seg_pcd(csv_path, save_folder_path)
parts = csv_path.split("/")
Expand Down Expand Up @@ -841,7 +841,7 @@ def run(csv_path, save_folder_path="output/pcd_seg/", size=marker_size):

# calculate the rotate angle in xoy palne around the z axis
if 1:
low_intes, high_intens = get_gray_thre(marker_full_data_arr_fitted[:, 3])
low_intes, high_intens = get_gray_thre(marker_full_data_arr_fitted[:, params['intensity_col_ind']])
print "low_intes,high_intes:", low_intes, high_intens
rate = 2
gray_zone = np.array([((rate - 1) * low_intes + high_intens), (low_intes + (rate - 1) * high_intens)]) / rate
Expand Down Expand Up @@ -878,12 +878,13 @@ def run(csv_path, save_folder_path="output/pcd_seg/", size=marker_size):

# for multiple processing
def main_for_pool(i):
pcd_file = "pcd/" + str(i).zfill(params["file_name_digits"]) + ".csv"
pcd_file = os.path.join(params['base_dir'], "pcd/") + str(i).zfill(params["file_name_digits"]) + ".csv"
print pcd_file
try:
result = run(csv_path=pcd_file)
print result
save_file_path = "output/pcd_seg/" + str(i).zfill(params["file_name_digits"]) + "_pcd_result.pkl"
save_file_path = os.path.join(params['base_dir'], "output/pcd_seg/") + str(i).zfill(
params["file_name_digits"]) + "_pcd_result.pkl"
with open(os.path.abspath(save_file_path), 'w') as file:
file.truncate()
cPickle.dump(result, file)
Expand All @@ -897,7 +898,7 @@ def main_for_pool(i):

# main function for detecting corners from pcd files in the folder
def detect_pcd_corners():
file_ls = os.listdir("pcd")
file_ls = os.listdir(os.path.join(params['base_dir'], "pcd"))
pcd_ls = []
for file in file_ls:
if file.find("csv") > -1:
Expand Down
Loading

0 comments on commit c1f2b7d

Please sign in to comment.