{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30615,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2 as cv\nimport PIL.Image as Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-19T13:09:55.143761Z","iopub.execute_input":"2023-12-19T13:09:55.144141Z","iopub.status.idle":"2023-12-19T13:09:57.659339Z","shell.execute_reply.started":"2023-12-19T13:09:55.144111Z","shell.execute_reply":"2023-12-19T13:09:57.657884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\ntrain_csv = train_csv[train_csv['is_tma']==False]","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:09:57.662103Z","iopub.execute_input":"2023-12-19T13:09:57.662888Z","iopub.status.idle":"2023-12-19T13:09:57.695398Z","shell.execute_reply.started":"2023-12-19T13:09:57.662833Z","shell.execute_reply":"2023-12-19T13:09:57.694089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_folder = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\nimg_files = [os.path.join(path_folder, \n                          f\"{str(f[1][0])}_thumbnail.png\") for f in train_csv.iterrows()]","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:09:57.69753Z","iopub.execute_input":"2023-12-19T13:09:57.697915Z","iopub.status.idle":"2023-12-19T13:09:57.749049Z","shell.execute_reply.started":"2023-12-19T13:09:57.697883Z","shell.execute_reply":"2023-12-19T13:09:57.74806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = train_csv[train_csv['is_tma']==False]","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:09:57.75176Z","iopub.execute_input":"2023-12-19T13:09:57.752497Z","iopub.status.idle":"2023-12-19T13:09:57.75903Z","shell.execute_reply.started":"2023-12-19T13:09:57.752425Z","shell.execute_reply":"2023-12-19T13:09:57.757896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"한 이미지를 받아서 crop 이미지의 np.sum(image)값 비교, 임계점 찾기","metadata":{}},{"cell_type":"code","source":"def find_threshold(image_path):\n    img = Image.open(image_path)\n    gray = img.convert('L')\n    gray1 = np.array(gray)     # for plotting rectangle\n    \n    h, w = np.array(gray1).shape\n    \n    list_img = []\n    thresholds =[]\n    crop_number = 0\n    patch_size = (512, 512)\n\n    \n    for y in range(0, h, patch_size[0]):\n        for x in range(0, w, patch_size[1]):\n            \n            if y + patch_size[0] <= h:\n                if x + patch_size[1] <= w:\n                    cv.rectangle(gray1, (x,y), (x+patch_size[0], y+patch_size[1]),\n                                 (255, 0, 0), thickness=10)\n                    \n                    # Crop the Image\n                    crop_img = gray.crop((x, y, x + patch_size[0], y + patch_size[1]))\n                    crop_img = np.array(crop_img)\n                    threshold = np.sum(crop_img)\n                    crop_number += 1\n                    \n                    clahe = cv.createCLAHE(clipLimit=40.0, tileGridSize=(2, 2))\n                    clahe_img = clahe.apply(crop_img)\n                    list_img.append(clahe_img)\n                    thresholds.append(threshold)\n    return gray1, list_img , crop_number, thresholds","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:09:57.760824Z","iopub.execute_input":"2023-12-19T13:09:57.761552Z","iopub.status.idle":"2023-12-19T13:09:57.775664Z","shell.execute_reply.started":"2023-12-19T13:09:57.761508Z","shell.execute_reply":"2023-12-19T13:09:57.774261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_img(path):\n    gray, list_img, total, thresholds = find_threshold(path)\n    print(f\"image path : {path}\")\n    print(f\"original image shape: {gray.shape}, total cropping number: {total}\")\n\n    plt.imshow(gray, cmap='gray')\n\n    print(f\"cropped image number: {len(list_img)}\")\n    plt.figure(figsize=(6, 6))\n    #print(thresholds)\n       \n    for i in range(min(len(list_img), 30)):\n        try:\n            plt.subplot(8, 8, i + 1)\n            plt.imshow(list_img[i], cmap='gray')\n            plt.title(f\"{thresholds[i]}\", fontsize='smaller')  # Show the sum value as xlabel\n            plt.axis('off')\n        except:\n            pass\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:09:57.779112Z","iopub.execute_input":"2023-12-19T13:09:57.779609Z","iopub.status.idle":"2023-12-19T13:09:57.793496Z","shell.execute_reply.started":"2023-12-19T13:09:57.779543Z","shell.execute_reply":"2023-12-19T13:09:57.792534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def find_threshold2(image_path, threshold, undup=True):\n    img = Image.open(image_path)\n    gray = img.convert('L')\n    gray1 = np.array(gray)     # for plotting rectangle\n    \n    h, w = np.array(gray1).shape\n    \n    list_img = []\n    crop_number = 0\n    patch_size = (512, 512)\n    #threshold = threshold\n    stride = patch_size[0] // 4\n\n    if undup:\n        for y in range(0, h, patch_size[0]):\n            for x in range(0, w, patch_size[1]):\n                \n                if y + patch_size[0] <= h:\n                    if x + patch_size[1] <= w:\n                        cv.rectangle(gray1, (x,y), (x+patch_size[0], y+patch_size[1]),\n                                    (255, 0, 0), thickness=10)\n                        \n                        # Crop the Image\n                        crop_img = gray.crop((x, y, x + patch_size[0], y + patch_size[1]))\n                        crop_img = np.array(crop_img)\n                        \n                        if  np.sum(crop_img) > threshold:\n                            clahe = cv.createCLAHE(clipLimit=40.0, tileGridSize=(2, 2))\n                            clahe_img = clahe.apply(crop_img)\n                            list_img.append(clahe_img)\n                            crop_number += 1\n    else:\n        for y in range(0, h, stride):\n            for x in range(0, w, stride):\n                \n                if y + patch_size[0] <= h:\n                    if x + patch_size[1] <= w:\n                        cv.rectangle(gray1, (x,y), (x+patch_size[0], y+patch_size[1]),\n                                    (255, 0, 0), thickness=10)\n                        \n                        # Crop the Image\n                        crop_img = gray.crop((x, y, x + patch_size[0], y + patch_size[1]))\n                        crop_img = np.array(crop_img)\n                        crop_number += 1\n                        \n                        if  np.sum(crop_img) > threshold:\n                            clahe = cv.createCLAHE(clipLimit=40.0, tileGridSize=(2, 2))\n                            clahe_img = clahe.apply(crop_img)\n                            list_img.append(clahe_img)    \n\n    return gray1, list_img , crop_number","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:11:39.87439Z","iopub.execute_input":"2023-12-19T13:11:39.875828Z","iopub.status.idle":"2023-12-19T13:11:39.893895Z","shell.execute_reply.started":"2023-12-19T13:11:39.87576Z","shell.execute_reply":"2023-12-19T13:11:39.892816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_img2(path, threshold,undup=True):\n    gray, list_img, total = find_threshold2(path,threshold,undup)\n    print(f\"image path : {path}\")\n    print(f\"original image shape: {gray.shape}, total cropping number: {total}\")\n    \n    plt.imshow(gray, cmap='gray')\n \n    \n    print(f\"cropped image number: {len(list_img)}\")\n    plt.figure(figsize=(6,6))\n    for i in range(len(list_img)):\n        try:\n            plt.subplot(8,8, i+1)\n            plt.imshow(list_img[i], cmap='gray')\n            plt.axis('off')\n        except: pass\n    plt.show()\n    print('======================================')\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:11:44.029704Z","iopub.execute_input":"2023-12-19T13:11:44.030457Z","iopub.status.idle":"2023-12-19T13:11:44.037619Z","shell.execute_reply.started":"2023-12-19T13:11:44.030421Z","shell.execute_reply":"2023-12-19T13:11:44.036785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in img_files[23:30]:\n    plot_img(file)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:11:47.593181Z","iopub.execute_input":"2023-12-19T13:11:47.593837Z","iopub.status.idle":"2023-12-19T13:12:02.677214Z","shell.execute_reply.started":"2023-12-19T13:11:47.593803Z","shell.execute_reply":"2023-12-19T13:12:02.675964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in img_files[23:30]:\n    plot_img2(file, 35000000, undup=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:12:27.272643Z","iopub.execute_input":"2023-12-19T13:12:27.273086Z","iopub.status.idle":"2023-12-19T13:12:54.832439Z","shell.execute_reply.started":"2023-12-19T13:12:27.27304Z","shell.execute_reply":"2023-12-19T13:12:54.83132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in img_files[23:30]:\n    plot_img2(file, 35000000, undup=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:12:59.198648Z","iopub.execute_input":"2023-12-19T13:12:59.199026Z","iopub.status.idle":"2023-12-19T13:13:07.719232Z","shell.execute_reply.started":"2023-12-19T13:12:59.198996Z","shell.execute_reply":"2023-12-19T13:13:07.718103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in img_files[23:30]:\n    plot_img2(file, 30000000, undup=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:14:48.279966Z","iopub.execute_input":"2023-12-19T13:14:48.280366Z","iopub.status.idle":"2023-12-19T13:14:57.181577Z","shell.execute_reply.started":"2023-12-19T13:14:48.280337Z","shell.execute_reply":"2023-12-19T13:14:57.180378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in img_files[220:228]:\n    plot_img2(file, 30000000, undup=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:15:19.896697Z","iopub.execute_input":"2023-12-19T13:15:19.897133Z","iopub.status.idle":"2023-12-19T13:15:56.187453Z","shell.execute_reply.started":"2023-12-19T13:15:19.897093Z","shell.execute_reply":"2023-12-19T13:15:56.18613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in img_files[220:228]:\n    plot_img2(file, 40000000, undup=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T13:16:07.355615Z","iopub.execute_input":"2023-12-19T13:16:07.356052Z","iopub.status.idle":"2023-12-19T13:16:39.398171Z","shell.execute_reply.started":"2023-12-19T13:16:07.356013Z","shell.execute_reply":"2023-12-19T13:16:39.396966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}