{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":1,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import cv2\nimport pandas as pd\nimport numpy as np\nfrom skimage.morphology import closing, square\nfrom skimage.segmentation import clear_border\nfrom skimage.measure import label, regionprops\nfrom skimage.filters import threshold_otsu\nfrom skimage import data\nfrom skimage.color import label2rgb\nimport matplotlib.patches as mpatches\nimport matplotlib.pyplot as plt","execution_count":2,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"373093bc4d0de7e9365e1a0c7581671383de1327"},"cell_type":"code","source":"image_path = \"../input/stage2_test_final/0a8dfb6763ec049b57454e6b5605f872cdf41cd13951fb2a2f31a6e3c8667712/images/0a8dfb6763ec049b57454e6b5605f872cdf41cd13951fb2a2f31a6e3c8667712.png\"","execution_count":5,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9a627ed84113ad3bd2eec84f8cf3b03e4fea39d"},"cell_type":"code","source":"image = cv2.imread(image_path)\nimage = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)","execution_count":6,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e715668bd53e38b2dbf7bf3a102d96135b196bb"},"cell_type":"code","source":"plt.imshow(image)\nplt.show()","execution_count":7,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"11d3db8ee2416327f6aa97e3bbb2c908331d4e9c"},"cell_type":"code","source":"# apply threshold\nthresh = threshold_otsu(image)\nif thresh:\n    bw = closing(image > thresh, square(3))\nelse:\n    ret, bw = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY)","execution_count":8,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6de13ad56d64a832da0767910921d6ddf3fd316a"},"cell_type":"code","source":"print(bw)\nplt.imshow(bw)\nprint(thresh)","execution_count":9,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"cf28e7af117fa3783440e55650aff2df1ecf6805"},"cell_type":"code","source":"def rle_encoding(x):\n    '''\n    x: numpy array of shape (height, width), 1 - mask, 0 - background\n    Returns run length as list\n    '''\n    dots = np.where(x.T.flatten()==1)[0] # .T sets Fortran order down-then-right\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if (b>prev+1): run_lengths.extend((b+1, 0))\n        run_lengths[-1] += 1\n        prev = b\n#     print(run_lengths)\n    return run_lengths","execution_count":10,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df20808012ac8667dc5810442ebf32d94597d6de"},"cell_type":"code","source":"# label image regions\nlabel_image = label(bw)\nplt.imshow(label_image)\nimag_label_overlay = label2rgb(label_image, image=image)\n\nfig, ax = plt.subplots(figsize=(10, 6))\nax.imshow(imag_label_overlay)\n\nfor region in regionprops(label_image):\n    # take regions with large enough areas\n    temp_image_null = np.zeros(label_image.shape)\n    for coord in region.coords:\n        temp_image_null[coord[0]][coord[1]] = 1\n#     print(temp_image_null)\n    plt.imshow(temp_image_null)\n    plt.show()\n    rle_encoding(temp_image_null)\n        \nax.set_axis_off()\nplt.tight_layout()\nplt.show()","execution_count":13,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5d86b680b632868843a7fb4942ba798f516345f2"},"cell_type":"code","source":"import glob","execution_count":14,"outputs":[]},{"metadata":{"_uuid":"6efd9e091720c56f3a66ec4d6092564de4870777"},"cell_type":"markdown","source":"###  Now using clustering for different nuclei in the cells\n#### Storing the run length encoded results in clustering_submit.csv"},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"afc9e2a339f03c82c207eb10082592304a3daa99"},"cell_type":"code","source":"df_array = []\nimage_count = 0\nfor image in glob.glob(\"../input/stage2_test_final/*/images/*\"):\n    image_count += 1\n    image_path = image\n    \n    # convert image to bw\n    input_image = cv2.imread(image_path)\n    input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2GRAY)\n    # apply threshold\n    thresh = threshold_otsu(input_image)\n    if thresh:\n        bw = closing(input_image > thresh, square(3))\n    else:\n        ret, bw = cv2.threshold(input_image, 0, 255, cv2.THRESH_BINARY)\n        \n    # label image regions\n    label_image = label(bw)\n\n    count = 0\n    for region in regionprops(label_image):\n        # take regions with large enough areas\n        if region.area > 70:\n            count += 1\n            temp_image_null = np.zeros(label_image.shape)\n            for coord in region.coords:\n                temp_image_null[coord[0]][coord[1]] = 1\n                \n            if len(np.unique(temp_image_null)) > 1:\n                array = rle_encoding(temp_image_null)\n                rle_string = str(array).replace(\",\", \"\").strip(\"[\").strip(\"]\")\n                temp_df = pd.DataFrame([{\"ImageID\": image_path.split(\"/\")[-1].split(\".\")[0], \"EncodedPixels\": rle_string}], columns=[\"ImageID\", \"EncodedPixels\"])\n                df_array.append(temp_df)\n#                 print(temp_df)\n    print(image_count, image_path.split(\"/\")[-1].split(\".\")[0], count)\ndf = pd.concat(df_array, ignore_index=True)","execution_count":16,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0d7fc86debb44b4bbb22dbe58b2daa314af7eb50"},"cell_type":"code","source":"df.head(10)","execution_count":17,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"be07767328cfa196045d6c059a8ac33c7d781bea"},"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","execution_count":18,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"37207ac9db6d18dd24ac1339a08f65621fa8347c"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}