{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom skimage import data\nimport matplotlib.pyplot as plt\n\nimport skimage.data as data\nimport skimage.segmentation as seg\nimport skimage.filters as filters\nimport skimage.draw as draw\nimport skimage.color as color\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-14T02:01:09.443927Z","iopub.execute_input":"2021-12-14T02:01:09.445092Z","iopub.status.idle":"2021-12-14T02:01:11.537328Z","shell.execute_reply.started":"2021-12-14T02:01:09.444925Z","shell.execute_reply":"2021-12-14T02:01:11.536695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:01:11.539559Z","iopub.execute_input":"2021-12-14T02:01:11.539882Z","iopub.status.idle":"2021-12-14T02:01:11.545116Z","shell.execute_reply.started":"2021-12-14T02:01:11.539840Z","shell.execute_reply":"2021-12-14T02:01:11.544194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/sartorius-cell-instance-segmentation/train.csv')\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:01:11.546691Z","iopub.execute_input":"2021-12-14T02:01:11.547237Z","iopub.status.idle":"2021-12-14T02:01:12.214522Z","shell.execute_reply.started":"2021-12-14T02:01:11.547195Z","shell.execute_reply":"2021-12-14T02:01:12.213736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EDA","metadata":{}},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:01:12.216822Z","iopub.execute_input":"2021-12-14T02:01:12.217307Z","iopub.status.idle":"2021-12-14T02:01:12.298632Z","shell.execute_reply.started":"2021-12-14T02:01:12.217262Z","shell.execute_reply":"2021-12-14T02:01:12.297786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len([*df_train['annotation']])","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:01:12.299897Z","iopub.execute_input":"2021-12-14T02:01:12.300692Z","iopub.status.idle":"2021-12-14T02:01:12.323210Z","shell.execute_reply.started":"2021-12-14T02:01:12.300655Z","shell.execute_reply":"2021-12-14T02:01:12.322365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of Annotation =\", df_train[df_train.id=='0030fd0e6378'].shape[0])","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:01:12.324506Z","iopub.execute_input":"2021-12-14T02:01:12.325349Z","iopub.status.idle":"2021-12-14T02:01:12.350082Z","shell.execute_reply.started":"2021-12-14T02:01:12.325305Z","shell.execute_reply":"2021-12-14T02:01:12.349082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of Annotation =\", df_train[df_train.id=='03b27b381a5f'].shape[0])","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:01:12.351670Z","iopub.execute_input":"2021-12-14T02:01:12.352173Z","iopub.status.idle":"2021-12-14T02:01:12.373788Z","shell.execute_reply.started":"2021-12-14T02:01:12.352128Z","shell.execute_reply":"2021-12-14T02:01:12.372972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.cell_type.unique()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:01:12.374999Z","iopub.execute_input":"2021-12-14T02:01:12.375283Z","iopub.status.idle":"2021-12-14T02:01:12.394311Z","shell.execute_reply.started":"2021-12-14T02:01:12.375253Z","shell.execute_reply":"2021-12-14T02:01:12.393444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[df_train.cell_type=='shsy5y'].sample_id.unique().shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:01:12.396112Z","iopub.execute_input":"2021-12-14T02:01:12.396759Z","iopub.status.idle":"2021-12-14T02:01:12.429227Z","shell.execute_reply.started":"2021-12-14T02:01:12.396714Z","shell.execute_reply":"2021-12-14T02:01:12.428344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Bounding Box","metadata":{}},{"cell_type":"code","source":"def get_Bounding_Box(annotate):\n    '''\n    Returns [(x1,y1), (x2, y2))]\n    '''\n    shape = (704, 520)\n    img = np.zeros((shape[0] * shape[1]), dtype=np.float32)\n    s = annotate.split()\n    starts = list(map(lambda x: int(x) - 1, s[0::2]))\n    lengths = list(map(int, s[1::2]))\n    ends = [x + y for x, y in zip(starts, lengths)]\n    temp_img = np.zeros((shape[0] * shape[1]), dtype=np.float32)\n    for start, end in zip(starts, ends):\n        temp_img[start : end] = 1\n    p2 = np.where(temp_img==1)[0][0]//704\n    p4 = np.where(temp_img==1)[0][-1]//704\n    temp_img = np.transpose(temp_img.reshape(520, 704)).reshape(shape[0] * shape[1])\n    p1 = np.where(temp_img==1)[0][0]//520\n    p3 = np.where(temp_img==1)[0][-1]//520        \n    \n    return ((p1, p2),(p3,p4))","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:01:12.433316Z","iopub.execute_input":"2021-12-14T02:01:12.433733Z","iopub.status.idle":"2021-12-14T02:01:12.445686Z","shell.execute_reply.started":"2021-12-14T02:01:12.433686Z","shell.execute_reply":"2021-12-14T02:01:12.445071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotate_list=[]\nfor annotate in [*df_train['annotation']]:\n    annotate_list.append(get_Bounding_Box(annotate))\n\ndf_train['box'] = annotate_list\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:01:12.447075Z","iopub.execute_input":"2021-12-14T02:01:12.447556Z","iopub.status.idle":"2021-12-14T02:02:37.416947Z","shell.execute_reply.started":"2021-12-14T02:01:12.447513Z","shell.execute_reply":"2021-12-14T02:02:37.415995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_show(image, nrows=1, ncols=1, color_map='gray', title='default image'):\n    fig, ax = plt.subplots(nrows=nrows, ncols=ncols, figsize=(7, 7))\n    ax.imshow(image, cmap=color_map)\n    ax.axis('off')\n    return fig, ax","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:02:37.418357Z","iopub.execute_input":"2021-12-14T02:02:37.418717Z","iopub.status.idle":"2021-12-14T02:02:37.425563Z","shell.execute_reply.started":"2021-12-14T02:02:37.418675Z","shell.execute_reply":"2021-12-14T02:02:37.424498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plt.imread('../input/sartorius-cell-instance-segmentation/train/0140b3c8f445.png')\nimage_show(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:02:37.426641Z","iopub.execute_input":"2021-12-14T02:02:37.426842Z","iopub.status.idle":"2021-12-14T02:02:37.681295Z","shell.execute_reply.started":"2021-12-14T02:02:37.426818Z","shell.execute_reply":"2021-12-14T02:02:37.680620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create annotation mask","metadata":{}},{"cell_type":"code","source":"def create_Mask(image_id, index=None):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height, width, channels) of array to return \n    color: color for the mask\n    Returns numpy array (mask)\n    '''\n    points_list=[]\n    value=1\n    labels = df_train[df_train[\"id\"] == image_id][\"annotation\"].tolist()\n    boxs = df_train[df_train[\"id\"] == image_id][\"box\"].tolist()\n    height = df_train[df_train[\"id\"] == image_id]['height']\n    height = int(height.unique())\n    width = df_train[df_train[\"id\"] == image_id]['width']\n    width = int(width.unique())\n    shape = (height, width)\n    img = np.zeros((shape[0] * shape[1]), dtype=np.float32)\n    for label, box in zip(labels, boxs):\n        s = label.split()\n        starts = list(map(lambda x: int(x) - 1, s[0::2]))\n        lengths = list(map(int, s[1::2]))\n        ends = [x + y for x, y in zip(starts, lengths)]\n        for start, end in zip(starts, ends):\n            img[start : end] = value\n        points_list.append(box)\n        value += 1\n        \n    mask = img.reshape(shape[0], shape[1])\n    \n    return mask, points_list","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:02:37.682224Z","iopub.execute_input":"2021-12-14T02:02:37.682719Z","iopub.status.idle":"2021-12-14T02:02:37.692635Z","shell.execute_reply.started":"2021-12-14T02:02:37.682691Z","shell.execute_reply":"2021-12-14T02:02:37.691804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = '1e7ac54a9422'\nmask, points = create_Mask(image_id)\nimage_show(mask, color_map=None)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:06:14.155225Z","iopub.execute_input":"2021-12-14T02:06:14.155958Z","iopub.status.idle":"2021-12-14T02:06:14.371909Z","shell.execute_reply.started":"2021-12-14T02:06:14.155911Z","shell.execute_reply":"2021-12-14T02:06:14.371048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimage = mask.copy()\nfor point in points:\n    image = cv2.rectangle(image, point[0], point[1], (225,0,0), 2)\n    \nplt.figure(figsize=(15,15))\nplt.imshow(mask)\nplt.imshow(image, alpha=0.5)\n\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:06:16.927848Z","iopub.execute_input":"2021-12-14T02:06:16.928839Z","iopub.status.idle":"2021-12-14T02:06:17.295925Z","shell.execute_reply.started":"2021-12-14T02:06:16.928794Z","shell.execute_reply":"2021-12-14T02:06:17.295071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filters","metadata":{}},{"cell_type":"code","source":"from skimage.filters import gaussian, median, unsharp_mask\nfrom skimage import io, img_as_float, img_as_ubyte\nfrom skimage.morphology import disk\nfrom skimage.restoration import denoise_bilateral, denoise_nl_means, estimate_sigma, denoise_tv_chambolle","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:02:38.557435Z","iopub.execute_input":"2021-12-14T02:02:38.557630Z","iopub.status.idle":"2021-12-14T02:02:38.976016Z","shell.execute_reply.started":"2021-12-14T02:02:38.557606Z","shell.execute_reply":"2021-12-14T02:02:38.975280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.id.unique()[0]","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:02:38.977184Z","iopub.execute_input":"2021-12-14T02:02:38.977399Z","iopub.status.idle":"2021-12-14T02:02:38.988532Z","shell.execute_reply.started":"2021-12-14T02:02:38.977374Z","shell.execute_reply":"2021-12-14T02:02:38.987993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_path = \"../input/sartorius-cell-instance-segmentation/train/\"\nimg_id = df_train.id.unique()[30]\nimg = img_as_float(io.imread(dir_path+img_id+\".png\", as_gray=True))\nimage_show(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:02:38.989849Z","iopub.execute_input":"2021-12-14T02:02:38.990089Z","iopub.status.idle":"2021-12-14T02:02:39.234424Z","shell.execute_reply.started":"2021-12-14T02:02:38.990059Z","shell.execute_reply":"2021-12-14T02:02:39.233555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_2_show(image, filter_img, title, color_map='gray'):\n    fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(15, 15))\n    ax[0].imshow(image, cmap=color_map)\n    ax[1].imshow(filter_img, cmap=color_map)\n    ax[2].imshow(filter_img+image, cmap=color_map)\n    ax[0].title.set_text('Default image')\n    ax[1].title.set_text(title)\n    ax[2].title.set_text(title+\" and default image\")\n    ax[0].axis('off')\n    ax[1].axis('off')\n    ax[2].axis('off')\n    return fig, ax","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:02:39.235677Z","iopub.execute_input":"2021-12-14T02:02:39.235964Z","iopub.status.idle":"2021-12-14T02:02:39.244607Z","shell.execute_reply.started":"2021-12-14T02:02:39.235926Z","shell.execute_reply":"2021-12-14T02:02:39.244075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Gaussian Blur Denoising Filter**","metadata":{}},{"cell_type":"code","source":"img_gaussian_filter = gaussian(img, sigma=1, mode='constant', cval=1.0)\nimage_2_show(img, img_gaussian_filter, title='Gaussian Filter')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:02:39.245550Z","iopub.execute_input":"2021-12-14T02:02:39.246089Z","iopub.status.idle":"2021-12-14T02:02:39.636957Z","shell.execute_reply.started":"2021-12-14T02:02:39.246056Z","shell.execute_reply":"2021-12-14T02:02:39.636089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Median Filter","metadata":{}},{"cell_type":"code","source":"img_median_filter = median(img, disk(1), mode='constant', cval=0.0)\nimage_2_show(img, img_median_filter, title='Median Filter')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:02:39.638336Z","iopub.execute_input":"2021-12-14T02:02:39.638845Z","iopub.status.idle":"2021-12-14T02:02:40.034414Z","shell.execute_reply.started":"2021-12-14T02:02:39.638796Z","shell.execute_reply":"2021-12-14T02:02:40.033527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Bilateral Filter**","metadata":{}},{"cell_type":"code","source":"img_bilateral_filter = denoise_bilateral(img, sigma_color=0.05, sigma_spatial=15, multichannel=False)\nimage_2_show(img, img_bilateral_filter, title='Bilateral Filter')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:02:40.035492Z","iopub.execute_input":"2021-12-14T02:02:40.035703Z","iopub.status.idle":"2021-12-14T02:03:35.241055Z","shell.execute_reply.started":"2021-12-14T02:02:40.035678Z","shell.execute_reply":"2021-12-14T02:03:35.240241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**NLM Filter**","metadata":{}},{"cell_type":"code","source":"sigma_est = np.mean(estimate_sigma(img, multichannel=True))\ndenoise_img = denoise_nl_means(img, h=1.15 * sigma_est, fast_mode=True,\n                               patch_size=5, patch_distance=3, multichannel=False)\nimage_2_show(img, denoise_img, title='None Local Mean Filter')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:03:35.242609Z","iopub.execute_input":"2021-12-14T02:03:35.243076Z","iopub.status.idle":"2021-12-14T02:03:35.992242Z","shell.execute_reply.started":"2021-12-14T02:03:35.243018Z","shell.execute_reply":"2021-12-14T02:03:35.991413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Total Variation Filter**","metadata":{}},{"cell_type":"code","source":"TV_img = denoise_tv_chambolle(img, weight=0.1, eps=0.0002, n_iter_max=200, multichannel=False)\nimage_2_show(img, TV_img, title='Total Variation (TV) Filter')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:03:35.993373Z","iopub.execute_input":"2021-12-14T02:03:35.993593Z","iopub.status.idle":"2021-12-14T02:03:36.578849Z","shell.execute_reply.started":"2021-12-14T02:03:35.993556Z","shell.execute_reply":"2021-12-14T02:03:36.578059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Unsharp Filter**","metadata":{}},{"cell_type":"code","source":"img_gaussian_filter = gaussian(img, sigma=1, mode='constant', cval=1.0)\nunsharp_img = unsharp_mask(img, radius=5, amount=1.0)\nimage_2_show(img, unsharp_img, title='Unsharp Filter')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:03:36.579949Z","iopub.execute_input":"2021-12-14T02:03:36.580217Z","iopub.status.idle":"2021-12-14T02:03:36.990263Z","shell.execute_reply.started":"2021-12-14T02:03:36.580187Z","shell.execute_reply":"2021-12-14T02:03:36.989284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.filters import roberts, sobel, scharr, prewitt, farid\nroberts_img = roberts(img)\nsobel_img = sobel(img)\nscharr_img = scharr(img)\nprewitt_img = prewitt(img)\nfarid_img = farid(img)\nimage_2_show(img, roberts_img, title='Roberts Filter')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:03:36.991842Z","iopub.execute_input":"2021-12-14T02:03:36.992311Z","iopub.status.idle":"2021-12-14T02:03:37.430172Z","shell.execute_reply.started":"2021-12-14T02:03:36.992264Z","shell.execute_reply":"2021-12-14T02:03:37.429592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.filters import meijering, sato, frangi, hessian\nmeijering_img = meijering(img)\nsato_img = sato(img)\nfrangi_img = frangi(img)\nhessian_img = hessian(img)\nmask = create_Mask(img_id)\nimage_2_show(img, meijering_img, title='Meijering Filter')\nimage_2_show(sato_img, hessian_img, title='Hessian Filter')\n# image_2_show(frangi_img, mask, title='Mask Filter')\n\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:03:37.433267Z","iopub.execute_input":"2021-12-14T02:03:37.433585Z","iopub.status.idle":"2021-12-14T02:03:39.764243Z","shell.execute_reply.started":"2021-12-14T02:03:37.433556Z","shell.execute_reply":"2021-12-14T02:03:39.763360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Annotation","metadata":{}},{"cell_type":"code","source":"image_id = '0140b3c8f445'\nmask, _= create_Mask(image_id, 1)\nimage_show(mask, color_map=None)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:07:27.648803Z","iopub.execute_input":"2021-12-14T02:07:27.649715Z","iopub.status.idle":"2021-12-14T02:07:27.879699Z","shell.execute_reply.started":"2021-12-14T02:07:27.649664Z","shell.execute_reply":"2021-12-14T02:07:27.878810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imsave('mask.png', mask)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:07:29.314480Z","iopub.execute_input":"2021-12-14T02:07:29.314778Z","iopub.status.idle":"2021-12-14T02:07:29.356281Z","shell.execute_reply.started":"2021-12-14T02:07:29.314747Z","shell.execute_reply":"2021-12-14T02:07:29.355385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mask_annotat(mask):\n    img = mask.reshape(mask.shape[0]*mask.shape[1])\n    annotation = []\n    flag = 0\n    for i in range(img.shape[0]):\n        if flag==0 and img[i]==1:\n            annotation.append(i)\n            flag = 1\n        elif flag==1 and img[i]==0:\n            annotation.append(i - annotation[-1])\n            flag=0\n        else:\n            pass\n\n    return \" \".join([str(x) for x in annotation])","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:07:30.470763Z","iopub.execute_input":"2021-12-14T02:07:30.471655Z","iopub.status.idle":"2021-12-14T02:07:30.479742Z","shell.execute_reply.started":"2021-12-14T02:07:30.471605Z","shell.execute_reply":"2021-12-14T02:07:30.478839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_annotat(mask)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:07:34.601508Z","iopub.execute_input":"2021-12-14T02:07:34.602314Z","iopub.status.idle":"2021-12-14T02:07:35.938409Z","shell.execute_reply.started":"2021-12-14T02:07:34.602269Z","shell.execute_reply":"2021-12-14T02:07:35.937470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## END :)","metadata":{}}]}