{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Output will be cropped images of every glomeruli in a zipfile....\n## Rember to run 3rd only once\n## the output images will help you to train model faster....\n[Link to notebook for rle2mask and a train_test.csv file](https://www.kaggle.com/realc9der/ecoding-of-glomeruli/edit)"},{"metadata":{"_uuid":"d9d5ba33-2087-4b55-beb2-7cdaa40e6865","_cell_guid":"d6c2aa95-1bbf-48c9-a9bb-eb8431ab2b69","trusted":true,"_kg_hide-input":true,"_kg_hide-output":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\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 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":"32063d0d-8d74-496e-a29a-ae811ad5eadc","_cell_guid":"14c96f39-7f8a-4eeb-901f-ebf834f3de51","trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"import os\nimport json\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sn\nimport cv2 as cv\nimport tifffile\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\"\nos.mkdir(\"../working/images\")\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# a cell that use all the above function and variable to create a the image cropping\ndef get_id(n):\n    return df_train.id[n]\n\n\ndef Coor_of_Glomeruli(ID,n):\n    df = pd.read_json(\"../input/hubmap-kidney-segmentation/train/\"+ID+\".json\")\n    return np.array(df.geometry[n][\"coordinates\"])[0,:,:],n\n\n\ndef get_image(ID):\n    return tifffile.imread(\"../input/hubmap-kidney-segmentation/train/\"+ID+\".tiff\")\n\n\ndef find_box(coor_array,h,w):\n    L_x = coor_array[0][0]\n    S_x = L_x\n    L_y = coor_array[0][1]\n    S_y = L_y\n    for i in range(len(coor_array)):\n        if L_x < coor_array[i][0]:\n            L_x = coor_array[i][0]\n    for i in range(len(coor_array)):\n        if S_x > coor_array[i][0]:\n            S_x = coor_array[i][0]\n    for i in range(len(coor_array)):\n        if L_y < coor_array[i][1]:\n            L_y = coor_array[i][1]\n    for i in range(len(coor_array)):\n        if S_y > coor_array[i][1]:\n            S_y = coor_arraty[i][1]\n    return L_x,S_x,L_y,S_y\n\n\ndef mask2rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels= img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n \n\n\n\ndef save_image(sub_image,image_id,no_of_glomeruli):\n    path = '../working/images/'+image_id+\"_\"+str(no_of_glomeruli)+'.png'\n    #ther many glomeruli in image so no_of_glomeruli represent that \n    cv.imwrite(path,sub_image)\n\n    \ndef total_glomeruli(ID):\n    df = pd.read_json(\"../input/hubmap-kidney-segmentation/train/\"+ID+\".json\")\n    return len(df.geometry)\n\n\n\ndef rle2mask(mask_rle,shape):\n    s = mask_rle.split()\n    starts, lengths = [\n    np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])\n    ]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo : hi] = 1\n    return img.reshape(shape).T\n\n########################\n########################\ndf_train = pd.read_csv(\"../input/hubmap-kidney-segmentation/train.csv\")\ntrain_data = pd.DataFrame(data=None,columns=['file_name','rle',],index=None) # creating a empyt data\n\npath = os.path.join(\"../working/images\")\n#os.mkdir(path,)\n########################\n########################\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e1067a24-af0f-4424-a7b4-2f25d1ba0bc3","_cell_guid":"5d3971da-4810-4f76-94b5-cafa3ef89875","trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"for i in range(len(df_train.id)):\n    image_id = get_id(i) #got the image id \n    #get the coordinates array of each glomeruli\n    image = tifffile.imread(\"../input/hubmap-kidney-segmentation/train/\"+image_id+\".tiff\")#get_image(image_id) #image\n    Total_Glomeruli = total_glomeruli(image_id)\n    w,h = image.shape[:2]\n    path = os.path.join(\"../working/cropped_images/\")\n#cropping images\n    for j in range(Total_Glomeruli):\n        coor_array,no_of_Glomeruli = Coor_of_Glomeruli(image_id,j)\n        #get the box\n        L_x,S_x,L_y,S_y = find_box(coor_array,h,w)\n        #crop the image\n        if S_x < w and S_y < h and L_x < w and L_y < h:\n            sub_image = image[S_y:L_y,S_x:L_x]    \n        # save the cropped image\n        save_image(sub_image,image_id,no_of_Glomeruli)\n        #start with new image\n\n    print(\"done Id \",image_id)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Now main task is to create a zip file with all the images\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"!zip -r cropped_images.zip ../working/images/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"go download\")","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}