def infer(start: list):
    """
    :param start: Initial point
    :return: Moving position, the index of maximum confidence direction, Current termination probability
    """
    max_z = re_spacing_img.shape[0]
    max_x = re_spacing_img.shape[1]
    max_y = re_spacing_img.shape[2]

    cut_size = 9
    spacing_x = spacing[0]
    spacing_y = spacing[1]
    spacing_z = spacing[2]

    center_x_pixel = get_spacing_res2(start[0], spacing_x, resize_factor[1])
    center_y_pixel = get_spacing_res2(start[1], spacing_y, resize_factor[2])
    center_z_pixel = get_spacing_res2(start[2], spacing_z, resize_factor[0])

    left_x = center_x_pixel - cut_size
    right_x = center_x_pixel + cut_size
    left_y = center_y_pixel - cut_size
    right_y = center_y_pixel + cut_size
    left_z = center_z_pixel - cut_size
    right_z = center_z_pixel + cut_size

    new_patch = np.zeros(
        (cut_size * 2 + 1, cut_size * 2 + 1, cut_size * 2 + 1))

    if not (left_x < 0 or right_x < 0 or left_y < 0 or right_y < 0
            or left_z < 0 or right_z < 0 or left_x >= max_x or right_x >= max_x
            or left_y >= max_y or right_y >= max_y or left_z >= max_z
            or right_z >= max_z):
        for ind in range(left_z, right_z + 1):
            src_temp = re_spacing_img[ind].copy()
            new_patch[ind - left_z] = src_temp[left_y:right_y + 1,
                                               left_x:right_x + 1]
        input_data = data_preprocess(new_patch)

        inputs = input_data.to(device)
        outputs = infer_model(inputs.float())

        outputs = outputs.view((len(input_data), max_points + 1))
        outputs_1 = outputs[:, :len(outputs[0]) - 1]
        outputs_2 = outputs[:, -1]

        outputs_1 = torch.nn.functional.softmax(outputs_1, 1)
        indexs = np.argsort(outputs_1.cpu().detach().numpy()[0])[::-1]
        curr_prob = prob_terminates(outputs_1,
                                    max_points).cpu().detach().numpy()[0]
        curr_r = outputs_2.cpu().detach().numpy()[0]
        sx, sy, sz = get_shell(max_points, curr_r)
        return [sx, sy, sz], indexs, curr_r, curr_prob
    else:
        return None
Beispiel #2
0
def creat_data(max_points, pi_folder, spacing_path, gap_size):
    '''

    :param max_points:
    :param pi_folder:
    :param spacing_path:
    :param gap_size:
    :return:
    '''

    pre_ind_list = []
    next_ind_list = []
    radials_list = []
    patch_name = []
    print("processing dataset ", pi_folder)
    pi = os.path.basename(pi_folder)
    image_pre_fix = pi_folder
    icafem = iCafe(pi_folder)
    file_name = image_pre_fix + '/TH_' + pi + '.tif'
    src_array = sitk.GetArrayFromImage(
        sitk.ReadImage(file_name, sitk.sitkFloat32))

    #re_spacing_img, curr_spacing, resize_factor = resample(src_array, np.array([spacing_z, spacing_x, spacing_y]),
    #                                                       np.array([0.5, 0.5, 0.5]))
    re_spacing_img = src_array

    curr_mean = np.array([0, 0, 0])

    rotate_prob = 0.3

    #v is vessel id
    for v in range(icafem.snakelist.NSnakes):
        if icafem.snakelist[v].length < 10:
            continue
        print("processing vessel %d" % v)
        center = np.array([
            icafem.snakelist[v][j].pos.lst()
            for j in range(icafem.snakelist[v].NP)
        ])

        radials_data = np.array([
            icafem.snakelist[v][j].rad for j in range(icafem.snakelist[v].NP)
        ])
        start_ind = 0
        end_ind = icafem.snakelist[v].NP
        #print("start ind:", start_ind)
        #print("end ind:", end_ind)
        counter = 0
        last_center_x_pixel = -1
        last_center_y_pixel = -1
        last_center_z_pixel = -1
        # for j in range(start_ind, start_ind+1):
        for j in range(start_ind, end_ind):
            if j % gap_size == 0:
                if j % 10 == 0:
                    print('\rj:', j, end='')
                center_x = icafem.snakelist[v][j].pos.x
                center_y = icafem.snakelist[v][j].pos.y
                center_z = icafem.snakelist[v][j].pos.z

                org_x_pixel = center_x
                org_y_pixel = center_y
                org_z_pixel = center_z

                if org_x_pixel != last_center_x_pixel or org_y_pixel != last_center_y_pixel or org_z_pixel != last_center_z_pixel:
                    #print("last:",[last_center_x_pixel,last_center_y_pixel,last_center_z_pixel])
                    #print("curr:",[org_x_pixel, org_y_pixel, org_z_pixel])
                    last_center_x_pixel = org_x_pixel
                    last_center_y_pixel = org_y_pixel
                    last_center_z_pixel = org_z_pixel

                    radial = icafem.snakelist[v][j].rad

                    if radial == 0:
                        continue

                    record_set = set()
                    curr_conv = np.array([[radial * 0.25, 0.0, 0.0],
                                          [0.0, radial * 0.25, 0.0],
                                          [0.0, 0.0, radial * 0.25]])

                    # To then obtain an off-centerline sample, point x is translated using a random shift sampled from a 3D normal distribution with μ = 0.0, σ = 0.25r

                    for k in range(10):
                        off_center_x, off_center_y, off_center_z = np.random.multivariate_normal(
                            mean=curr_mean, cov=curr_conv, size=1).T
                        center_x_new = center_x + off_center_x[0]
                        center_y_new = center_y + off_center_y[0]
                        center_z_new = center_z + off_center_z[0]
                        center_x_pixel = center_x_new
                        center_y_pixel = center_y_new
                        center_z_pixel = center_z_new

                        while True:
                            if (center_x_pixel != org_x_pixel
                                    or center_y_pixel != org_y_pixel
                                    or center_z_pixel != org_z_pixel) and (
                                        center_x_pixel, center_y_pixel,
                                        center_z_pixel) not in record_set:
                                record_set.add((center_x_pixel, center_y_pixel,
                                                center_z_pixel))
                                break
                            else:
                                off_center_x, off_center_y, off_center_z = np.random.multivariate_normal(
                                    mean=curr_mean, cov=curr_conv, size=1).T
                                center_x_new = int(
                                    round(center_x + off_center_x[0]))
                                center_y_new = int(
                                    round(center_y + off_center_y[0]))
                                center_z_new = int(
                                    round(center_z + off_center_z[0]))

                                center_x_pixel = center_x_new
                                center_y_pixel = center_y_new
                                center_z_pixel = center_z_new

                        new_radial_ind = get_new_radial_ind(
                            center, [center_x_new, center_y_new, center_z_new])

                        new_radial = radials_data[new_radial_ind]

                        sx, sy, sz = get_shell(max_points, new_radial)
                        shell_arr = np.zeros((len(sx), 3))
                        for s_ind in range(len(sx)):
                            shell_arr[s_ind][0] = sx[s_ind]
                            shell_arr[s_ind][1] = sy[s_ind]
                            shell_arr[s_ind][2] = sz[s_ind]

                        pre_ind, next_ind = get_pre_next_point_ind(
                            center, radials_data, new_radial_ind)
                        if pre_ind != -1 and next_ind != -1:
                            cut_size = 9

                            left_x = int(center_x_pixel - cut_size)
                            right_x = int(center_x_pixel + cut_size)
                            if left_x < 0 or right_x >= icafem.SM:
                                continue
                            left_y = int(center_y_pixel - cut_size)
                            right_y = int(center_y_pixel + cut_size)
                            if left_x < 0 or right_y >= icafem.SN:
                                continue
                            left_z = int(center_z_pixel - cut_size)
                            right_z = int(center_z_pixel + cut_size)

                            new_src_arr = np.zeros(
                                (cut_size * 2 + 1, cut_size * 2 + 1,
                                 cut_size * 2 + 1))
                            for ind in range(left_z, right_z + 1):
                                if ind < 0 or ind >= icafem.SZ:
                                    continue
                                src_temp = re_spacing_img[ind].copy()
                                if src_temp[left_y:right_y + 1,
                                            left_x:right_x +
                                            1].shape != (cut_size * 2 + 1,
                                                         cut_size * 2 + 1):
                                    print('patch size mismatch')
                                    continue
                                new_src_arr[ind -
                                            left_z] = src_temp[left_y:right_y +
                                                               1,
                                                               left_x:right_x +
                                                               1]

                            if np.random.uniform() <= rotate_prob:
                                curr_c = [
                                    center_x_new, center_y_new, center_z_new
                                ]
                                new_src_arr, new_pre_cood, new_next_cood = rotate_augmentation(
                                    new_src_arr,
                                    pre_ind,
                                    next_ind,
                                    curr_c,
                                    center,
                                    angle_x=(-60. / 360 * 2. * np.pi,
                                             60. / 360 * 2. * np.pi),
                                    angle_y=(-60. / 360 * 2. * np.pi,
                                             60. / 360 * 2. * np.pi),
                                    angle_z=(-60. / 360 * 2. * np.pi,
                                             60. / 360 * 2. * np.pi))
                                p = [
                                    new_pre_cood[0], new_pre_cood[1],
                                    new_pre_cood[2]
                                ]
                                pre_sim = find_closer_point_angle(
                                    shell_arr, p, curr_c)
                                p = [
                                    new_next_cood[0], new_next_cood[1],
                                    new_next_cood[2]
                                ]
                                next_sim = find_closer_point_angle(
                                    shell_arr, p, curr_c)
                                pre_ind_list.append(pre_sim)
                                next_ind_list.append(next_sim)
                                radials_list.append(new_radial)
                            else:
                                pre_x = center[pre_ind][0]
                                pre_y = center[pre_ind][1]
                                pre_z = center[pre_ind][2]

                                next_x = center[next_ind][0]
                                next_y = center[next_ind][1]
                                next_z = center[next_ind][2]

                                curr_c = [
                                    center_x_new, center_y_new, center_z_new
                                ]
                                p = [pre_x, pre_y, pre_z]
                                pre_sim = find_closer_point_angle(
                                    shell_arr, p, curr_c)
                                p = [next_x, next_y, next_z]
                                next_sim = find_closer_point_angle(
                                    shell_arr, p, curr_c)
                                pre_ind_list.append(pre_sim)
                                next_ind_list.append(next_sim)
                                radials_list.append(new_radial)
                            folder_path = r'D:\LiChen\LATTEpatch/careii_patch/offset/point_' + str(
                                max_points) + '_gp_' + str(
                                    gap_size) + '/' + 'd' + pi
                            if not os.path.exists(folder_path):
                                os.makedirs(folder_path)
                            record_name = 'careii_patch/offset/point_' + str(
                                max_points
                            ) + '_gp_' + str(
                                gap_size
                            ) + '/' + 'd' + pi + '/' + 'd_' + pi + '_' + 'v_' + str(
                                v) + '_' + 'patch_%d_' % counter + str(
                                    k) + '.nii.gz'
                            org_name = r'D:\LiChen\LATTEpatch/' + record_name
                            out = sitk.GetImageFromArray(new_src_arr)
                            sitk.WriteImage(out, org_name)
                            patch_name.append(record_name)

                    counter += 1

    return pre_ind_list, next_ind_list, radials_list, patch_name
def creat_data(max_points, path_name, spacing_path, gap_size, save_num):
    spacing_info = np.loadtxt(spacing_path, delimiter=",", dtype=np.float32)
    pre_ind_list = []
    next_ind_list = []
    radials_list = []
    patch_name = []
    i = save_num
    print("processing dataset %d" % i)
    image_pre_fix = path_name + '0' + str(i) + '/' + 'image' + '0' + str(i)
    file_name = image_pre_fix + '.nii.gz'
    src_array = sitk.GetArrayFromImage(
        sitk.ReadImage(file_name, sitk.sitkFloat32))

    spacing_x = spacing_info[i][0]
    spacing_y = spacing_info[i][1]
    spacing_z = spacing_info[i][2]
    re_spacing_img, curr_spacing, resize_factor = resample(
        src_array, np.array([spacing_z, spacing_x, spacing_y]),
        np.array([0.5, 0.5, 0.5]))
    for v in range(4):
        print("processing vessel %d" % v)
        reference_path = path_name + '0' + str(i) + '/' + 'vessel' + str(
            v) + '/' + 'reference.txt'
        txt_data = np.loadtxt(reference_path, dtype=np.float32)
        center = txt_data[..., 0:3]

        radials_data = txt_data[..., 3]
        start_ind = get_start_ind(center, radials_data)

        end_ind = get_end_ind(center, radials_data)

        print("start ind:", start_ind)
        print("end ind:", end_ind)
        counter = 0

        last_center_x_pixel = -1
        last_center_y_pixel = -1
        last_center_z_pixel = -1

        for j in range(start_ind, end_ind + 1):
            # for j in range(start_ind, start_ind + 1):
            if j % gap_size == 0:
                print('j:', j)
                center_x = center[j][0]
                center_y = center[j][1]
                center_z = center[j][2]

                org_x_pixel = get_spacing_res2(center_x, spacing_x,
                                               resize_factor[1])
                org_y_pixel = get_spacing_res2(center_y, spacing_y,
                                               resize_factor[2])
                org_z_pixel = get_spacing_res2(center_z, spacing_z,
                                               resize_factor[0])

                if org_x_pixel != last_center_x_pixel or org_y_pixel != last_center_y_pixel or org_z_pixel != last_center_z_pixel:
                    print("last:", [
                        last_center_x_pixel, last_center_y_pixel,
                        last_center_z_pixel
                    ])
                    print("curr:", [org_x_pixel, org_y_pixel, org_z_pixel])
                    last_center_x_pixel = org_x_pixel
                    last_center_y_pixel = org_y_pixel
                    last_center_z_pixel = org_z_pixel

                    radial = radials_data[j]

                    pre_ind, next_ind = get_pre_next_point_ind(
                        center, radials_data, j)
                    if pre_ind != -1 and next_ind != -1:
                        pre_x = center[pre_ind][0]
                        pre_y = center[pre_ind][1]
                        pre_z = center[pre_ind][2]

                        next_x = center[next_ind][0]
                        next_y = center[next_ind][1]
                        next_z = center[next_ind][2]

                        sx, sy, sz = get_shell(max_points, radial)
                        shell_arr = np.zeros((len(sx), 3))
                        for s_ind in range(len(sx)):
                            shell_arr[s_ind][0] = sx[s_ind]
                            shell_arr[s_ind][1] = sy[s_ind]
                            shell_arr[s_ind][2] = sz[s_ind]

                        center_x_pixel = get_spacing_res2(
                            center_x, spacing_x, resize_factor[1])
                        center_y_pixel = get_spacing_res2(
                            center_y, spacing_y, resize_factor[2])
                        center_z_pixel = get_spacing_res2(
                            center_z, spacing_z, resize_factor[0])

                        curr_c = [center_x, center_y, center_z]
                        p = [pre_x, pre_y, pre_z]
                        pre_sim = find_closer_point_angle(shell_arr, p, curr_c)
                        p = [next_x, next_y, next_z]
                        next_sim = find_closer_point_angle(
                            shell_arr, p, curr_c)

                        pre_ind_list.append(pre_sim)
                        next_ind_list.append(next_sim)
                        radials_list.append(radial)

                        cut_size = 9

                        left_x = center_x_pixel - cut_size
                        right_x = center_x_pixel + cut_size
                        left_y = center_y_pixel - cut_size
                        right_y = center_y_pixel + cut_size
                        left_z = center_z_pixel - cut_size
                        right_z = center_z_pixel + cut_size

                        new_src_arr = np.zeros(
                            (cut_size * 2 + 1, cut_size * 2 + 1,
                             cut_size * 2 + 1))
                        for ind in range(left_z, right_z + 1):
                            src_temp = re_spacing_img[ind].copy()
                            new_src_arr[ind -
                                        left_z] = src_temp[left_y:right_y + 1,
                                                           left_x:right_x + 1]

                        folder_path = './patch_data/centerline_patch/no_offset/point_' + str(
                            max_points) + '_gp_' + str(
                                gap_size) + '/' + 'd' + str(i)
                        if not os.path.exists(folder_path):
                            os.makedirs(folder_path)
                        record_name = 'centerline_patch/no_offset/point_' + str(
                            max_points
                        ) + '_gp_' + str(gap_size) + '/' + 'd' + str(
                            i) + '/' + 'd_' + str(i) + '_' + 'v_' + str(
                                v) + '_' + 'patch_%d' % counter + '.nii.gz'

                        org_name = './patch_data/' + record_name
                        out = sitk.GetImageFromArray(new_src_arr)
                        sitk.WriteImage(out, org_name)
                        patch_name.append(record_name)
                        counter += 1

    return pre_ind_list, next_ind_list, radials_list, patch_name
def creat_data(max_points, path_name, spacing_path, gap_size, save_num):
    '''

    :param max_points:
    :param path_name:
    :param spacing_path:
    :param gap_size:
    :param save_num:
    :return:
    '''
    spacing_info = np.loadtxt(spacing_path, delimiter=",", dtype=np.float32)
    pre_ind_list = []
    next_ind_list = []
    radials_list = []
    patch_name = []
    i = save_num
    print("processing dataset %d" % i)
    image_pre_fix = path_name + '0' + str(i) + '/' + 'image' + '0' + str(i)
    file_name = image_pre_fix + '.nii.gz'
    src_array = sitk.GetArrayFromImage(
        sitk.ReadImage(file_name, sitk.sitkFloat32))

    spacing_x = spacing_info[i][0]
    spacing_y = spacing_info[i][1]
    spacing_z = spacing_info[i][2]
    re_spacing_img, curr_spacing, resize_factor = resample(
        src_array, np.array([spacing_z, spacing_x, spacing_y]),
        np.array([0.5, 0.5, 0.5]))

    curr_mean = np.array([0, 0, 0])

    rotate_prob = 0.3

    for v in range(4):
        # for v in range(1):
        print("processing vessel %d" % v)
        reference_path = path_name + '0' + str(i) + '/' + 'vessel' + str(
            v) + '/' + 'reference.txt'
        txt_data = np.loadtxt(reference_path, dtype=np.float32)
        center = txt_data[..., 0:3]

        radials_data = txt_data[..., 3]
        start_ind = get_start_ind(center, radials_data)
        end_ind = get_end_ind(center, radials_data)
        print("start ind:", start_ind)
        print("end ind:", end_ind)
        counter = 0
        last_center_x_pixel = -1
        last_center_y_pixel = -1
        last_center_z_pixel = -1
        # for j in range(start_ind, start_ind+1):
        for j in range(start_ind, end_ind + 1):
            if j % gap_size == 0:
                print('j:', j)
                center_x = center[j][0]
                center_y = center[j][1]
                center_z = center[j][2]

                org_x_pixel = get_spacing_res2(center_x, spacing_x,
                                               resize_factor[1])
                org_y_pixel = get_spacing_res2(center_y, spacing_y,
                                               resize_factor[2])
                org_z_pixel = get_spacing_res2(center_z, spacing_z,
                                               resize_factor[0])

                if org_x_pixel != last_center_x_pixel or org_y_pixel != last_center_y_pixel or org_z_pixel != last_center_z_pixel:
                    print("last:", [
                        last_center_x_pixel, last_center_y_pixel,
                        last_center_z_pixel
                    ])
                    print("curr:", [org_x_pixel, org_y_pixel, org_z_pixel])
                    last_center_x_pixel = org_x_pixel
                    last_center_y_pixel = org_y_pixel
                    last_center_z_pixel = org_z_pixel

                    radial = radials_data[j]

                    record_set = set()
                    curr_conv = np.array([[radial * 0.25, 0.0, 0.0],
                                          [0.0, radial * 0.25, 0.0],
                                          [0.0, 0.0, radial * 0.25]])

                    # To then obtain an off-centerline sample, point x is translated using a random shift sampled from a 3D normal distribution with μ = 0.0, σ = 0.25r

                    for k in range(10):
                        off_center_x, off_center_y, off_center_z = np.random.multivariate_normal(
                            mean=curr_mean, cov=curr_conv, size=1).T
                        center_x_new = center_x + off_center_x[0]
                        center_y_new = center_y + off_center_y[0]
                        center_z_new = center_z + off_center_z[0]
                        center_x_pixel = get_spacing_res2(
                            center_x_new, spacing_x, resize_factor[1])
                        center_y_pixel = get_spacing_res2(
                            center_y_new, spacing_y, resize_factor[2])
                        center_z_pixel = get_spacing_res2(
                            center_z_new, spacing_z, resize_factor[0])

                        while True:
                            if (center_x_pixel != org_x_pixel
                                    or center_y_pixel != org_y_pixel
                                    or center_z_pixel != org_z_pixel) and (
                                        center_x_pixel, center_y_pixel,
                                        center_z_pixel) not in record_set:
                                record_set.add((center_x_pixel, center_y_pixel,
                                                center_z_pixel))
                                break
                            else:
                                off_center_x, off_center_y, off_center_z = np.random.multivariate_normal(
                                    mean=curr_mean, cov=curr_conv, size=1).T
                                center_x_new = center_x + off_center_x[0]
                                center_y_new = center_y + off_center_y[0]
                                center_z_new = center_z + off_center_z[0]

                                center_x_pixel = get_spacing_res2(
                                    center_x_new, spacing_x, resize_factor[1])
                                center_y_pixel = get_spacing_res2(
                                    center_y_new, spacing_y, resize_factor[2])
                                center_z_pixel = get_spacing_res2(
                                    center_z_new, spacing_z, resize_factor[0])

                        new_radial_ind = get_new_radial_ind(
                            center, [center_x_new, center_y_new, center_z_new])

                        new_radial = radials_data[new_radial_ind]

                        sx, sy, sz = get_shell(max_points, new_radial)
                        shell_arr = np.zeros((len(sx), 3))
                        for s_ind in range(len(sx)):
                            shell_arr[s_ind][0] = sx[s_ind]
                            shell_arr[s_ind][1] = sy[s_ind]
                            shell_arr[s_ind][2] = sz[s_ind]

                        pre_ind, next_ind = get_pre_next_point_ind(
                            center, radials_data, new_radial_ind)
                        # 只有找到了前一个点和后一个点才进入切割流程
                        if pre_ind != -1 and next_ind != -1:

                            cut_size = 9

                            left_x = center_x_pixel - cut_size
                            right_x = center_x_pixel + cut_size
                            left_y = center_y_pixel - cut_size
                            right_y = center_y_pixel + cut_size
                            left_z = center_z_pixel - cut_size
                            right_z = center_z_pixel + cut_size

                            new_src_arr = np.zeros(
                                (cut_size * 2 + 1, cut_size * 2 + 1,
                                 cut_size * 2 + 1))
                            for ind in range(left_z, right_z + 1):
                                src_temp = re_spacing_img[ind].copy()
                                new_src_arr[ind -
                                            left_z] = src_temp[left_y:right_y +
                                                               1,
                                                               left_x:right_x +
                                                               1]

                            if np.random.uniform() <= rotate_prob:
                                curr_c = [
                                    center_x_new, center_y_new, center_z_new
                                ]
                                new_src_arr, new_pre_cood, new_next_cood = rotate_augmentation(
                                    new_src_arr,
                                    pre_ind,
                                    next_ind,
                                    curr_c,
                                    center,
                                    angle_x=(-60. / 360 * 2. * np.pi,
                                             60. / 360 * 2. * np.pi),
                                    angle_y=(-60. / 360 * 2. * np.pi,
                                             60. / 360 * 2. * np.pi),
                                    angle_z=(-60. / 360 * 2. * np.pi,
                                             60. / 360 * 2. * np.pi))
                                p = [
                                    new_pre_cood[0], new_pre_cood[1],
                                    new_pre_cood[2]
                                ]
                                pre_sim = find_closer_point_angle(
                                    shell_arr, p, curr_c)
                                p = [
                                    new_next_cood[0], new_next_cood[1],
                                    new_next_cood[2]
                                ]
                                next_sim = find_closer_point_angle(
                                    shell_arr, p, curr_c)
                                pre_ind_list.append(pre_sim)
                                next_ind_list.append(next_sim)
                                radials_list.append(new_radial)
                            else:
                                pre_x = center[pre_ind][0]
                                pre_y = center[pre_ind][1]
                                pre_z = center[pre_ind][2]

                                next_x = center[next_ind][0]
                                next_y = center[next_ind][1]
                                next_z = center[next_ind][2]

                                curr_c = [
                                    center_x_new, center_y_new, center_z_new
                                ]
                                p = [pre_x, pre_y, pre_z]
                                pre_sim = find_closer_point_angle(
                                    shell_arr, p, curr_c)
                                p = [next_x, next_y, next_z]
                                next_sim = find_closer_point_angle(
                                    shell_arr, p, curr_c)
                                pre_ind_list.append(pre_sim)
                                next_ind_list.append(next_sim)
                                radials_list.append(new_radial)
                            folder_path = './patch_data/centerline_patch/offset/point_' + str(
                                max_points) + '_gp_' + str(
                                    gap_size) + '/' + 'd' + str(i)
                            if not os.path.exists(folder_path):
                                os.makedirs(folder_path)
                            record_name = 'centerline_patch/offset/point_' + str(
                                max_points
                            ) + '_gp_' + str(gap_size) + '/' + 'd' + str(
                                i) + '/' + 'd_' + str(i) + '_' + 'v_' + str(
                                    v) + '_' + 'patch_%d_' % counter + str(
                                        k) + '.nii.gz'
                            org_name = './patch_data/' + record_name
                            out = sitk.GetImageFromArray(new_src_arr)
                            sitk.WriteImage(out, org_name)
                            patch_name.append(record_name)

                    counter += 1

    return pre_ind_list, next_ind_list, radials_list, patch_name
