-
Notifications
You must be signed in to change notification settings - Fork 1
/
patch_color_norm.py
83 lines (78 loc) · 3.01 KB
/
patch_color_norm.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import cv2
import openslide
import numpy as np
import os
from PIL import Image
import glob
#cls = "/luminalA/"
# path = "/home/Drive3/yashashwi/tcga_REMAINING/thyroid_remaining/"
path = "/home/Drive3/yashashwi/tcga_REMAINING/color_norm/tcga_color_norm/new_norm"
dest = "/home/Drive3/yashashwi/tcga_REMAINING/thyroid_remaining/patches_color_norm"
level = 1
# label = 2
slide = 1
window = 500*2*5
threshould = 220
def generate_patches(filename, patient,ind):
# if not os.path.exists(dest+'/'+str(i)+'/'+patient+'/'):
# # print("aaaaaaaaa")
# # os.mkdir(dest+'/'+patient+'/')
# os.mkdir(dest+'/'+str(i)+'/'+patient+'/')
ind=100+ind
if not os.path.exists(dest+'/'+str(ind)+'/'):
os.mkdir(dest+'/'+str(ind)+'/')
if not os.path.exists(dest+'/'+str(ind)+'/'+patient+'/'):
os.mkdir(dest+'/'+str(ind)+'/'+patient+'/')
a = openslide.OpenSlide(filename)
col,row = a.level_dimensions[level]
# print(len(range(0,row-window,np.int(window/slide))))
# print(len(range(0,col-window,np.int(window/slide))))
kk=0
for i in range(0,row-window,np.int(window/slide)):
for j in range(0,col-window,np.int(window/slide)):
im_object = a.read_region((j,i),level,(window,window))
tpatch = np.asarray(im_object)
patch = tpatch[:,:,:3]
prob0 = patch[:,:,0].mean()
prob1 = patch[:,:,1].mean()
prob2 = patch[:,:,2].mean()
if(prob0 < threshould and prob1 < threshould and prob2 < threshould):
kk=kk+1
# im_object.save(dest+'/'+patient+'/'+ patient + '__'+ str(i) +'_'+ str(j) + '.png')
im_objectnew=im_object.resize((2500,2500))
im_objectnew.save(dest+'/'+str(ind)+"/"+patient +'/'+patient +'__'+ str(kk) + '.png')
# for qq in range(1,50):
# angle=np.random.randint(1,359)
# # rI=rotateImage(im_objectnew, angle)
# im2 = im_objectnew.convert('RGBA')
# # rotated image
# rot = im2.rotate(angle, expand=0)
# # a white image same size as rotated image
# fff = Image.new('RGBA', rot.size, (255,)*4)
# # create a composite image using the alpha layer of rot as a mask
# out = Image.composite(rot, fff, rot)
# # save your work (converting back to mode='1' or whatever..)
# print(dest+'/'+str(ind)+'/'+patient+'/' +patient+'__'+ str(i) +'_'+ str(j) +'_'+str(qq)+ '.png')
# out.convert(im_objectnew.mode).save(dest+'/'+str(ind)+'/'+patient+'/' +patient+'__'+ str(i) +'_'+ str(j) +'_'+str(qq)+ '.png')
import glob, os
# os.chdir("/home/SSD/thyroid_remaining")
# for file in glob.glob("*.txt"):
# print(file)
# files = sorted(glob.glob(os.path.join(path, "*/*.svs")))
files = (glob.glob(path+"*/*.tif"))
# x, patient = files[0].split("TCGA-")
# print(patient)
print len(files)
# i=0
for ind in range(0,len(files)):
# i=i+1
print(ind)
x, patient = files[ind].split("TCGA-")
patient, y = patient.split("-01Z")
# print(patient)
# print(file)
if not os.path.exists((os.path.join(path,str("TCGA-"+patient)))):
a = openslide.OpenSlide(files[ind])
# mag = a.properties["openslide.objective-power"]
print(str("TCGA-"+patient))
generate_patches(files[ind], "TCGA-"+patient,ind)