{"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 os\nimport gc\nimport cv2\nimport tifffile\nimport numpy as np \nimport pandas as pd \nimport seaborn as sns\nimport warnings\nimport matplotlib.pyplot as plt\n%matplotlib inline\nwarnings.filterwarnings(\"ignore\")\ngc.enable()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-31T02:21:38.320499Z","iopub.execute_input":"2022-07-31T02:21:38.321628Z","iopub.status.idle":"2022-07-31T02:21:38.329981Z","shell.execute_reply.started":"2022-07-31T02:21:38.321583Z","shell.execute_reply":"2022-07-31T02:21:38.32887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dirs = [\"../input/mayo-clinic-strip-ai/train/\", \"../input/mayo-clinic-strip-ai/test/\"]\ntrain_df = pd.read_csv(\"../input/mayo-clinic-strip-ai/train.csv\")\nimg_id = train_df.iloc[0].image_id\n\nimg = tifffile.imread(dirs[1] + img_id + \".tif\")\nimg = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)\nprint(\"original size:\",img.shape)\n\nimg = cv2.resize(img,(int(img.shape[1]/100),int(img.shape[0]/100)))\nprint(\"resized:\",img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T02:21:38.458058Z","iopub.execute_input":"2022-07-31T02:21:38.459282Z","iopub.status.idle":"2022-07-31T02:22:10.612061Z","shell.execute_reply.started":"2022-07-31T02:21:38.459243Z","shell.execute_reply":"2022-07-31T02:22:10.610817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T02:22:10.614358Z","iopub.execute_input":"2022-07-31T02:22:10.615457Z","iopub.status.idle":"2022-07-31T02:22:10.81897Z","shell.execute_reply.started":"2022-07-31T02:22:10.615406Z","shell.execute_reply":"2022-07-31T02:22:10.817814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#gray scale\ndef to_grayscale(img):\n    grayed = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n    return grayed\n\nimg_grayed = to_grayscale(img)\nplt.imshow(img_grayed,cmap=\"gray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T02:22:10.820343Z","iopub.execute_input":"2022-07-31T02:22:10.820716Z","iopub.status.idle":"2022-07-31T02:22:11.014581Z","shell.execute_reply.started":"2022-07-31T02:22:10.820674Z","shell.execute_reply":"2022-07-31T02:22:11.013476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#binary_threshold \ndef binary_threshold(img):\n    grayed = to_grayscale(img)\n    _,img_binary_threshold = cv2.threshold(grayed,140,255,cv2.THRESH_BINARY)\n    return img_binary_threshold\n\nimg_binary_threshold = binary_threshold(img)\nprint(type(img_binary_threshold))\nplt.imshow(img_binary_threshold,cmap=\"gray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T02:22:11.016959Z","iopub.execute_input":"2022-07-31T02:22:11.017307Z","iopub.status.idle":"2022-07-31T02:22:11.216183Z","shell.execute_reply.started":"2022-07-31T02:22:11.017274Z","shell.execute_reply":"2022-07-31T02:22:11.215114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#boundingRect\ncontours, hierarchy = cv2.findContours(img_binary_threshold, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)\n\nimg_rect = img.copy()\n\nfor i in range(0, len(contours)):\n    if len(contours[i]) > 0:\n\n        # remove small objects\n        if cv2.contourArea(contours[i]) < 500:\n            continue\n            \n        rect = contours[i]\n        x, y, w, h = cv2.boundingRect(rect)\n        cv2.rectangle(img_rect, (x, y), (x + w, y + h), (0, 255, 0), 10)\n        #cv2.polylines(img2, contours[i], True, (255, 255, 255), 5)\n\n# save\n#cv2.imwrite('tulips_boundingbox.jpg', img)\nplt.imshow(cv2.cvtColor(img_rect,cv2.COLOR_BGR2RGB))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T02:22:11.217664Z","iopub.execute_input":"2022-07-31T02:22:11.218023Z","iopub.status.idle":"2022-07-31T02:22:11.428955Z","shell.execute_reply.started":"2022-07-31T02:22:11.21799Z","shell.execute_reply":"2022-07-31T02:22:11.427793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"it doesn't work ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}