def creat_data(path_name, spacing_path, gap_size, save_num):
    spacing_info = np.loadtxt(spacing_path, delimiter=",", dtype=np.float32)
    proximity_list = []
    patch_name = []
    i = save_num
    print("processing dataset %d" % i)
    image_pre_fix = path_name + '0' + str(i) + '/' + 'image' + '0' + str(i)
    file_name = image_pre_fix + '.nii.gz'
    src_array = sitk.GetArrayFromImage(
        sitk.ReadImage(file_name, sitk.sitkFloat32))

    spacing_x = spacing_info[i][0]
    spacing_y = spacing_info[i][1]
    spacing_z = spacing_info[i][2]
    re_spacing_img, curr_spacing, resize_factor = resample(
        src_array, np.array([spacing_z, spacing_x, spacing_y]),
        np.array([1, 1, 1]))

    max_z, max_y, max_x = re_spacing_img.shape
    print('new shape:', re_spacing_img.shape)

    ostia_points = []
    for j in range(4):
        reference_path = '/data_process_tools/train_data/dataset0' + str(
            i) + '/vessel' + str(j) + '/pointS.txt'
        txt_data = np.loadtxt(reference_path, dtype=np.float32)
        if j == 0 or j == 1:
            print('0:', txt_data)
            ostia_points.append(txt_data)
        else:
            ostia_points[1] = ostia_points[1] + txt_data
            print(ostia_points[1])
    ostia_points[1] = ostia_points[1] / 3
    print('ostia points:', ostia_points)

    min_range = 17
    # max_range = 100
    max_points = 100

    counter = 0
    record_set = set()
    for op in ostia_points:
        max_range = get_max_boundr([max_x, max_y, max_z], op)
        for k in range(min_range, int(max_range + 1)):
            x_list, y_list, z_list = get_shell(max_points, k)
            record_set.add(
                (int(round(op[0])), int(round(op[1])), int(round(op[2]))))

            for m in range(len(x_list)):
                new_x = int(round(op[0] + x_list[m]))
                new_y = int(round(op[1] + y_list[m]))
                new_z = int(round(op[2] + z_list[m]))
                check_temp = (new_x, new_y, new_z)
                if check_temp not in record_set:
                    record_set.add(check_temp)
                    center_x_pixel = new_x
                    center_y_pixel = new_y
                    center_z_pixel = new_z

                    target_point = np.array(
                        [center_x_pixel, center_y_pixel, center_z_pixel])
                    print("new center:", target_point)
                    min_dis = np.linalg.norm(target_point - op)
                    print('min dis:', min_dis)
                    curr_proximity = get_proximity(min_dis, cutoff_value=16)
                    print('proximity:', curr_proximity)
                    cut_size = 9

                    left_x = center_x_pixel - cut_size
                    right_x = center_x_pixel + cut_size
                    left_y = center_y_pixel - cut_size
                    right_y = center_y_pixel + cut_size
                    left_z = center_z_pixel - cut_size
                    right_z = center_z_pixel + cut_size

                    if (right_z + 1
                        ) < len(re_spacing_img) and left_z >= 0 and (
                            right_y + 1) < max_y and left_y >= 0 and (
                                right_x + 1
                            ) < max_x and left_x >= 0 and curr_proximity <= 0:
                        new_src_arr = np.zeros(
                            (cut_size * 2 + 1, cut_size * 2 + 1,
                             cut_size * 2 + 1))
                        for ind in range(left_z, right_z + 1):
                            src_temp = re_spacing_img[ind].copy()
                            new_src_arr[ind -
                                        left_z] = src_temp[left_y:right_y + 1,
                                                           left_x:right_x + 1]

                        folder_path = './patch_data/ostia_patch/negative/' + 'gp_' + str(
                            gap_size) + '/d' + str(i)
                        if not os.path.exists(folder_path):
                            os.makedirs(folder_path)
                        record_name = 'ostia_patch/negative/' + 'gp_' + str(
                            gap_size) + '/d' + str(i) + '/' + 'd_' + str(
                                i) + '_' + 'x_' + str(
                                    center_x_pixel) + '_y_' + str(
                                        center_y_pixel) + '_z_' + str(
                                            center_z_pixel) + '.nii.gz'
                        print(record_name)
                        org_name = './patch_data/' + record_name
                        out = sitk.GetImageFromArray(new_src_arr)
                        sitk.WriteImage(out, org_name)

                        proximity_list.append(curr_proximity)
                        patch_name.append(record_name)
                        counter += 1
                    else:
                        print('out of bounder skip this block')

    return patch_name, proximity_list
