Skip to content

Instantly share code, notes, and snippets.

@3dimaging
Created April 26, 2019 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3dimaging/a1532428d045fedde89de60ec55891c9 to your computer and use it in GitHub Desktop.
Save 3dimaging/a1532428d045fedde89de60ec55891c9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import multiresolutionimageinterface as mir
import matplotlib.pyplot as plt
import os.path as osp
import openslide
import matplotlib.pyplot as plt
from pathlib import Path
import glob
import re
#please make sure the same number of files in the folder of tumor file and folder of annotation files
#please change the slide_path, anno_path, mask_path accordingly, and leave everything else untouched.
slide_path = '/home/wli/Downloads/CAMELYON16/testing/images'
#anno_path = '/home/wli/Downloads/CAMELYON16/testing/Lesion_annotations'
anno_path = '/home/wli/Downloads/CAMELYON16/testing/lesion_annotations_new_download'
mask_path = '/home/wli/Downloads/CAMELYON16/testing/masking_new'
tumor_paths = glob.glob(osp.join(slide_path, '*.tif'))
tumor_paths.sort()
anno_tumor_paths = glob.glob(osp.join(anno_path, '*.xml'))
anno_tumor_paths.sort()
#image_pair = zip(tumor_paths, anno_tumor_paths)
#image_pair = list(image_mask_pair)
reader = mir.MultiResolutionImageReader()
i=0
while i < len(anno_tumor_paths):
new_slide_path = [x for x in tumor_paths if re.search(
osp.basename(anno_tumor_paths[i]).replace('.xml', '.tif'), x)]
new_tumor_path = new_slide_path[0]
print(new_tumor_path)
mr_image = reader.open(new_tumor_path)
annotation_list=mir.AnnotationList()
xml_repository = mir.XmlRepository(annotation_list)
xml_repository.setSource(anno_tumor_paths[i])
xml_repository.load()
annotation_mask=mir.AnnotationToMask()
camelyon17_type_mask = False
label_map = {'metastases': 1, 'normal': 2} if camelyon17_type_mask else {'_0': 1, '_1': 1, '_2': 0}
#label_map = {'metastases': 1, 'normal': 2} if camelyon17_type_mask else {'_0': 255, '_1': 255, '_2': 0}
conversion_order = ['metastases', 'normal'] if camelyon17_type_mask else ['_0', '_1', '_2']
output_path= osp.join(mask_path, osp.basename(new_tumor_path).replace('.tif', '_mask.tif'))
annotation_mask.convert(annotation_list, output_path, mr_image.getDimensions(), mr_image.getSpacing(), label_map, conversion_order)
i=i+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment