-
Notifications
You must be signed in to change notification settings - Fork 0
/
createPatches.py
61 lines (42 loc) · 1.71 KB
/
createPatches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
from xml.dom import minidom
import matplotlib.path as mplPath
import matplotlib.pyplot as plt
import matplotlib.patches as patches
xmldoc = minidom.parse('/home/ghost/Desktop/annotations/tumor_078.xml')
itemlist = xmldoc.getElementsByTagName('Annotation')
from openslide import *
def getTumor(tumorName) :
waypoints=[]
count = 0
for s in itemlist:
if(tumorName == s.attributes['Name'].value) : #JUST TUMOR 1
points = s.getElementsByTagName('Coordinate')
for t in points : #83
a = float(t.attributes['X'].value)
b = float(t.attributes['Y'].value)
waypoints.append([a,b])
return mplPath.Path(np.asarray(waypoints))
img = OpenSlide('/home/ghost/Desktop/tumor_078.tif')
def getRegion(name, patchIndex, polygon) :
region = img.read_region(location=(int(polygon.vertices[patchIndex][0]-100),int(polygon.vertices[patchIndex][1]-100)), level= 0, size=(200,200))
region = np.asarray(region.convert('RGB'))
matrix = np.zeros((200,200))
for i in range(200) :
for j in range(200) :
x = polygon.vertices[patchIndex][0]+(i-100)
y = polygon.vertices[patchIndex][1]+(j-100)
if polygon.contains_point((x, y)) :
matrix[j][i] = 255
im = Image.fromarray(matrix).convert('RGB')
im2 = Image.fromarray(region).convert('RGB')
im.save(f'./dataset/{name}_gt.png')
im2.save(f'./dataset/{name}_im.png')
count = 0
for j in range(200) :
name = f'Tumor{j+1}'
polygon = getTumor(name)
for i in range(len(polygon.vertices)) :
getRegion(count, i, polygon)
count+=1
print(count)