Created
April 28, 2019 20:23
-
-
Save 3dimaging/ee141a87f224c31f3c3c377825038b27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
wsi_dir = '/home/wzli/Downloads/CAMELYON16/testing/images/test_001.tif' | |
wsi_image = openslide.open_slide(wsi_dir) | |
dims = wsi_image.dimensions | |
#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 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/16 | |
Y_coord = float(coordinate.attrib.get('Y')) | |
# Y_coord = Y_coord - 155000 | |
#Y_coord = ((Y_coord)*dims[1])/Yimageorg | |
Y_coord = Y_coord/16 | |
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) | |
xml_path = '/home/wzli/Downloads/CAMELYON16/testing/lesion_annotations/test_001.xml' | |
annotations = convert_xml_df(xml_path) | |
#x_values = list(annotations['X'].get_values()) | |
#y_values = list(annotations['Y'].get_values()) | |
#xy = list(zip(x_values,y_values)) | |
def Remove(duplicate): | |
final_list = [] | |
for num in duplicate: | |
if num not in final_list: | |
final_list.append(num) | |
return final_list | |
final_list = Remove(annotations['Name']) | |
coxy = [[] for x in range(len(final_list))] | |
i = 0 | |
for n in 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[i] = np.array(newxy, dtype=np.int32) | |
i=i+1 | |
#image = cv2.imread('/home/wli/Downloads/tumor_036.xml', -1) | |
canvas = np.zeros((int(dims[1]/16), int(dims[0]/16)), 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.split(xml_path)[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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment