{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport gc\nimport cv2\nimport tqdm\nimport glob\nimport numpy as np\nfrom matplotlib import pyplot as plt\nimport tifffile as tiff\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from pylab import rcParams\nrcParams['figure.figsize'] = 10, 10\nrcParams['figure.figsize'] = 10, 10","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for path in glob.glob('/kaggle/input/hubmap-kidney-segmentation/train/*.tiff'):\n    box_size = 1024\n    img = tiff.imread(path)\n\n    if len(img.shape) == 5: img = np.transpose(img.squeeze(), (1,2,0))\n\n    x_pad = box_size - (img.shape[0] % box_size)\n    xsp = x_pad // 2\n    xep = x_pad - xsp\n    y_pad = box_size - (img.shape[1] % box_size)\n    ysp = y_pad // 2\n    yep = y_pad - ysp\n\n    img = np.pad(img, ((xsp,xep),(ysp, yep), (0,0)), 'constant')\n    img[img == 0] = 220\n    mini_img = cv2.resize(img, (1024,1024))\n    shifted = cv2.pyrMeanShiftFiltering(mini_img, 25, 500, 1)\n    shifted = shifted.mean(axis=-1) < mini_img.mean() * 1.1\n    \n    group_size = 8\n    idxs = np.round(np.linspace(0, mini_img.shape[0], int(mini_img.shape[0]/group_size))).astype(int)\n    idys = np.round(np.linspace(0, mini_img.shape[1], int(mini_img.shape[1]/group_size))).astype(int)\n    avg_img = np.zeros((len(idxs), len(idys)))\n\n    for idx in range(len(idxs) - 1):\n        for idy in range(len(idys) - 1):\n            xs = idxs[idx]\n            xe = idxs[idx+1]\n            ys = idys[idy]\n            ye = idys[idy+1]\n            img_sel = shifted[xs:xe, ys:ye]\n            avg_img[idx+1, idy+1] = img_sel.mean() > 0.0\n# commented out due to size and image \n#     avg_img = cv2.resize(avg_img, (img.shape[1], img.shape[0]), cv2.INTER_NEAREST) > 0.0\n#     plt.imshow(img)\n#     plt.imshow(avg_img, cmap='jet', alpha=0.5)\n    avg_img = cv2.resize(avg_img, (mini_img.shape[1], mini_img.shape[0]), cv2.INTER_NEAREST) > 0.0\n    plt.imshow(mini_img)\n    plt.imshow(avg_img, cmap='jet', alpha=0.5)\n    plt.show()\n    gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}