{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Assignment 3\nFor Computer Science Participants:\n\nTask: Use any publicly available fundus image dataset and perform four complex image processing operations. Apply these techniques to the entire dataset.\n\n# Submission Guidelines\nEnsure you visualize the results of each operation for a subset of images to showcase the transformations.\nSubmit a comprehensive report detailing your methodology, code snippets, results, and visualizations.\nProvide the code used for the image processing operations","metadata":{}},{"cell_type":"code","source":"\nimport numpy as np \nimport pandas as pd\nfrom glob import glob\n\nimport cv2\nimport matplotlib.pylab as plt","metadata":{"execution":{"iopub.status.busy":"2024-06-27T17:27:32.154825Z","iopub.execute_input":"2024-06-27T17:27:32.155274Z","iopub.status.idle":"2024-06-27T17:27:32.972615Z","shell.execute_reply.started":"2024-06-27T17:27:32.155241Z","shell.execute_reply":"2024-06-27T17:27:32.970753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading Images","metadata":{}},{"cell_type":"code","source":"fundus_images = glob(\"/kaggle/input/aptos2019-blindness-detection/test_images/*.png\")\n\nmpl_image = plt.imread(fundus_images[13])\ncv2_image = cv2.imread(fundus_images[13])\n\nmpl_image.shape, cv2_image.shape\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T17:27:41.885197Z","iopub.execute_input":"2024-06-27T17:27:41.886205Z","iopub.status.idle":"2024-06-27T17:27:42.664681Z","shell.execute_reply.started":"2024-06-27T17:27:41.886146Z","shell.execute_reply":"2024-06-27T17:27:42.663541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Displaying Image ","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize = (10,10))\n\nax.imshow(mpl_image)\nax.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T16:14:29.286735Z","iopub.execute_input":"2024-06-27T16:14:29.287157Z","iopub.status.idle":"2024-06-27T16:14:30.949121Z","shell.execute_reply.started":"2024-06-27T16:14:29.287126Z","shell.execute_reply":"2024-06-27T16:14:30.947665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I will be applying the techniques to only the first 10 images as there are thousands of images in the dataset which takes a lot of time to process but theoretically, they could be applied to the full dataset just by not splicing the array at the start.","metadata":{}},{"cell_type":"markdown","source":"# Edge Detection","metadata":{}},{"cell_type":"code","source":"#splicing the array to just the first 10 images\nfundus_images = fundus_images[:10]\n\n\nplt.figure(figsize=(20, 20))\n\nfor i, img_path in enumerate(fundus_images):\n\n    image = cv2.imread(img_path)\n    \n    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    \n\n    edges = cv2.Canny(gray_image, 100, 200)\n    \n    # displaying the original image\n    plt.subplot(10, 2, 2 * i + 1) # formula for calculating the correct place for the respective\n                                  #image in the subplot\n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    plt.title('Original Image')\n    plt.axis('off')\n    \n    # displaying the edge-detected image\n    plt.subplot(10, 2, 2 * i + 2)\n    plt.imshow(edges, cmap='gray')\n    plt.title('Edge Detection')\n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T17:46:04.070610Z","iopub.execute_input":"2024-06-27T17:46:04.071384Z","iopub.status.idle":"2024-06-27T17:46:07.834663Z","shell.execute_reply.started":"2024-06-27T17:46:04.071345Z","shell.execute_reply":"2024-06-27T17:46:07.833544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We might use edge detection to make it easier for us or the computer to identify objects in the image. We may use the modified image for further processing. For example, in diabetic retinopathy, it could be used to identify hemorrhages as there will be abnormal growth of blood vessels and microaneurysms. These charecterists of diabetic retinopathy would show up in an edge detected image.","metadata":{}},{"cell_type":"markdown","source":"# Image Segmentation","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\n\n\norb = cv2.ORB_create()\n\nfor i, img_path in enumerate(fundus_images):\n    image = cv2.imread(img_path)\n    \n    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n    keypoints, descriptors = orb.detectAndCompute(gray_image, None)\n    \n    # drawing keypoints on the image\n    image_with_keypoints = cv2.drawKeypoints(image, keypoints, None, color=(0, 255, 0))\n\n    plt.subplot(10, 2, 2 * i + 1)\n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    plt.title('Original Image')\n    plt.axis('off')\n    \n    plt.subplot(10, 2, 2 * i + 2)\n    plt.imshow(cv2.cvtColor(image_with_keypoints, cv2.COLOR_BGR2RGB))\n    plt.title('Image with ORB Keypoints')\n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T18:15:28.341532Z","iopub.execute_input":"2024-06-27T18:15:28.342755Z","iopub.status.idle":"2024-06-27T18:15:32.925952Z","shell.execute_reply.started":"2024-06-27T18:15:28.342716Z","shell.execute_reply":"2024-06-27T18:15:32.924838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Feature detection can help us or the computer identify points of interest in the image. Again, in the context of diabetic retinopathy, it can help us detect microaneurysms and hemorrhages.","metadata":{}},{"cell_type":"markdown","source":"# Image Filtering (Gaussian Blur):","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\n\n\n\nfor i, img_path in enumerate(fundus_images):\n    image = cv2.imread(img_path)\n    \n    blurred_image = cv2.GaussianBlur(image, (15, 15), 0)\n\n\n    plt.subplot(10, 2, 2 * i + 1)\n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    plt.title('Original Image')\n    plt.axis('off')\n    \n    plt.subplot(10, 2, 2 * i + 2)\n    plt.imshow(cv2.cvtColor(blurred_image, cv2.COLOR_BGR2RGB))\n    plt.title('Blurred Image')\n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T18:28:27.227536Z","iopub.execute_input":"2024-06-27T18:28:27.227969Z","iopub.status.idle":"2024-06-27T18:28:32.323168Z","shell.execute_reply.started":"2024-06-27T18:28:27.227937Z","shell.execute_reply":"2024-06-27T18:28:32.321929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We might want to use blurring in cases where we want to ignore small details that can distract us or the computer. In cases where we just want to focus on the bigger picture, we might use blurring.","metadata":{}},{"cell_type":"markdown","source":"# Thresholding","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\n\n\nfor i, img_path in enumerate(fundus_images):\n    image = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)\n    \n    threshold_value, thresholded_image = cv2.threshold(image, 100, 255, cv2.THRESH_BINARY)\n\n\n    plt.subplot(10, 2, 2 * i + 1)\n    plt.imshow(image, cmap='gray')\n    plt.title('Original Image')\n    plt.axis('off')\n    \n    plt.subplot(10, 2, 2 * i + 2)\n    plt.imshow(thresholded_image, cmap='gray')\n    plt.title('Thresholded Image')\n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T18:51:12.337179Z","iopub.execute_input":"2024-06-27T18:51:12.337624Z","iopub.status.idle":"2024-06-27T18:51:15.813864Z","shell.execute_reply.started":"2024-06-27T18:51:12.337589Z","shell.execute_reply":"2024-06-27T18:51:15.812756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Thresholding is also used to identify intresting features that we might want to extract from the image. In the case of diabetic retinopthy, it might help us seperate the blood vessels from the rest of the eye as can be seen in the 7th image above through which we can identify hemmorages more easily.","metadata":{}}]}