Skip to content

Instantly share code, notes, and snippets.

@jendelel
Last active November 19, 2024 18:45
Show Gist options
  • Save jendelel/3a8e768a8eb9345d49f2a82d02946122 to your computer and use it in GitHub Desktop.
Save jendelel/3a8e768a8eb9345d49f2a82d02946122 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.
"""
def load_point(point_string):
x, y = tuple([float(num) for num in point_string.strip('()').split(',')])
return y, x
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 = [load_point(point) for point in points]
if len(points) <= 2:
for point in points:
mask[int(point[0]), int(point[1])] = 1
else:
x, y = zip(*points)
x, y = np.array(x), np.array(y)
poly_x, poly_y = polygon(x, y, shape=imshape)
mask[poly_x, poly_y] = 1
return mask
@jasminjahanpuspo
Copy link

Hello, I am using this code. It's pretty much easy to understand but somehow I get all values zeros when run on Colab. Here is a screenshot of my code. Any kind of help will be appreciated. Capture5

Hi @jasminjahanpuspo were you able to resolve this? As I also tried the code and I run into the same issue.

@shubham-pipada Thank you for asking! Unfortunately, I wasn't able to solve this issue yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment