def read_text_list_from_file(text_file_fname, # type: str ): # type: (...) -> list with open(text_file_fname, 'r') as f: text_list = f.readlines() for i in range(len(text_list)): text_list[i] = string_utils.remove_newlines(text_list[i]) return text_list
def from_ascii_file( cls, ascii_file # type: str ): # type: (...) -> Spectrum with open(ascii_file) as f: content = f.readlines() wavelengths = [] spectral_data = [] wavelengths_and_reflectances = content[3:] for text in wavelengths_and_reflectances: text = string_utils.remove_newlines(text) text = ' '.join(text.split()) w, r = text.split() wavelengths.append(float(w)) spectral_data.append(float(r)) spectral_data = np.array(spectral_data) wavelengths = np.array(wavelengths) spectrum = Spectrum() spectrum.set_wavelength_units(WAVELENGTH_UNITS) spectrum.set_wavelengths(wavelengths) spectrum.set_spectral_data(spectral_data) spectrum.set_spectrum_units(SPECTRUM_UNITS) return spectrum
def read_calibrated_external_camera_parameters(self, calibrated_external_params_file # type: str ): # type: (...) -> dict # TODO, this is duplicated in pix4d utils and can probably be removed here. external_params = {} with open(calibrated_external_params_file, 'r') as f: lines = f.readlines() cleaned_up_lines = [string_utils.remove_newlines(x) for x in lines][1:] for file_params in cleaned_up_lines: fname, x, y, z, omega, phi, kappa = file_params.split(" ") external_params[fname] = [float(x), float(y), float(z), float(omega), float(phi), float(kappa)] return external_params
def read_pix4d_calibrated_internal_camera_parameters(internal_params_cam_file, # type: str search_text="Pix4D", # type: str ): # type: (...) -> list """ Reads the internal parameters camera file generated by pix4d and return a list of dictionaries, which contain values for each camera's internal parameters accordin to the Pix4d documentation. For each camera, the following parameters are contained in the dictionary: 'F': focal length in millimeters 'K1': Radial distortion parameter 1 'K2': Radial distortion parameter 2 'K3': Radial distortion parameter 3 'Px': Principal point in the x pixel direction 'Py': Principal point in the y pixel direction 'T1': Tangential distortion parameter 1 'T2': Tangential distortion parameter 2 'fpa_height_mm': focal plane height, in millimeters 'fpa_width_mm': focal plane width, in millimeters :param internal_params_cam_file: :param search_text: :return: list of dictionaries containing the internal camera parameters """ with open(internal_params_cam_file, 'r') as f: lines = f.readlines() lines = [string_utils.remove_newlines(x) for x in lines] new_camera_param_start_locations = [] for i, line in enumerate(lines): if search_text in line: new_camera_param_start_locations.append(i) new_camera_param_end_locations = new_camera_param_start_locations[1:] new_camera_param_end_locations.append(i) camera_params_list = [] for i in range(len(new_camera_param_start_locations)): internal_params_dict = {} text_block = lines[new_camera_param_start_locations[i] + 1:new_camera_param_end_locations[i]] text_block = list(filter(lambda a: a != '', text_block)) for txt in text_block: if txt.startswith("#"): if "Focal Length mm" in txt: fpa_width_mm, fpa_height_mm = txt[txt.rfind(" ") + 1:].replace("mm", "").split("x") internal_params_dict["fpa_width_mm"] = float(fpa_width_mm) internal_params_dict["fpa_height_mm"] = float(fpa_height_mm) else: pass else: key, val = txt.split(" ") internal_params_dict[key] = float(val) camera_params_list.append(internal_params_dict) return camera_params_list
def read_p_matrices(pmatrix_txt_file # type: str ): # type: (...) -> dict # TODO: create working example for this or remove, it doesn't seem to be used by any other methods in the project. p_matricies = {} with open(pmatrix_txt_file, 'r') as f: lines = f.readlines() for line in lines: line_no_linesep = string_utils.remove_newlines(line.strip()) line_fields = line_no_linesep.split(' ') params = np.asarray(line_fields[1:], dtype=float) fname = line_fields[0] p_matricies[fname] = params return p_matricies
def read_calibrated_external_camera_parameters(calibrated_external_params_file # type: str ): # type: (...) -> dict """ Reads external orientation parameters from a file for an entire pix4d collect :param calibrated_external_params_file: external orientation parameters file to read :return: dictionary of external orientation parameters. Key is the base image file name value is a numpy array of length 6 containing x, y, z, omega, phi, kappa Units are specified by the collect's projection (x, y, z), and degrees for omega, phi, kappa (roll, pitch, yaw) """ external_params = {} with open(calibrated_external_params_file, 'r') as f: lines = f.readlines() cleaned_up_lines = [string_utils.remove_newlines(x) for x in lines][1:] for file_params in cleaned_up_lines: fname, x, y, z, omega, phi, kappa = file_params.split(" ") external_params[fname] = [float(x), float(y), float(z), float(omega), float(phi), float(kappa)] return external_params
def read_calibrated_camera_parameters(calibrated_params_file # type: str ): # type: (...) -> dict """ This method reads the calibrated camera parameters for an entire pix4d collect and returns a dictionary The dictionary keys are the base image file name. The values are another dictionary that have the following key / value pairs: 'camera_matrix_k': 3x3 numpy array containing the camera k matrix 'camera_position_t': ndarray of length 3 containing the camera position 't' 'camera_rotation_r': 3x3 ndarray containing the camera's rotation matrix 'n_x_pixels': number of x pixels 'n_y_pixels': number of y pixels 'radial_distortion': numpy array containing 3 radial distortion parameters 'tangential_distortion': numpy array containing 2 tangential distortion parameters. :param calibrated_params_file: :return: dictionary of image filenames and calibrated parameters """ calibrated_params = {} with open(calibrated_params_file, 'r') as f: lines = f.readlines() cleaned_up_lines = [string_utils.remove_newlines(x) for x in lines] params = cleaned_up_lines[8:] per_file_params = [params[x:x + 10] for x in range(0, len(params), 10)] for file_params in per_file_params: fname, image_w, image_h = file_params[0].split(" ") tmp_list = np.array(file_params[1:4]) camera_matrix_k = np.array([list(map(float, row.split())) for row in tmp_list]) tmp_list = np.array(file_params[4:5]) radial_distortion = np.array(np.squeeze([list(map(float, row.split())) for row in tmp_list])) tmp_list = np.array(file_params[5:6]) tangential_distortion = np.array(np.squeeze([list(map(float, row.split())) for row in tmp_list])) tmp_list = np.array(file_params[6:7]) camera_position_t = np.array([list(map(float, row.split())) for row in tmp_list]) tmp_list = np.array(file_params[7:10]) camera_rotation_r = np.array([list(map(float, row.split())) for row in tmp_list]) fname_params = {"n_x_pixels": int(image_w), "n_y_pixels": int(image_h), "camera_matrix_k": camera_matrix_k, "radial_distortion": radial_distortion, "tangential_distortion": tangential_distortion, "camera_position_t": camera_position_t, "camera_rotation_r": camera_rotation_r} calibrated_params[fname] = fname_params return calibrated_params
def read_pix4d_calibrated_internal_camera_parameters(self, internal_params_cam_file, # type: str search_text="Pix4D" # type: str ): # type: (...) -> list # TODO: This is duplicated in pix4d_utils and can probably be removed here. with open(internal_params_cam_file, 'r') as f: lines = f.readlines() lines = [string_utils.remove_newlines(x) for x in lines] new_camera_param_start_locations = [] for i, line in enumerate(lines): if search_text in line: new_camera_param_start_locations.append(i) new_camera_param_end_locations = new_camera_param_start_locations[1:] new_camera_param_end_locations.append(i) camera_params_list = [] for i in range(len(new_camera_param_start_locations)): internal_params_dict = {} text_block = lines[new_camera_param_start_locations[i] + 1:new_camera_param_end_locations[i]] text_block = list(filter(lambda a: a != '', text_block)) for txt in text_block: if txt.startswith("#"): if "Focal Length mm" in txt: fpa_width_mm, fpa_height_mm = txt[txt.rfind(" ") + 1:].replace("mm", "").split("x") internal_params_dict["fpa_width_mm"] = float(fpa_width_mm) internal_params_dict["fpa_height_mm"] = float(fpa_height_mm) else: pass else: key, val = txt.split(" ") internal_params_dict[key] = float(val) camera_params_list.append(internal_params_dict) return camera_params_list