Beispiel #6
0
def creat_data(path_name, spacing_path, gap_size, save_num):
    spacing_info = np.loadtxt(spacing_path, delimiter=",", dtype=np.float32)
    proximity_list = []
    patch_name = []
    i = save_num
    print("processing dataset %d" % i)
    image_pre_fix = path_name + '0' + str(i) + '/' + 'image' + '0' + str(i)
    file_name = image_pre_fix + '.nii.gz'
    src_array = sitk.GetArrayFromImage(
        sitk.ReadImage(file_name, sitk.sitkFloat32))

    spacing_x = spacing_info[i][0]
    spacing_y = spacing_info[i][1]
    spacing_z = spacing_info[i][2]
    re_spacing_img, curr_spacing, resize_factor = resample(
        src_array, np.array([spacing_z, spacing_x, spacing_y]),
        np.array([1, 1, 1]))

    max_z, max_y, max_x = re_spacing_img.shape

    vessels = []
    for j in range(4):
        reference_path = './train_data/dataset0' + str(i) + '/vessel' + str(
            j) + '/reference.txt'
        txt_data = np.loadtxt(reference_path, dtype=np.float32)
        temp_center = txt_data[..., 0:3]
        vessels.append(temp_center)

    record_set = set()
    max_range = 4
    max_points = 30
    for v in range(4):
        print("processing vessel %d" % v)
        reference_path = path_name + '0' + str(i) + '/' + 'vessel' + str(
            v) + '/' + 'reference.txt'
        txt_data = np.loadtxt(reference_path, dtype=np.float32)
        center = txt_data[..., 0:3]

        counter = 0

        last_center_x_pixel = -1
        last_center_y_pixel = -1
        last_center_z_pixel = -1

        for j in range(len(center)):
            center_x = center[j][0]
            center_y = center[j][1]
            center_z = center[j][2]
            record_set.add((center_x, center_y, center_z))
            if j % gap_size == 0:

                org_x_pixel = int(round(center_x))
                org_y_pixel = int(round(center_y))
                org_z_pixel = int(round(center_z))
                record_set.add((org_x_pixel, org_y_pixel, org_z_pixel))
                if org_x_pixel != last_center_x_pixel or org_y_pixel != last_center_y_pixel or org_z_pixel != last_center_z_pixel:
                    last_center_x_pixel = org_x_pixel
                    last_center_y_pixel = org_y_pixel
                    last_center_z_pixel = org_z_pixel
                    for k in range(1, max_range + 1):
                        x_list, y_list, z_list = get_shell(max_points, k)
                        for m in range(len(x_list)):
                            new_x = int(round(center_x + x_list[m]))
                            new_y = int(round(center_y + y_list[m]))
                            new_z = int(round(center_z + z_list[m]))
                            check_temp = (new_x, new_y, new_z)
                            if check_temp not in record_set:
                                record_set.add(check_temp)
                                center_x_pixel = new_x
                                center_y_pixel = new_y
                                center_z_pixel = new_z

                                target_point = np.array([
                                    center_x_pixel, center_y_pixel,
                                    center_z_pixel
                                ])
                                print("new center:", target_point)
                                min_dis = get_closer_distence(
                                    vessels, target_point)
                                curr_proximity = get_proximity(min_dis,
                                                               cutoff_value=4)
                                print('proximity:', curr_proximity)
                                cut_size = 9

                                left_x = center_x_pixel - cut_size
                                right_x = center_x_pixel + cut_size
                                left_y = center_y_pixel - cut_size
                                right_y = center_y_pixel + cut_size
                                left_z = center_z_pixel - cut_size
                                right_z = center_z_pixel + cut_size

                                if (
                                        right_z + 1
                                ) < len(re_spacing_img) and left_z >= 0 and (
                                        right_y + 1
                                ) < max_y and left_y >= 0 and (
                                        right_x + 1
                                ) < max_x and left_x >= 0 and curr_proximity > 0:
                                    new_src_arr = np.zeros(
                                        (cut_size * 2 + 1, cut_size * 2 + 1,
                                         cut_size * 2 + 1))
                                    for ind in range(left_z, right_z + 1):
                                        src_temp = re_spacing_img[ind].copy()
                                        new_src_arr[ind - left_z] = src_temp[
                                            left_y:right_y + 1,
                                            left_x:right_x + 1]

                                    folder_path = './patch_data/seeds_patch/positive/' + 'gp_' + str(
                                        gap_size) + '/d' + str(i)
                                    if not os.path.exists(folder_path):
                                        os.makedirs(folder_path)
                                    record_name = 'seeds_patch/positive/' + 'gp_' + str(
                                        gap_size
                                    ) + '/d' + str(i) + '/' + 'd_' + str(
                                        i) + '_v_' + str(v) + '_x_' + str(
                                            center_x_pixel) + '_y_' + str(
                                                center_y_pixel) + '_z_' + str(
                                                    center_z_pixel) + '.nii.gz'
                                    print(record_name)
                                    org_name = './patch_data/' + record_name
                                    out = sitk.GetImageFromArray(new_src_arr)
                                    sitk.WriteImage(out, org_name)
                                    proximity_list.append(curr_proximity)
                                    patch_name.append(record_name)
                                    counter += 1

                                else:
                                    print('out of bounder skip this block')
            # break

    return patch_name, proximity_list