{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5"},"cell_type":"markdown","source":"**This note book contain some useful functions that may help **\n\nThe first function is the labelImage function : This function display the masks for a given image"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os #operating system functionality\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg \nfrom scipy import misc","execution_count":1,"outputs":[]},{"metadata":{"_cell_guid":"c8d430a2-6af8-4a53-8286-ee3a4b9376d6","_uuid":"f2b942b557b4c0c8d3f6e731b6d4a23d118407a8","trusted":true},"cell_type":"code","source":"#Let's first read an image from the train folder\nImageId='0ea221716cf13710214dcd331a61cea48308c3940df1d28cfc7fd817c83714e1'\nfolder='../input/stage1_train/'\nf = misc.imread(folder+ImageId+'/images/'+ImageId+'.png',flatten=False)\nheigh=f.shape[0]\nwidth=f.shape[1]\nprint('Image : ', ImageId, ' ',heigh,'*',width)\nplt.imshow(f)\nplt.show()","execution_count":2,"outputs":[]},{"metadata":{"_cell_guid":"c60250c9-6275-4051-880f-ac6d07be6d22","_uuid":"80d94659939ff4ee11ec1d9661e031fac5fe342e","trusted":true},"cell_type":"code","source":"# Now let's plot the masks for this image\n#But first let's read the file containing the labels\nlabels = pd.read_csv('../input/stage1_train_labels.csv',sep=',')\nlabels.head(5)","execution_count":3,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"72f8ec20fdcefb4745897507823dd83136c413b9","_cell_guid":"d5f85dee-9429-47d2-949f-ba74c47ff256","trusted":true},"cell_type":"code","source":"#The labelImage function\ndef labelImage(ImageId,imageSize):\n    #define the array where to store the labels\n    y=np.zeros((imageSize,),dtype=bool)\n    #select just the encoded pixels for the given image\n    temp=list(labels[labels['ImageId']==ImageId]['EncodedPixels'])\n    for w in temp:\n        x=w.split()\n        x=[int(x[i]) for i in range(0,len(x)) ]\n        for i in range(0,len(x),2):\n            y[x[i]:min((x[i]+x[i+1]),len(y)-1)] = [True]*(min((x[i]+x[i+1]),len(y)-1)-x[i])\n        \n    return y","execution_count":4,"outputs":[]},{"metadata":{"_cell_guid":"bac2e7d2-3679-46e1-8234-8e0ed25d4555","_uuid":"5c7ff6f713f54375f93835bb32967f55b90acb72","trusted":true},"cell_type":"code","source":"y=labelImage(ImageId,heigh*width)\nplt.imshow(y.reshape((heigh,width),order='F'),cmap='gray')\nplt.show()\nplt.imshow(f)\nplt.show()","execution_count":5,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"687de430b5831d7cd875df85d1b9b6f258bc3fdc","_cell_guid":"a9f7759c-f5d1-4b70-97e0-9e2eabbda17d","trusted":true},"cell_type":"code","source":"#Now let's try to use a simple algorithm to detect the Nucleis in this image\n#But let use this function to plot an image encoded in one-dimension arra\ndef showDigitImage(x, h=0,Type='gray', order_='F'):\n    if h<=0:\n        dimension_1 = int(np.sqrt(len(x)))\n        dimension_2=dimension_1\n    else:\n        dimension_1=h\n        dimension_2=int(len(x)/h)\n        \n    print('(',dimension_1,dimension_2,')',sep=' ')\n    plt.imshow(x.reshape((dimension_1,dimension_2),order=order_),cmap='gray')\n    plt.show()","execution_count":6,"outputs":[]},{"metadata":{"_cell_guid":"e6c83262-c4a3-4f0f-8ed2-3c8cf354ac92","_uuid":"89cd83378d255284361465731878dca0dfebd4ce","trusted":true},"cell_type":"code","source":"showDigitImage(y,heigh)","execution_count":7,"outputs":[]},{"metadata":{"_cell_guid":"63d1e491-2191-4574-a4e1-ec0e3f84ddbb","_uuid":"abf86dc23bdfbe230844c56a248d2f553f6a47d7","trusted":true,"collapsed":true},"cell_type":"code","source":"#The binarize function\ndef binarize(x, trashold=128, h=0,Type='gray'):\n    x2=x.copy()\n    for i in range(0,len(x)):\n        if x[i]>trashold :\n            x2[i]=1\n        else:\n            x2[i]=0\n    showDigitImage(x2,h,Type)\n    \n    return x2","execution_count":40,"outputs":[]},{"metadata":{"_cell_guid":"b935d4d7-5864-4bd9-bb67-082e7ae65280","_uuid":"4e33ff53b31f8af443d702b18f261c6449a3367f","trusted":true},"cell_type":"code","source":"#But we need to grayScale the image first\ndef grayScaleImage(ImageId,folder='../input/stage1_train/'):\n    f = misc.imread(folder+ImageId+'/images/'+ImageId+'.png',flatten=False)\n    l=f.shape[0]\n    array_image=f.flatten()\n    array_image_mean=[]\n    for i in range(0,len(array_image),f.shape[2]):\n        array_image_mean.append((0.0+array_image[i]+array_image[i+1]+array_image[i+2]+0)/3)\n    n=len(array_image_mean)\n    image_features = []\n    for i in range(0,len(array_image_mean)):\n        image_features.append([array_image_mean[i]])\n    \n    print('Gray scale image : ')\n    plt.imshow(f)\n    plt.show()\n    return np.array(image_features)\n","execution_count":32,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77532334a0395a1aab42f5933ffa24d14a642c7b"},"cell_type":"code","source":"plt.hist(grayScaleImage(ImageId))","execution_count":33,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62d8570dede1423a4961412990833e85e73a9106"},"cell_type":"code","source":"binarize(grayScaleImage(ImageId),27,heigh)","execution_count":52,"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}