Skip to content

Instantly share code, notes, and snippets.

@Feyn-Man
Forked from jendelel/load_inbreast_roi.py
Last active July 19, 2022 12:49
Show Gist options
  • Save Feyn-Man/de6f62997d051fc6ff75a6aa968537f5 to your computer and use it in GitHub Desktop.
Save Feyn-Man/de6f62997d051fc6ff75a6aa968537f5 to your computer and use it in GitHub Desktop.
Load INBREAST ROIs
from skimage.draw import polygon
import numpy as np
import plistlib
def load_inbreast_mask(mask_path, imshape=(4084, 3328)):
"""
This function loads a osirix xml region as a binary numpy array for INBREAST
dataset
@mask_path : Path to the xml file
@imshape : The shape of the image as an array e.g. [4084, 3328]
return: numpy array where positions in the roi are assigned a value of 1.
"""
mask = np.zeros(imshape)
with open(mask_path, 'rb') as mask_file:
plist_dict = plistlib.load(mask_file, fmt=plistlib.FMT_XML)['Images'][0]
numRois = plist_dict['NumberOfROIs']
rois = plist_dict['ROIs']
assert len(rois) == numRois
for roi in rois:
numPoints = roi['NumberOfPoints']
points = roi['Point_px']
assert numPoints == len(points)
points = [eval(point) for point in points]
if len(points) <= 2:
for point in points:
mask[int(point[1]), int(point[0])] = 1
else:
x, y = zip(*points)
col, row = np.array(x), np.array(y) ##x coord is the column coord in an image and y is the row
poly_x, poly_y = polygon(row, col, shape=imshape)
mask[poly_x, poly_y] = 1
return mask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment