def time_end(info_dict: InfoDict, time_name): info_dict.time_dict[time_name + '_end'] = time.time() if (time_name + '_diff' not in info_dict.time_dict.keys()) or ( info_dict.time_dict[time_name + '_diff'] == -1.): info_dict.time_dict[time_name + '_diff'] = info_dict.time_dict[ time_name + '_end'] - info_dict.time_dict[time_name + '_begin'] else: info_dict.time_dict[time_name + '_diff'] += info_dict.time_dict[ time_name + '_end'] - info_dict.time_dict[time_name + '_begin']
def load_input_data(input_dir_path): """ 读取dcm数据 """ info_dict = InfoDict() info_dict.data_path = input_dir_path image_raw, info_dict = load_data(info_dict) # 保证图像为512x512大小 image_raw = judge_resize_512(image_raw, 'linear') info_dict.image_shape_raw = image_raw.shape classes_value = [7] * len(image_raw) info_dict.classes_value = classes_value return image_raw, info_dict
def store_info_dict(info_dict=InfoDict(), input_dict={}, **kwargs): assertor.type_assert(input_dict, dict) assertor.type_assert(kwargs, dict) for temp_dict_key, temp_dict_value in input_dict.items(): info_dict[temp_dict_key] = temp_dict_value for temp_dict_key, temp_dict_value in kwargs.items(): info_dict[temp_dict_key] = temp_dict_value return info_dict
def parser_argv_to_info_dict(info_dict: InfoDict, file_pos: str, is_classify: bool): """ 解析输入参数存到info_dict中 整体流程: 1.判别输入是否一致(都为key value pair或者顺序调用) 2.判别是key value pair还是顺序调用 3.调用相应的解析 :param info_dict: :param is_classify: :return: """ argv_list = sys.argv[1:] assertor.greater_or_equal_assert(len(argv_list), 1) # 1.判别输入是否一致(都为key value pair或者顺序调用) if not _is_input_consistant(argv_list): raise ValueError( 'The input mix with keyvalue pair and none keyvalue pair parameter' ) # 2.判别是key value pair还是顺序调用 if _is_keyvalue_pair(argv_list): # 3.调用相应的解析 input_dict = parser_keyvalue_argv_as_dict(argv_list, file_pos) else: # 3.调用相应的解析 input_dict = parser_argv_as_dict(argv_list, file_pos, is_classify) info_dict = store_info_dict(info_dict, input_dict) if 'full_body_classify' in file_pos: info_dict.alg_name = 'full_body_classify' else: info_dict.alg_name = file_pos.split('get_')[-1].replace( '_contours.py', '') return info_dict
def interp_2d_pack(image_block, info_dict: InfoDict, kernal_size=0): '''2d image interpolation package :param image_block: 3d volume, format: zyx :param info_dict: should have info_dict.spacing_list, info_dict.target_spacing :param kind: interpolation methods, cv2.INTER_LINEAR cv2.INTER_NEAREST cv2.INTER_CUBIC(slow) :param kernel_size: used in median blurring for interpolation results. if 0, then no blurring operation :return image_interp: resized image volume ,its dtype is same with image_block :return info_dict: info_dict ''' if len(image_block.shape) != 3: # 输入图像的shape错误, 返回错误码 Error.exit(ErrorCode.process_input_shape_error) if not "target_spacing" in info_dict: Error.exit(ErrorCode.process_module_error) raw_dtype = image_block.dtype image_block = check_for_cv2_dtype(image_block, raw_dtype) spacing = [info_dict.spacing_list[1], info_dict.spacing_list[2]] pixel = np.array(spacing, dtype=np.float32) / np.array( info_dict.target_spacing, dtype=np.float32) new_x = int(image_block.shape[2] * pixel[0]) new_y = int(image_block.shape[1] * pixel[1]) info_dict.image_shape_before_interp = image_block.shape image_interp = np.zeros((image_block.shape[0], new_x, new_y), np.float32) for i in range(image_block.shape[0]): image_one = interp_2d_yx(image_block[i, :, :], new_x, new_y, info_dict.interp_kind, kernal_size) image_interp[i, :, :] = image_one return image_interp, info_dict
def time_begin(info_dict: InfoDict, time_name): info_dict.time_dict[time_name + '_begin'] = time.time()
def check_large_image_zyx(input_array: np.ndarray, info_dict: InfoDict, old_spacing=None, threshold_shape=(512, 512)): """ 功能:解决超过512, 512图像问题进行插值 整体流程: 1. 得到原始图像大小并进行判断 2. 如果小于512, 直接返回 3. 如果大于512, 计算两个维度的比值,选取大的比值计算出新的图像大小 4. 计算出新的spacing, 存回info_dict 5. 对原图像进行插值得到返回图像 """ if old_spacing is None: old_spacing = info_dict.spacing_list.copy() # 断言保证 assertor.array_x_dims_assert( input_array, 3, error_code=ErrorCode.process_input_shape_error, msg='Assert pos: interp_large_image_zyx') assertor.equal_assert(len(old_spacing), 3, error_code=ErrorCode.process_input_shape_error, msg='Assert pos: interp_large_image_zyx') assertor.equal_assert(len(threshold_shape), 2, error_code=ErrorCode.process_input_shape_error, msg='Assert pos: interp_large_image_zyx') # 开关保护 if not info_dict.use_large_image_check: return input_array, info_dict # 1. 得到原始图像大小并进行判断 input_z, input_y, input_x = input_array.shape thre_y, thre_x = threshold_shape # 2. 如果小于512, 直接返回 if (input_x <= thre_x) and (input_y <= thre_y): return input_array, info_dict # 3. 如果大于512, 计算两个维度的比值,选取大的比值计算出新的图像大小 ratio_x = input_x / thre_x ratio_y = input_y / thre_y ratio = max(ratio_x, ratio_y) new_x = int(input_x / ratio) new_y = int(input_y / ratio) # 4. 计算出新的spacing, 存回info_dict new_spacing = trans_spacing_by_shape(old_spacing[1:], [input_y, input_x], [new_y, new_x]) info_dict.spacing_list[1:] = new_spacing info_dict.large_raw_spacing = old_spacing # 5. 对原图像进行插值得到返回图像 rst_array = np.zeros((input_z, new_y, new_x), dtype=input_array.dtype) for i in range(input_z): # 注意,这里需要反着调用 rst_array[i, :, :] = interp_2d_yx(input_array[i, :, :], new_x, new_y) info_dict.large_image_shape = (input_z, input_y, input_x) info_dict.image_shape_raw = rst_array.shape return rst_array, info_dict
def anti_check_large_image_zyx(input_array: np.ndarray, info_dict: InfoDict): """ 功能: 反处理图像大于512,512的情况 整体流程: 1. 得到返回图像大小和原始图像大小并进行判断 2. 如果为None, 直接返回 3. 计算出新的spacing, 存回info_dict 4. 对原图像进行插值得到返回图像 """ # 断言保证 assertor.array_x_dims_assert( input_array, 3, error_code=ErrorCode.process_input_shape_error, msg='Assert pos: interp_large_image_zyx') # 开关保护 if not info_dict.use_large_image_check: return input_array, info_dict # 1. 得到返回图像大小和原始图像大小并进行判断 large_image_shape = info_dict.large_image_shape # 2. 如果为None, 直接返回 if large_image_shape is None: return input_array, info_dict # 3. 计算出新的spacing, 存回info_dict large_raw_spacing = info_dict.large_raw_spacing if large_raw_spacing is None: print('large_image_shape exist, but large_raw_spacing is None') Error.exit(ErrorCode.process_data_type_error) info_dict.spacing_list = large_raw_spacing # 4. 对原图像进行插值得到返回图像 rst_array = np.zeros(large_image_shape, dtype=input_array.dtype) input_z, input_y, input_x = large_image_shape for i in range(input_z): # 注意,这里需要反着调用 rst_array[i, :, :] = interp_2d_yx(input_array[i, :, :], input_x, input_y, kind=CV2_interp_type.nearest) info_dict.image_shape_raw = large_image_shape return rst_array, info_dict # if __name__ == '__main__': # info_dict = InfoDict() # info_dict.spacing_list = [1,1,1] # input_array = np.zeros((15,1024,512), dtype=np.int16) # out_array, info_dict = check_large_image_zyx(input_array, info_dict) # print(input_array.shape) # print(out_array.shape) # print(info_dict.spacing_list) # print(info_dict.large_image_shape) # print(info_dict.large_raw_spacing) # print() # new_array, info_dict = anti_check_large_image_zyx(out_array, info_dict) # print(input_array.shape) # print(out_array.shape) # print(new_array.shape) # print(info_dict.spacing_list) # print(info_dict.large_image_shape) # print(info_dict.large_raw_spacing)