{"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 Image Random Cropping\nHGSC (High-Grade Serous Carcinoma)","metadata":{}},{"cell_type":"markdown","source":"https://www.kaggle.com/code/stpeteishii/lesion-sample-hgsc<br/>\nhttps://www.kaggle.com/code/stpeteishii/image-cropping-and-adjusting-brightness","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport os\nimport random\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-01T13:38:58.15765Z","iopub.execute_input":"2023-11-01T13:38:58.158236Z","iopub.status.idle":"2023-11-01T13:38:58.166083Z","shell.execute_reply.started":"2023-11-01T13:38:58.158199Z","shell.execute_reply":"2023-11-01T13:38:58.164682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 10143_thumbnail.png","metadata":{}},{"cell_type":"code","source":"img0=plt.imread('/kaggle/input/UBC-OCEAN/train_thumbnails/10143_thumbnail.png')\nplt.figure(figsize=(6,6))\nplt.imshow(img0)\nplt.show()\nprint(img0.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T13:38:58.168611Z","iopub.execute_input":"2023-11-01T13:38:58.169158Z","iopub.status.idle":"2023-11-01T13:39:00.445258Z","shell.execute_reply.started":"2023-11-01T13:38:58.169115Z","shell.execute_reply":"2023-11-01T13:39:00.443915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\ndisplay(train[train['image_id']==10143])","metadata":{"execution":{"iopub.status.busy":"2023-11-01T13:39:00.446758Z","iopub.execute_input":"2023-11-01T13:39:00.44744Z","iopub.status.idle":"2023-11-01T13:39:00.465135Z","shell.execute_reply.started":"2023-11-01T13:39:00.447403Z","shell.execute_reply":"2023-11-01T13:39:00.46374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 10143.png","metadata":{}},{"cell_type":"code","source":"img0 = Image.open('/kaggle/input/UBC-OCEAN/train_images/10143.png')\nprint(img0.size)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T13:39:00.467995Z","iopub.execute_input":"2023-11-01T13:39:00.468387Z","iopub.status.idle":"2023-11-01T13:39:00.476182Z","shell.execute_reply.started":"2023-11-01T13:39:00.468353Z","shell.execute_reply":"2023-11-01T13:39:00.474878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_width,new_height = 6000,6000\nimg = img0.resize((new_width,new_height))\nprint(img.size)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T13:39:00.47762Z","iopub.execute_input":"2023-11-01T13:39:00.477985Z","iopub.status.idle":"2023-11-01T13:40:17.245417Z","shell.execute_reply.started":"2023-11-01T13:39:00.477953Z","shell.execute_reply":"2023-11-01T13:40:17.24392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crop_width,crop_height = 224,224\nrange_width = new_width-crop_width\nrange_height = new_height-crop_height\nX = random.sample(range(range_width),8)\nY = random.sample(range(range_height),8)\nprint(X)\nprint(Y)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T13:40:17.247002Z","iopub.execute_input":"2023-11-01T13:40:17.247454Z","iopub.status.idle":"2023-11-01T13:40:17.254156Z","shell.execute_reply.started":"2023-11-01T13:40:17.247424Z","shell.execute_reply":"2023-11-01T13:40:17.252921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Extract multiple cropping images without black background from the original image.","metadata":{}},{"cell_type":"code","source":"zeros=[]\nXY=[]\nfor i in range(8):\n    for j in range(8):\n        x,y=X[i],Y[j]\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        zeros+=[channel_zero_count]\n        XY+=[(x,y)]\n        if channel_zero_count <= 1000:\n            plt.figure(figsize=(4,4))\n            plt.title('x: '+str(x)+', y: '+str(y)+', zero_count: '+str(channel_zero_count))\n            plt.imshow(cropped)\n            plt.show()\n        \nprint(zeros)\nprint(XY)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T13:40:17.255674Z","iopub.execute_input":"2023-11-01T13:40:17.25673Z","iopub.status.idle":"2023-11-01T13:40:17.316932Z","shell.execute_reply.started":"2023-11-01T13:40:17.256689Z","shell.execute_reply":"2023-11-01T13:40:17.314888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}