{"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":"# Ovary Cropped Image for Train","metadata":{}},{"cell_type":"markdown","source":"About 10 images without black background were extracted from the original images of the training data. The extracted images were stored in folders for each finding.","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nimport numpy as np\nimport pandas as pd \n\nimport matplotlib.pyplot as plt\nimport cv2\nfrom PIL import Image\nImage.MAX_IMAGE_PIXELS = 10000000000","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-01T15:01:18.83685Z","iopub.execute_input":"2023-11-01T15:01:18.837218Z","iopub.status.idle":"2023-11-01T15:01:19.313705Z","shell.execute_reply.started":"2023-11-01T15:01:18.837192Z","shell.execute_reply":"2023-11-01T15:01:19.312932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths=[]\nfor dirname, _, filenames in os.walk('/kaggle/input/UBC-OCEAN/train_images'):\n    for filename in filenames:\n        paths+=[(os.path.join(dirname, filename))]","metadata":{"execution":{"iopub.status.busy":"2023-11-01T15:01:19.315217Z","iopub.execute_input":"2023-11-01T15:01:19.315614Z","iopub.status.idle":"2023-11-01T15:01:19.364931Z","shell.execute_reply.started":"2023-11-01T15:01:19.315589Z","shell.execute_reply":"2023-11-01T15:01:19.364062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\ndisplay(train)\nprint(train.columns.tolist())\nnames=train['label'].unique().tolist()\n!mkdir train\n\nfor name in names:\n    !mkdir train/{name}","metadata":{"execution":{"iopub.status.busy":"2023-11-01T15:01:19.366276Z","iopub.execute_input":"2023-11-01T15:01:19.366559Z","iopub.status.idle":"2023-11-01T15:01:25.383013Z","shell.execute_reply.started":"2023-11-01T15:01:19.366534Z","shell.execute_reply":"2023-11-01T15:01:25.381874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_width,new_height = 6224,6224\ncrop_width,crop_height = 224,224\nrange_width = new_width-crop_width\nrange_height = new_height-crop_height\n    \nfor i in range(len(train)):\n    file=str(train.loc[i,'image_id'])+'.png'\n    path=os.path.join('/kaggle/input/UBC-OCEAN/train_images',file)\n    label=train.loc[i,'label']\n    img0 = Image.open(path)\n    img = img0.resize((new_width,new_height))\n    X = random.sample(range(range_width),8)\n    Y = random.sample(range(range_height),8)\n    for j in range(8):\n        x,y=X[j],Y[j]\n        topath=os.path.join('train',label,file[0:-4]+'_'+str(j).zfill(2)+'.png')       \n        cropped = img.crop((x,y,x+crop_width,y+crop_height))\n        channel_zero_count = 0\n        for yi in range(crop_height):\n            for xi in range(crop_width):\n                r,g,b = cropped.getpixel((xi,yi))\n                if (r==0 and g==0 and b==0) or (r>220 and g>220 and b>220):\n                    channel_zero_count += 1\n        if channel_zero_count <= 1000:\n            cropped.save(topath)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T15:01:25.384437Z","iopub.execute_input":"2023-11-01T15:01:25.385666Z","iopub.status.idle":"2023-11-01T15:01:25.393027Z","shell.execute_reply.started":"2023-11-01T15:01:25.38562Z","shell.execute_reply":"2023-11-01T15:01:25.391414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n    tpaths=[]\n    for dirname, _, filenames in os.walk('/kaggle/input/UBC-OCEAN/test_images'):\n        for filename in filenames:\n            tpaths+=[(os.path.join(dirname, filename))]\n\n    !mkdir test\n\n\n    for i in range(len(tpaths)):\n        file=tpaths[i].split('/')[-1]\n        path=os.path.join('/kaggle/input/UBC-OCEAN/test_images',file)\n        img0 = Image.open(path)\n        img = img0.resize((new_width,new_height))\n        X = random.sample(range(range_width),16)\n        Y = random.sample(range(range_height),16)\n        for j in range(16):\n            x,y=X[j],Y[j]\n            topath=os.path.join('test',file[0:-4]+'_'+str(j).zfill(2)+'.png')  \n            cropped = img.crop((x,y,x+crop_width,y+crop_height))\n            channel_zero_count = 0\n            for yi in range(crop_height):\n                for xi in range(crop_width):\n                    r,g,b = cropped.getpixel((xi,yi))\n                    if (r==0 and g==0 and b==0) or (r>220 and g>220 and b>220):\n                        channel_zero_count += 1\n            if channel_zero_count <= 1000:\n                cropped.save(topath)\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}