示例#1
0
def extract_patches_multifiles_boundary(data_dir, names, target_label,
                                        patch_size, patch_step, save_dir):
    patches = []
    labeles = []
    for name in names:
        if name is not None and not name.endswith(target_label):
            continue
        cur_data_dir = os.path.join(data_dir, name)
        print 'extract patches from ', cur_data_dir, ' at ', str(os.getpid())
        pv_mask_image, pv_mhd_image = read_from_dir(cur_data_dir)
        erode_mask_image = image_erode(pv_mask_image, size=5)
        if np.sum(erode_mask_image == 1) < 30:
            continue
        expand_mask_image = image_expand(pv_mask_image, size=10)
        pv_mask_image = np.asarray(
            np.logical_and(erode_mask_image == 0, expand_mask_image == 1),
            np.uint8)
        [x_min, x_max, y_min, y_max] = get_boundingbox(pv_mask_image)
        r = patch_size / 2
        cur_patches = []
        for i in range(x_min, x_max, patch_step):
            for j in range(y_min, y_max, patch_step):
                cur_patch = pv_mhd_image[i - r:i + r + 1, j - r:j + r + 1]
                cur_mask_patch = pv_mhd_image[i - r:i + r + 1, j - r:j + r + 1]
                if ((1.0 * np.sum(cur_mask_patch)) /
                    (1.0 * patch_size * patch_size)) < 0.1:
                    continue

                cur_patches.append(np.array(cur_patch).flatten())
        if save_dir is None:
            # if len(cur_patches) == 0:
            #     continue
            patches.append(cur_patches)
            labeles.append(int(target_label))
    print len(patches), len(labeles)
    return patches, labeles
示例#2
0
    def extract_interior_boundary_patch_npy(dir_name, suffix_name, save_dir, patch_size, patch_step):
        '''
            提取指定类型病灶的patch 保存原始像素值,存成npy的格式
            :param patch_size: 提取patch的大小
            :param dir_name: 目前所有病例的存储路径
            :param suffix_name: 指定的病灶类型的后缀,比如说cyst 就是0
            :param save_dir: 提取得到的patch的存储路径
            :param patch_step: 提取patch的步长
            :return: None
        '''
        count = 0
        names = os.listdir(dir_name)
        for name in names:
            if name.endswith(suffix_name):
                # 只提取指定类型病灶的patch
                mask_images = []
                mhd_images = []
                flag = True
                for phasename in phasenames:
                    image_path = glob(os.path.join(dir_name, name, phasename + '_Image*.mhd'))[0]
                    mask_path = os.path.join(dir_name, name, phasename + '_Registration.mhd')
                    mhd_image = read_mhd_image(image_path)
                    mhd_image = np.squeeze(mhd_image)
                    # show_image(mhd_image)
                    mask_image = read_mhd_image(mask_path)
                    mask_image = np.squeeze(mask_image)

                    [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image)

                    if (xmax - xmin) <= 5 or (ymax - ymin) <= 5:
                        flag = False
                        continue
                    interior_boundary = image_erode(mask_image, 5)
                    expand_boundary = image_expand(mask_image, 10)
                    mask_image = np.asarray(np.logical_and(interior_boundary == 0, expand_boundary == 1), np.uint8)
                    mask_image = mask_image[xmin: xmax, ymin: ymax]
                    mhd_image = mhd_image[xmin: xmax, ymin: ymax]
                    mhd_image[mask_image != 1] = 0
                    mask_images.append(mask_image)
                    mhd_images.append(mhd_image)
                    # show_image(mhd_image)
                if not flag:
                    continue
                mask_images = convert2depthlaster(mask_images)
                mhd_images = convert2depthlaster(mhd_images)
                count += 1
                [width, height, depth] = list(np.shape(mhd_images))
                patch_count = 1
                # if width * height >= 900:
                #     patch_step = int(math.sqrt(width * height / 100))
                for i in range(patch_size / 2, width - patch_size / 2, patch_step):
                    for j in range(patch_size / 2, height - patch_size / 2, patch_step):
                        cur_patch = mhd_images[i - patch_size / 2:i + patch_size / 2,
                                    j - patch_size / 2: j + patch_size / 2, :]
                        if (np.sum(mask_images[i - patch_size / 2:i + patch_size / 2,
                                   j - patch_size / 2: j + patch_size / 2, :]) / (
                                        (patch_size - 1) * (patch_size - 1) * 3)) < 0.7:
                            continue
                        save_path = os.path.join(save_dir, name + '_' + str(patch_count) + '.npy')
                        # print save_path
                        np.save(save_path, np.array(cur_patch))
                        patch_count += 1
                if patch_count == 1:
                    continue
                    # save_path = os.path.join(save_dir, name + '_' + str(patch_count) + '.png')
                    # roi_image = Image.fromarray(np.asarray(mhd_images, np.uint8))
                    # roi_image.save(save_path)
        print count