{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":false},"cell_type":"markdown","source":"# Stain Seperation"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"This is an implementation of stain seperation algorithm as described in *STRUCTURE-PRESERVED COLOR NORMALIZATION FOR HISTOLOGICAL IMAGES, IEEE International Symposium on Biomedical Imaging, 2015.*\n\nI used it to seperate stains into different channels and train my network based these new images. In my experience, it did not improve the score much. Still, I hope you can make use of it."},{"metadata":{"_uuid":"f7dbdcc6d4496d55f515bab0dcf93adec54684cd"},"cell_type":"markdown","source":"Please note that to use the this code, you have to install SPAMS python package from: http://spams-devel.gforge.inria.fr/doc-python/html/doc_spams003.html\n\nIt only works for RGB images with pixel intensity between [0-255]. Otherwise, change `beer_lambert_transform` function to use approperiate value."},{"metadata":{"trusted":true,"_uuid":"0f137469f1d48b4375d4de7f463880a32c9b5093"},"cell_type":"code","source":"import numpy as np\nfrom skimage import color\nfrom spams import trainDL, lasso\n\ndef beer_lambert_transform(tensor):\n  ivec = tensor.reshape(tensor.shape[0] * tensor.shape[1], tensor.shape[2])\n  v = np.log(255) - np.log(ivec+1)\n\n  # Remove white pixel from stain color map\n  luminlayer = color.rgb2lab(tensor)[:,:,0]\n  inew = ivec[ luminlayer.flatten() / np.max(luminlayer) < 0.9  , :]\n  v_for_w = np.log(255) - np.log(inew+1)\n\n  return v, v_for_w\n\ndef est_w_h(v, v_initial, n_stains, sparsity_thr):\n  w = trainDL(np.asfortranarray(v_initial.T),mode=2,iter=100,lambda1=sparsity_thr,posAlpha=True,posD=True,modeD=0,whiten=False,K=n_stains,verbose=False)\n  #w = w[w[:,0].argsort()]\n  H = lasso(np.asfortranarray(v.T),np.asfortranarray(w),mode=2,lambda1=sparsity_thr,pos=True,verbose=False)\n  return H, w\n\ndef stain_seperator(img, n_stains, sparsity_thr=0.8, return_wh=False):\n  assert np.ndim(img) == 3, \"stain_seperator only works with rgb images\"                                                                    \n  assert img.shape[2] == 3, \"stain_seperator expects image with shape [.., .., 3]\"                                                          \n\n  if img.dtype.char not in np.typecodes['AllFloat']:                  \n    img = img.astype(np.float)     \n\n  width = img.shape[0]             \n  height = img.shape[1]            \n\n  (v, v_for_w) = beer_lambert_transform(img)                          \n  h, w = est_w_h(v, v_for_w, n_stains, sparsity_thr)                  \n\n  if return_wh:                    \n      return w, h.toarray()        \n\n  h1 = w*h                         \n  h1 = np.exp(-h1)                 \n\n  stain_maps = np.moveaxis(h1.reshape(3,width,height),0,-1)           \n\n  return stain_maps                \n","execution_count":1,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"bdff452093ce786e8df9824f8662c1e283f5cab5"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}