-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor_normalization.py
More file actions
134 lines (94 loc) · 3.97 KB
/
Color_normalization.py
File metadata and controls
134 lines (94 loc) · 3.97 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#Color normalization with multiprocessing
#Modify https://github.com/schaugf/HEnorm_python
import numpy as np
import multiprocessing as mp
import pandas as pd
import argparse
from PIL import Image
import os
def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
return files
def normalizeStaining(img,name, saveFile=None, Io=240, alpha=1, beta=0.15):
''' Normalize staining appearence of H&E stained images
Example use:
see test.py
Input:
I: RGB input image
Io: (optional) transmitted light intensity
Output:
Inorm: normalized image
H: hematoxylin image
E: eosin image
Reference:
A method for normalizing histology slides for quantitative analysis. M.
Macenko et al., ISBI 2009
'''
HERef = np.array([[0.5626, 0.2159],
[0.7201, 0.8012],
[0.4062, 0.5581]])
maxCRef = np.array([1.9705, 1.0308])
# define height and width of image
h, w, c = img.shape
# reshape image
img = img.reshape((-1,3))
# calculate optical density
OD = -np.log((img.astype(float)+1)/Io)
# remove transparent pixels
ODhat = OD[~np.any(OD<beta, axis=1)]
# compute eigenvectors
try:
eigvals, eigvecs = np.linalg.eigh(np.cov(ODhat.T))
except np.linalg.LinAlgError or RuntimeWarning:
return img, True #first attempt at dealing with backgrounds read as tissue, may no longer be necessary due to VarianceCheck()
#eigvecs *= -1
#project on the plane spanned by the eigenvectors corresponding to the two
# largest eigenvalues
That = ODhat.dot(eigvecs[:,1:3])
phi = np.arctan2(That[:,1],That[:,0])
minPhi = np.percentile(phi, alpha)
maxPhi = np.percentile(phi, 100-alpha)
vMin = eigvecs[:,1:3].dot(np.array([(np.cos(minPhi), np.sin(minPhi))]).T)
vMax = eigvecs[:,1:3].dot(np.array([(np.cos(maxPhi), np.sin(maxPhi))]).T)
# a heuristic to make the vector corresponding to hematoxylin first and the
# one corresponding to eosin second
if vMin[0] > vMax[0]:
HE = np.array((vMin[:,0], vMax[:,0])).T
else:
HE = np.array((vMax[:,0], vMin[:,0])).T
# rows correspond to channels (RGB), columns to OD values
Y = np.reshape(OD, (-1, 3)).T
# determine concentrations of the individual stains
C = np.linalg.lstsq(HE,Y, rcond=None)[0]
# normalize stain concentrations
maxC = np.array([np.percentile(C[0,:], 99), np.percentile(C[1,:],99)])
tmp = np.divide(maxC,maxCRef)
C2 = np.divide(C,tmp[:, np.newaxis])
# recreate the image using reference mixing matrix
Inorm = np.multiply(Io, np.exp(-HERef.dot(C2)))
Inorm[Inorm>255] = 254
Inorm = np.reshape(Inorm.T, (h, w, 3)).astype(np.uint8)
# unmix hematoxylin and eosin
H = np.multiply(Io, np.exp(np.expand_dims(-HERef[:,0], axis=1).dot(np.expand_dims(C2[0,:], axis=0))))
H[H>255] = 254
H = np.reshape(H.T, (h, w, 3)).astype(np.uint8)
E = np.multiply(Io, np.exp(np.expand_dims(-HERef[:,1], axis=1).dot(np.expand_dims(C2[1,:], axis=0))))
E[E>255] = 254
E = np.reshape(E.T, (h, w, 3)).astype(np.uint8)
if saveFile is None:
Image.fromarray(Inorm).save('please input folder'+name) #please input folder
#Image.fromarray(H).save('please input folder'+name+'_H.png') #please input folder
#Image.fromarray(E).save('please input folder'+name+'_E.png') #please input folder
return Inorm, #False#, H, E
def data_process(x):
fig_list=file_name('path to folder') #please input file
for i in range(0,len(fig_list)):
img = np.array(Image.open('path to folder'+fig_list[i]))
normalize_img=normalizeStaining(img,name=fig_list[i])
#print(i)
def multicore():
pool = mp.Pool()
result = pool.map(data_process, range(10))
#print(result)
# if __name__=='__main__':
multicore()