Skip to content

Instantly share code, notes, and snippets.

@3dimaging
Created April 27, 2019 20:26
Show Gist options
  • Save 3dimaging/d2c7e15010e2f23f4d4f45f519dcd385 to your computer and use it in GitHub Desktop.
Save 3dimaging/d2c7e15010e2f23f4d4f45f519dcd385 to your computer and use it in GitHub Desktop.
042719
#!/usr/bin/env python3
#If you need more information about how ElementTree package handle XML file, please follow the link:
#https://docs.python.org/3/library/xml.etree.elementtree.html
#import multiresolutionimageinterface as mir
import matplotlib.pyplot as plt
import cv2
import numpy as np
import openslide
from pandas import DataFrame
import os.path as osp
#reader = mir.MultiResolutionImageReader()
#mr_image = reader.open('/home/wli/Downloads/tumor_036.tif')
#Ximageorg, Yimageorg = mr_image.getDimensions()
#dims = mr_image.getLevelDimensions(4)
#Ximage = (Ximage+240//2)//240
#Ximage = 4000
#Yimage = (Yimage+240//2)//240
#Yimage = 2000
import xml.etree.ElementTree as et
import pandas as pd
import glob
import math
def convert_xml_df(file):
parseXML = et.parse(file)
root = parseXML.getroot()
dfcols = ['Name', 'Order', 'X', 'Y']
df_xml = pd.DataFrame(columns=dfcols)
for child in root.iter('Annotation'):
for coordinate in child.iter('Coordinate'):
Name = child.attrib.get('Name')
Order = coordinate.attrib.get('Order')
X_coord = float(coordinate.attrib.get('X'))
# X_coord = X_coord - 30000
#X_coord = ((X_coord)*dims[0])/Ximageorg
X_coord = X_coord
#X_coord = X_coord/32
Y_coord = float(coordinate.attrib.get('Y'))
# Y_coord = Y_coord - 155000
#Y_coord = ((Y_coord)*dims[1])/Yimageorg
Y_coord = Y_coord
#Y_coord = Y_coord/32
df_xml = df_xml.append(pd.Series([Name, Order, X_coord, Y_coord], index = dfcols), ignore_index=True) # type: DataFrame
df_xml = pd.DataFrame(df_xml)
return (df_xml)
#x_values = list(annotations['X'].get_values())
#y_values = list(annotations['Y'].get_values())
#xy = list(zip(x_values,y_values))
def Remove_dup(duplicate):
final_list = []
for num in duplicate:
if num not in final_list:
final_list.append(num)
return final_list
def mask_gen(final_list):
coxy = [[] for x in range(len(final_list))]
for index, n in enumerate(final_list):
newx = annotations[annotations['Name']== n]['X']
newy = annotations[annotations['Name']== n]['Y']
print(n)
print(newx, newy)
newxy = list(zip(newx, newy))
coxy[index] = np.array(newxy, dtype=np.int32)
#image = cv2.imread('/home/wli/Downloads/tumor_036.xml', -1)
canvas = np.zeros((int(dims[1]), int(dims[0])), np.uint8)
#canvas = np.zeros((int(dims[1]/32), int(dims[0]/32)), np.uint8)
#tile =mr_image.getUCharPatch(0, 0, dims[0], dims[1], 4)
#canvas = np.zeros((Ximage, Yimage, 3), np.uint8) # fix the division
#coords = np.array([xy], dtype=np.int32)
#cv2.drawContours(canvas, [coords],-1, (0,255,0), -1)
#cv2.drawContours(canvas, coxy, -1, (255, 255, 255), 10)
#cv2.drawContours(canvas, coxy, -1, (255, 255, 255), CV_FILLED)
cv2.fillPoly(canvas, pts = coxy, color=(255,255,255))
#cv2.polylines(canvas, coxy, isClosed=True, color=(255,255,255), thickness=5)
cv2.imwrite('/home/wzli/home_made_mask_files/%s.tif' % osp.basename(osp.splitext(xml_file)[0]), canvas)
#cv2.imshow("tile", tile);cv2.waitKey();cv2.destroyAllWindows()
#cv2.fillConvexPoly(mask, coords,1)
#mask = mask.astype(np.bool)
#output = np.zeros_like(image)
#output[mask] = image[mask]
#cv2.imshow('image',output)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
if __name__ == '__main__':
xml_folder = '/home/wzli/Downloads/CAMELYON16/testing/lesion_annotations/'
xml_paths = glob.glob(osp.join(xml_folder, '*.xml'))
slide_folder = '/home/wzli/Downloads/CAMELYON16/testing/images/'
#slide_paths = glob.glob(osp.join(slide_folder, '*.tif'))
for xml_file in xml_paths:
slide_name = osp.basename(xml_file.replace('.xml', '.tif'))
slide_path = osp.join(slide_folder, slide_name)
wsi_image = openslide.open_slide(slide_path)
dims = wsi_image.dimensions
annotations = convert_xml_df(xml_file)
final_list = Remove_dup(annotations['Name'])
mask_gen(final_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment