{"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":"gpu","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\ndata=pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\ndata.head()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-05T07:35:53.081943Z","iopub.execute_input":"2023-12-05T07:35:53.082344Z","iopub.status.idle":"2023-12-05T07:35:53.098184Z","shell.execute_reply.started":"2023-12-05T07:35:53.082315Z","shell.execute_reply":"2023-12-05T07:35:53.097173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a function to generate the paths based on conditions\ndef generate_paths(row):\n    if row['is_tma'] == 1:\n        return f'/kaggle/input/UBC-OCEAN/train_images/{row[\"image_id\"]}.png'\n    else:\n        return f'/kaggle/input/UBC-OCEAN/train_thumbnails/{row[\"image_id\"]}_thumbnail.png'\n\n# Apply the function to create new columns\ndata['image_path'] = data.apply(generate_paths, axis=1)\ndata","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:53.102274Z","iopub.execute_input":"2023-12-05T07:35:53.10287Z","iopub.status.idle":"2023-12-05T07:35:53.127479Z","shell.execute_reply.started":"2023-12-05T07:35:53.10284Z","shell.execute_reply":"2023-12-05T07:35:53.126565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom skimage import io, color\nfrom skimage.transform import rescale, resize, downscale_local_mean\nimg=io.imread('/kaggle/input/UBC-OCEAN/train_thumbnails/10077_thumbnail.png', as_gray=True)\nimg","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:53.132974Z","iopub.execute_input":"2023-12-05T07:35:53.133319Z","iopub.status.idle":"2023-12-05T07:35:53.440292Z","shell.execute_reply.started":"2023-12-05T07:35:53.133286Z","shell.execute_reply":"2023-12-05T07:35:53.439322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_rescaled=rescale(img, 1.0/4.0, anti_aliasing=False)\nimg_rescaled","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:53.442393Z","iopub.execute_input":"2023-12-05T07:35:53.443136Z","iopub.status.idle":"2023-12-05T07:35:53.47434Z","shell.execute_reply.started":"2023-12-05T07:35:53.443094Z","shell.execute_reply":"2023-12-05T07:35:53.473467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized =resize(img, (256, 256), anti_aliasing=True)\nimg_resized","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:53.475433Z","iopub.execute_input":"2023-12-05T07:35:53.475761Z","iopub.status.idle":"2023-12-05T07:35:53.786703Z","shell.execute_reply.started":"2023-12-05T07:35:53.475735Z","shell.execute_reply":"2023-12-05T07:35:53.785797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_rescaled.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:53.788927Z","iopub.execute_input":"2023-12-05T07:35:53.789244Z","iopub.status.idle":"2023-12-05T07:35:53.795305Z","shell.execute_reply.started":"2023-12-05T07:35:53.789216Z","shell.execute_reply":"2023-12-05T07:35:53.794333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:53.796563Z","iopub.execute_input":"2023-12-05T07:35:53.796856Z","iopub.status.idle":"2023-12-05T07:35:54.713818Z","shell.execute_reply.started":"2023-12-05T07:35:53.79683Z","shell.execute_reply":"2023-12-05T07:35:54.712842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img_rescaled, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:54.715098Z","iopub.execute_input":"2023-12-05T07:35:54.715438Z","iopub.status.idle":"2023-12-05T07:35:55.014436Z","shell.execute_reply.started":"2023-12-05T07:35:54.715409Z","shell.execute_reply":"2023-12-05T07:35:55.01347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img_resized, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:55.015784Z","iopub.execute_input":"2023-12-05T07:35:55.016125Z","iopub.status.idle":"2023-12-05T07:35:55.317598Z","shell.execute_reply.started":"2023-12-05T07:35:55.016095Z","shell.execute_reply":"2023-12-05T07:35:55.316709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_downscaled= downscale_local_mean(img, (4,3))\nimg_downscaled","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:55.319145Z","iopub.execute_input":"2023-12-05T07:35:55.319488Z","iopub.status.idle":"2023-12-05T07:35:55.386375Z","shell.execute_reply.started":"2023-12-05T07:35:55.319456Z","shell.execute_reply":"2023-12-05T07:35:55.385237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img_downscaled, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:55.387714Z","iopub.execute_input":"2023-12-05T07:35:55.388045Z","iopub.status.idle":"2023-12-05T07:35:55.65919Z","shell.execute_reply.started":"2023-12-05T07:35:55.388015Z","shell.execute_reply":"2023-12-05T07:35:55.658125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.filters import gaussian, sobel\ngaussian_img=gaussian(img, sigma=5, mode='constant', cval=0.0)\nplt.imshow(gaussian_img)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:55.662063Z","iopub.execute_input":"2023-12-05T07:35:55.662375Z","iopub.status.idle":"2023-12-05T07:35:56.990556Z","shell.execute_reply.started":"2023-12-05T07:35:55.662347Z","shell.execute_reply":"2023-12-05T07:35:56.989665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gray_img=img=io.imread('/kaggle/input/UBC-OCEAN/train_thumbnails/10077_thumbnail.png', as_gray=True)\nsobel_img= sobel(gray_img)\nplt.imshow(sobel_img, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:56.991803Z","iopub.execute_input":"2023-12-05T07:35:56.992108Z","iopub.status.idle":"2023-12-05T07:35:58.536107Z","shell.execute_reply.started":"2023-12-05T07:35:56.992081Z","shell.execute_reply":"2023-12-05T07:35:58.535111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using open CV","metadata":{}},{"cell_type":"code","source":"import cv2\nimg=cv2.imread('/kaggle/input/UBC-OCEAN/train_thumbnails/10077_thumbnail.png', 1)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:35:58.537187Z","iopub.execute_input":"2023-12-05T07:35:58.53746Z","iopub.status.idle":"2023-12-05T07:36:00.253369Z","shell.execute_reply.started":"2023-12-05T07:35:58.537436Z","shell.execute_reply":"2023-12-05T07:36:00.252414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n\n# Assuming 'img' is your image\nresized = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)\n\n# Display the original and resized images using matplotlib\nplt.figure(figsize=(10, 5))\n\nplt.subplot(1, 2, 1)\nplt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\nplt.title('Original Image')\n\nplt.subplot(1, 2, 2)\nplt.imshow(cv2.cvtColor(resized, cv2.COLOR_BGR2RGB))\nplt.title('Resized Image')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:36:00.254618Z","iopub.execute_input":"2023-12-05T07:36:00.254924Z","iopub.status.idle":"2023-12-05T07:36:04.375023Z","shell.execute_reply.started":"2023-12-05T07:36:00.254898Z","shell.execute_reply":"2023-12-05T07:36:04.374102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# image filtering","metadata":{}},{"cell_type":"code","source":"from skimage import io, img_as_float\nfrom skimage.filters import unsharp_mask\nfrom skimage.filters import gaussian\nimport matplotlib.pyplot as plt\n\nimg = img_as_float(io.imread('/kaggle/input/UBC-OCEAN/train_thumbnails/10077_thumbnail.png', as_gray=True))\ngaussian_img = gaussian(img, sigma=2, mode='constant', cval=0.0)\nimg2 = (img - gaussian_img) * 2\nimg3 = img + img2\n\nplt.imshow(img, cmap='gray')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:36:04.376399Z","iopub.execute_input":"2023-12-05T07:36:04.376793Z","iopub.status.idle":"2023-12-05T07:36:05.556256Z","shell.execute_reply.started":"2023-12-05T07:36:04.376762Z","shell.execute_reply":"2023-12-05T07:36:05.555321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img2, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:36:05.557495Z","iopub.execute_input":"2023-12-05T07:36:05.557865Z","iopub.status.idle":"2023-12-05T07:36:06.414282Z","shell.execute_reply.started":"2023-12-05T07:36:05.557838Z","shell.execute_reply":"2023-12-05T07:36:06.413373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img3, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:36:06.415706Z","iopub.execute_input":"2023-12-05T07:36:06.416079Z","iopub.status.idle":"2023-12-05T07:36:07.294514Z","shell.execute_reply.started":"2023-12-05T07:36:06.41604Z","shell.execute_reply":"2023-12-05T07:36:07.293567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import io, img_as_float\nfrom skimage.filters import unsharp_mask\nfrom skimage.filters import gaussian\nimport matplotlib.pyplot as plt\n\nimg = img_as_float(io.imread('/kaggle/input/UBC-OCEAN/train_thumbnails/10077_thumbnail.png', as_gray=True))\ngaussian_img = gaussian(img, sigma=2, mode='constant', cval=0.0)\nimg2 = unsharp_mask(img, radius=3, amount=2)\nimg3 = img + img2\n\nplt.figure(figsize=(10, 4))\n\n# Original Image\nplt.subplot(1, 3, 1)\nplt.imshow(img, cmap='gray')\nplt.title('Original Image')\n\n# Unsharp Masked Image\nplt.subplot(1, 3, 2)\nplt.imshow(img2, cmap='gray')\nplt.title('Unsharp Masked Image')\n\n# Combined Image\nplt.subplot(1, 3, 3)\nplt.imshow(img3, cmap='gray')\nplt.title('Combined Image')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:36:07.296275Z","iopub.execute_input":"2023-12-05T07:36:07.29667Z","iopub.status.idle":"2023-12-05T07:36:09.896439Z","shell.execute_reply.started":"2023-12-05T07:36:07.296638Z","shell.execute_reply":"2023-12-05T07:36:09.895533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import io, img_as_float\nfrom skimage.filters import unsharp_mask\nfrom skimage.filters import gaussian\nimport matplotlib.pyplot as plt\n\n# Load the image\nimg = img_as_float(io.imread('/kaggle/input/UBC-OCEAN/train_thumbnails/10077_thumbnail.png', as_gray=True))\n\n# Apply unsharp mask\nunsharped_img = unsharp_mask(img, radius=3, amount=2)\n\n# Create a 1x2 subplot grid\nfig, axes = plt.subplots(1, 2, figsize=(12, 6))\n\n# Display the original image in the first subplot\naxes[0].imshow(img, cmap='gray')\naxes[0].set_title('Input Image')\n\n# Display the unsharpened image in the second subplot\naxes[1].imshow(unsharped_img, cmap='gray')\naxes[1].set_title('Unsharpened Image')\n\n# Adjust layout for better spacing\nplt.tight_layout()\n\n# Save the figure\nplt.savefig('/kaggle/working/images_grid.png')\n\n# Show the figure (optional)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-05T07:36:09.897536Z","iopub.execute_input":"2023-12-05T07:36:09.897825Z","iopub.status.idle":"2023-12-05T07:36:13.399641Z","shell.execute_reply.started":"2023-12-05T07:36:09.897798Z","shell.execute_reply":"2023-12-05T07:36:13.398588Z"},"trusted":true},"execution_count":null,"outputs":[]}]}