Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add annotation Module #175

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions AnnotationModule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import logging
from BaseImage import printMaskHelper
from skimage import io, img_as_ubyte
from skimage.draw import polygon
import os
from pathlib import PurePosixPath

import xml.etree.ElementTree as ET
import numpy as np

"""
xmlMask will create a mask that is true inside the annotated region described in the specified xml file. The xml file must follow the ImageScope format, the minimal components of which are:

<?xml version="1.0" encoding="UTF-8"?>
<Annotations>
<Annotation>
<Regions>
<Region>
<Vertices>
<Vertex X="56657.4765625" Y="78147.3984375"/>
<Vertex X="56657.4765625" Y="78147.3984375"/>
<Vertex X="56664.46875" Y="78147.3984375"/>
</Region>
</Regions>
</Annotation>
</Annotations>

With more <Annotation> or <Region> blocks as needed for additional annotations. There is no functional difference between multiple <Annotation> blocks and one <Annotation> blocks with multiple <Region> blocks
"""

def get_points(xml_fname):
"""Parses the xml file to get those annotations as lists of verticies"""
# create element tree object
tree = ET.parse(xml_fname)

# get root element
root = tree.getroot()

points = []

for annotation in root.findall('Annotation'):
for regions in annotation.findall('Regions'):
for region in regions.findall('Region'):
for vertices in region.findall('Vertices'):
points.append([(int(float(vertex.get('X'))),int(float(vertex.get('Y')))) for vertex in vertices.findall('Vertex')])

return points


def resize_points(points, resize_factor):
for k, pointSet in enumerate(points):
points[k] = [(int(p[0] * resize_factor), int(p[1] * resize_factor)) for p in pointSet]

return points.copy()

def mask_out_annotation(s,xml_fname):
"""Returns the mask of annotations"""

points = get_points(xml_fname)

resize_factor = np.shape(s["img_mask_use"])[1] / s["image_base_size"][0]

points = resize_points(points, resize_factor)

mask = np.zeros((np.shape(s["img_mask_use"])[0],np.shape(s["img_mask_use"])[1]),dtype=np.uint8)

for pointSet in points:
poly = np.asarray(pointSet)
rr, cc = polygon(poly[:,1],poly[:,0],mask.shape)
mask[rr,cc] = 1

return mask

def xmlMask(s,params):
logging.info(f"{s['filename']} - \txmlMask")
mask = s["img_mask_use"]

xml_basepath = params.get("xml_filepath",None)
xml_suffix = params.get("xml_suffix", "")
if not xml_basepath:
xml_basepath = s["dir"]

xml_fname = xml_basepath + os.sep + PurePosixPath(s['filename']).stem + xml_suffix + '.xml'

logging.info(f"{s['filename']} - \tusing {xml_fname}")

annotationMask = mask_out_annotation(s,xml_fname) > 0
io.imsave(s["outdir"] + os.sep + s["filename"] + "_xmlMask.png", img_as_ubyte(annotationMask))
pjl54 marked this conversation as resolved.
Show resolved Hide resolved

prev_mask = s["img_mask_use"]
s["img_mask_use"] = prev_mask & annotationMask

s.addToPrintList("xmlMask",
printMaskHelper(params.get("mask_statistics", s["mask_statistics"]), prev_mask, s["img_mask_use"]))

if len(s["img_mask_use"].nonzero()[0]) == 0: # add warning in case the final tissue is empty
logging.warning(
f"{s['filename']} - After AnnotationModule.xmlMask NO tissue remains detectable! Downstream modules likely to be incorrect/fail")
s["warnings"].append(
f"After AnnotationModule.xmlMask NO tissue remains detectable! Downstream modules likely to be incorrect/fail")

return
Loading