{"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":"markdown","source":"# My method to delete the blank space of pictures.\n\n- I use cv2.findContours to get the coordinate of targets.\n- However, if I do that to the original picture, cv2 pick up such a tiny unuseful target by mistake.\n- Therefore, by dilateing the targets using cv2.dilate function, I could get only good targets.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-30T00:37:48.39276Z","iopub.execute_input":"2022-08-30T00:37:48.393212Z","iopub.status.idle":"2022-08-30T00:38:04.110567Z","shell.execute_reply.started":"2022-08-30T00:37:48.393125Z","shell.execute_reply":"2022-08-30T00:38:04.109076Z"}}},{"cell_type":"code","source":"!pip install imutils","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \n\nfrom imutils import contours\nfrom matplotlib import pyplot as plt\nimport tifffile as tifi\nimport gc\n\nimport os\nfrom PIL import Image\nimport cv2\n\nINPUT_PATH = \"../input/mayo-clinic-strip-ai\"\n\ndf_train = pd.read_csv(os.path.join(INPUT_PATH + '/train.csv'))","metadata":{"execution":{"iopub.status.busy":"2022-08-30T00:38:04.114093Z","iopub.execute_input":"2022-08-30T00:38:04.115397Z","iopub.status.idle":"2022-08-30T00:38:04.475036Z","shell.execute_reply.started":"2022-08-30T00:38:04.115352Z","shell.execute_reply":"2022-08-30T00:38:04.474154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sizes = []\n# for id in df_train.image_id:\n#     size = os.path.getsize(INPUT_PATH + '/train/' + id + '.tif')\n#     sizes.append(size)\n    \n# df_train['file_size'] = sizes","metadata":{"execution":{"iopub.status.busy":"2022-08-30T00:38:04.478903Z","iopub.execute_input":"2022-08-30T00:38:04.479285Z","iopub.status.idle":"2022-08-30T00:38:05.072999Z","shell.execute_reply.started":"2022-08-30T00:38:04.479251Z","shell.execute_reply":"2022-08-30T00:38:05.07201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df = df_train.sort_values('file_size', ascending=False).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T00:38:05.095554Z","iopub.execute_input":"2022-08-30T00:38:05.096436Z","iopub.status.idle":"2022-08-30T00:38:05.104706Z","shell.execute_reply.started":"2022-08-30T00:38:05.096393Z","shell.execute_reply":"2022-08-30T00:38:05.103838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df.tail()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T00:38:05.106252Z","iopub.execute_input":"2022-08-30T00:38:05.107149Z","iopub.status.idle":"2022-08-30T00:38:05.119934Z","shell.execute_reply.started":"2022-08-30T00:38:05.107115Z","shell.execute_reply":"2022-08-30T00:38:05.118793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Example image","metadata":{}},{"cell_type":"code","source":"image_id = 'fd3079_0'","metadata":{"execution":{"iopub.status.busy":"2022-08-30T00:38:05.121331Z","iopub.execute_input":"2022-08-30T00:38:05.121638Z","iopub.status.idle":"2022-08-30T00:38:05.125472Z","shell.execute_reply.started":"2022-08-30T00:38:05.121611Z","shell.execute_reply":"2022-08-30T00:38:05.124629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = os.path.join(INPUT_PATH, f\"train/{image_id}.tif\")","metadata":{"execution":{"iopub.status.busy":"2022-08-30T00:38:05.126676Z","iopub.execute_input":"2022-08-30T00:38:05.127504Z","iopub.status.idle":"2022-08-30T00:38:05.13713Z","shell.execute_reply.started":"2022-08-30T00:38:05.127472Z","shell.execute_reply":"2022-08-30T00:38:05.135917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = tifi.imread(image_path)\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\nfig, ax = plt.subplots(figsize=(12,12))\nax.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T00:38:05.13821Z","iopub.execute_input":"2022-08-30T00:38:05.138924Z","iopub.status.idle":"2022-08-30T00:38:49.518643Z","shell.execute_reply.started":"2022-08-30T00:38:05.138893Z","shell.execute_reply":"2022-08-30T00:38:49.517432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- kernel is how much you want to dilate the targets. To determine it is a little bothersome.\n- before doing cv2.dilate, you have to change the image grayscale.","metadata":{}},{"cell_type":"code","source":"kernel = np.ones((100, 100),np.uint8)\n\nimg = tifi.imread(image_path)\nimgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\nret,thresh = cv2.threshold(imgray, 125, 255, cv2.THRESH_BINARY_INV)\nthresh = cv2.dilate(thresh,kernel,iterations = 1) \nthresh = cv2.copyMakeBorder(thresh,100,100,100,100,cv2.BORDER_CONSTANT,value=(0,0,0))\n\nfig, ax = plt.subplots(figsize=(12,12))\nax.imshow(thresh, cmap = \"gray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T01:11:47.607166Z","iopub.execute_input":"2022-08-30T01:11:47.607615Z","iopub.status.idle":"2022-08-30T01:12:05.754046Z","shell.execute_reply.started":"2022-08-30T01:11:47.607576Z","shell.execute_reply":"2022-08-30T01:12:05.752905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Above picture's white dots are detected by cv2.findContours.\n- Those coordnates are stored by contours.sort_contours.\n- contours.sort_contours have 2 types, 'top-to-bottom' and 'left-to-right'. I tried both of them, but results are not so different.","metadata":{}},{"cell_type":"code","source":"def get_each_picture(image_path, kernel):\n    '''Detect pathological samples area and return created pictures list.\n    Args:\n    image_file: image path\n    kernel:how much you want to dilate a letter to detect that correctly\n    '''\n    img = tifi.imread(image_path)\n    imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n    ret,thresh = cv2.threshold(imgray, 125, 255, cv2.THRESH_BINARY_INV)\n    del imgray\n    del ret\n    thresh = cv2.dilate(thresh,kernel,iterations = 1) \n    thresh = cv2.copyMakeBorder(thresh,100,100,100,100,cv2.BORDER_CONSTANT,value=(0,0,0))\n    cnts, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)\n    cnts_top_bottom, hierarchy = contours.sort_contours(cnts, method='top-to-bottom') \n    del thresh\n    del hierarchy\n    # x, y→coordinate of upper left, w→width, h→height\n    pictures_top_bottom = []\n    for contour in cnts_top_bottom:  \n        x, y, w, h = cv2.boundingRect(contour) \n\n        # image detection：store the coordinate of image\n        block_ROI_tb = img[y:y+h, x:x+w]\n        del x, y, w, h\n        \n        block_ROI_tb_gray = cv2.cvtColor(block_ROI_tb,cv2.COLOR_BGR2GRAY)\n        _, gray = cv2.threshold(block_ROI_tb_gray, 125, 255, cv2.THRESH_BINARY_INV)  \n        del block_ROI_tb_gray\n        u, count = np.unique(gray, return_counts=True)\n        if len(count) == 2:\n            # count[1] / (count[0]+count[1]) < 0.01 means the target is too little.\n            if count[1] / (count[0]+count[1]) > 0.01:\n                pictures_top_bottom.append(block_ROI_tb)\n        del block_ROI_tb\n    del cnts_top_bottom\n            \n    del img\n        \n    return pictures_top_bottom\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T00:53:24.412906Z","iopub.execute_input":"2022-08-30T00:53:24.413392Z","iopub.status.idle":"2022-08-30T00:53:24.425713Z","shell.execute_reply.started":"2022-08-30T00:53:24.413347Z","shell.execute_reply":"2022-08-30T00:53:24.42465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_image(image_path, list_tb, shape=(600,600)):\n    if not len(list_tb) == 1:    \n        for i, img in enumerate(list_tb):     \n            img_resized = cv2.resize(img, dsize=shape)\n            cv2.imwrite('./{}_{}.png'.format(image_path[-12:-4], i), img_resized)\n            del img, img_resized\n    else:\n        img_resized = cv2.resize(list_tb[0], dsize=shape)\n        cv2.imwrite('./{}.png'.format(image_path[-12:-4]), img_resized)\n        del img_resized\n    del list_tb\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T00:53:24.491203Z","iopub.execute_input":"2022-08-30T00:53:24.491833Z","iopub.status.idle":"2022-08-30T00:53:24.499305Z","shell.execute_reply.started":"2022-08-30T00:53:24.491798Z","shell.execute_reply":"2022-08-30T00:53:24.498049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kernel = np.ones((100, 100),np.uint8)\n\nlist_tb = get_each_picture(image_path, kernel)\ncreate_image(image_path, list_tb)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T01:07:59.743776Z","iopub.execute_input":"2022-08-30T01:07:59.744282Z","iopub.status.idle":"2022-08-30T01:08:07.564767Z","shell.execute_reply.started":"2022-08-30T01:07:59.744242Z","shell.execute_reply":"2022-08-30T01:08:07.563468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2022-08-30T01:08:45.947823Z","iopub.execute_input":"2022-08-30T01:08:45.948289Z","iopub.status.idle":"2022-08-30T01:08:47.190673Z","shell.execute_reply.started":"2022-08-30T01:08:45.948254Z","shell.execute_reply":"2022-08-30T01:08:47.189049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- created pictures are below. ","metadata":{}},{"cell_type":"code","source":"img = cv2.imread('fd3079_0_0.png')\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n# img = cv2.copyMakeBorder(img_1,100,100,100,100,cv2.BORDER_CONSTANT,value=(255,255,255))\n# image_resized = cv2.resize(img, (0,0), fx=0.05, fy=0.05)\n\nfig, ax = plt.subplots(figsize=(12,12))\nax.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T01:09:44.70791Z","iopub.execute_input":"2022-08-30T01:09:44.708562Z","iopub.status.idle":"2022-08-30T01:09:45.193069Z","shell.execute_reply.started":"2022-08-30T01:09:44.708528Z","shell.execute_reply":"2022-08-30T01:09:45.192168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('fd3079_0_1.png')\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n# img = cv2.copyMakeBorder(img_1,100,100,100,100,cv2.BORDER_CONSTANT,value=(255,255,255))\n# image_resized = cv2.resize(img, (0,0), fx=0.05, fy=0.05)\n\nfig, ax = plt.subplots(figsize=(12,12))\nax.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T01:10:10.277892Z","iopub.execute_input":"2022-08-30T01:10:10.279036Z","iopub.status.idle":"2022-08-30T01:10:10.608857Z","shell.execute_reply.started":"2022-08-30T01:10:10.278989Z","shell.execute_reply":"2022-08-30T01:10:10.607693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}