Skip to content

Instantly share code, notes, and snippets.

@odebeir
Last active April 1, 2016 23:10
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odebeir/5038467 to your computer and use it in GitHub Desktop.
Save odebeir/5038467 to your computer and use it in GitHub Desktop.
Color deconvolution for python cf : A. C. Ruifrok and D. A. Johnston, “Quantification of histochemical staining by color deconvolution.,” Analytical and quantitative cytology and histology / the International Academy of Cytology [and] American Society of Cytology, vol. 23, no. 4, pp. 291–9, Aug. 2001.
def convert_to_optical_densities(rgb,r0,g0,b0):
OD = rgb.astype(float)
OD[:,:,0] /= r0
OD[:,:,1] /= g0
OD[:,:,2] /= b0
return -np.log(OD)
def color_deconvolution(rgb,r0,g0,b0,verbose=False):
stain_OD = np.asarray([[0.18,0.20,0.08],[0.01,0.13,0.0166],[0.10,0.21,0.29]]) #hematoxylin, eosyn, DAB
n = []
for r in stain_OD:
n.append(r/norm(r))
normalized_OD = np.asarray(n)
D = inv(normalized_OD)
OD = convert_to_optical_densities(rgb,r0,g0,b0)
ODmax = np.max(OD,axis=2)
plt.figure()
plt.imshow(ODmax>.1)
# reshape image on row per pixel
rOD = np.reshape(OD,(-1,3))
# do the deconvolution
rC = np.dot(rOD,D)
#restore image shape
C = np.reshape(rC,OD.shape)
#remove problematic pixels from the the mask
ODmax[np.isnan(C[:,:,0])] = 0
ODmax[np.isnan(C[:,:,1])] = 0
ODmax[np.isnan(C[:,:,2])] = 0
ODmax[np.isinf(C[:,:,0])] = 0
ODmax[np.isinf(C[:,:,1])] = 0
ODmax[np.isinf(C[:,:,2])] = 0
return (ODmax,C)
@spotter
Copy link

spotter commented Feb 26, 2013

Line 9: What is the expected range of rgb channels (0-255 or 0.-1.)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment