{"cells":[{"metadata":{"_uuid":"4f868a899a2a40d95960bb13df38f9c15bdbf25f"},"cell_type":"markdown","source":"We've noticed our dataset contains images of different sizes, so it would be a good idea to scale them all to one size. 250x250px comes as a logical choice, being that a large number of our images are already in that shape. \nSome images have colour, others don't. So perhaps convert them all to grayscale. I'm not sure about this, perhaps there are more benefits in extra information from color than there are from making data uniform. But let's go like this for a start, it's easier. "},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","collapsed":true,"trusted":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\nfrom tqdm import tqdm\nfrom matplotlib import pyplot as plt\n\nimport skimage.io\nimport skimage.transform\nimport skimage.color\nimport skimage.exposure\n\n# Any results you write to the current directory are saved as output.","execution_count":92,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33a01244b3d1bc05851a44b44035b739986ae54c"},"cell_type":"code","source":"trainPath = '../input/stage1_train/'\ntestPath = '../input/stage1_test/'","execution_count":93,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"6698025a6c49c8ff136b60aa81266468050f2532"},"cell_type":"code","source":"def mergeMasks(imageRoot,imageDirName):\n    \n    rM,dM,fM=os.walk(imageRoot+imageDirName+\"/masks/\").__next__()\n    maskList=[rM+mask for mask in fM]\n    masks=skimage.io.imread_collection(maskList).concatenate()\n    s=masks.shape\n    \n   # Start with a matrix of size the same as the image (or masks)\n    labels=np.zeros((s[1],s[2]),np.uint16)\n    for i in range(s[0]):\n        labels[masks[i]>0]=i+1\n        \n    return labels","execution_count":94,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true},"cell_type":"markdown","source":"## Resize to uniform and convert to grayscale"},{"metadata":{"trusted":true,"_uuid":"dc5aa77fe123711ba4f951be28ab655b3dc5a841"},"cell_type":"code","source":"height=250\nwidth=250\ndesiredShape=(height,width)\n\nresizedImagesTrain=[]\nresizedMasksTrain=[]\nresizedImagesTest=[]\n\nrootTrain,dirsTrain,filesTrain=os.walk(trainPath).__next__()\n\nrootTest,dirsTest,filesTest=os.walk(testPath).__next__()\n\n\n# Resize train set\nprint(\"Processing images in the training directory...\")\nfor id in tqdm(dirsTrain):\n    tempImg=skimage.io.imread(rootTrain+id+\"/images/\"+id+\".png\")\n    tempImg=skimage.transform.resize(tempImg,desiredShape)\n    tempImg=skimage.color.rgb2gray(tempImg)\n    resizedImagesTrain.append(tempImg)\n    \n# Resize train masks\nprint(\"Resizing masks in the training directory...\")\nfor id in tqdm(dirsTrain):\n    allInOneMasks=mergeMasks(rootTrain,id)\n    tempImg=skimage.transform.resize(allInOneMasks,desiredShape)\n    resizedMasksTrain.append(tempImg)\n\n# Resize test set \nprint(\"Processing images in the test directory...\")    \nfor id in tqdm(dirsTest):\n    tempImg=skimage.io.imread(rootTest+id+\"/images/\"+id+\".png\")\n    tempImg=skimage.transform.resize(tempImg,desiredShape)\n    tempImg=skimage.color.rgb2gray(tempImg)\n    resizedImagesTest.append(tempImg)\n    \n\n","execution_count":95,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e25e9a4188938f9819a719c4efbbf821e379c41"},"cell_type":"code","source":"# So our data is now in resizedImagesTrain, resizedMasksTrain and resizedImagesTest\nprint(\"--- Resized Images Train --- \\n count: {} \\n data type: {} \\n image shape: {}\".format(len(resizedImagesTrain),\n                                                                                         type(resizedImagesTrain[0]),\n                                                                                         resizedImagesTrain[0].shape))\nprint(\"--- Resized Masks Train --- \\n count: {} \\n data type: {} \\n image shape: {}\".format(len(resizedMasksTrain),\n                                                                                        type(resizedMasksTrain[0]),\n                                                                                        resizedMasksTrain[0].shape))\nprint(\"--- Resized Images Test --- \\n count: {} \\n data type: {} \\n image shape: {}\".format(len(resizedImagesTest),\n                                                                                        type(resizedImagesTest[0]),\n                                                                                        resizedImagesTest[0].shape))","execution_count":48,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f2ebcd0b73d3eebe736a3a00f43c30b6ca4349b1"},"cell_type":"code","source":"plt.subplot(121)\nplt.imshow(skimage.io.imread(rootTrain+dirsTrain[44]+\"/images/\"+dirsTrain[44]+\".png\"))\nplt.title(\"Original image\")\n\nplt.subplot(122)\nplt.imshow(resizedImagesTrain[44],cmap=\"gray\")\nplt.title(\"Resized and cast to gray\")\nplt.show()\n","execution_count":96,"outputs":[]},{"metadata":{"_uuid":"5dd8285f4bf59b27756fec5b056937225bb81417"},"cell_type":"markdown","source":"## Image enhancements?\nEnhance contrast by equalizing the image histograms. Skimage.exposure offers an adaptive version that seems cool."},{"metadata":{"trusted":true,"_uuid":"3c955ba273f78728160b2806a6c8ef8157ba04fb"},"cell_type":"code","source":"# Histogram equalization\n\nprint(\"Equalizing training images...\")\nequalizedTrain=[skimage.exposure.equalize_adapthist(x) for x in tqdm(resizedImagesTrain)]\n\nprint(\"Equalizing test images...\")\nequalizedTest=[skimage.exposure.equalize_adapthist(x) for x in tqdm(resizedImagesTest)]\n\n\n","execution_count":97,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e58ffb6f5a58b56d1f9e60b4cb78fc8a7281289"},"cell_type":"code","source":"# check on an example\nimId=458\n\nplt.subplot(1,3,1)\nplt.imshow(resizedImagesTrain[imId],cmap=\"gray\")\n\nplt.subplot(1,3,2)\nplt.imshow(equalizedTrain[imId],cmap=\"gray\")\n\nplt.subplot(1,3,3)\nplt.imshow(resizedMasksTrain[imId])\nplt.show()","execution_count":98,"outputs":[]},{"metadata":{"_uuid":"753fd60a6fa1e68c2a55819e518877ef5f7e35eb"},"cell_type":"markdown","source":""}],"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}