{"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":"# Denoising Techniques for Biomedical Images\n![Denoising Techniques](https://www.researchgate.net/profile/Balasubramanian-Prabhu-Kavin/publication/341746088/figure/fig1/AS:971394754048000@1608609965036/Hierarchical-representation-of-various-types-of-image-denoising-techniques.png)\n[Source](https://www.researchgate.net/publication/341746088_A_modified_digital_signature_algorithm_to_improve_the_biomedical_image_integrity_in_cloud_environment?_tp=eyJjb250ZXh0Ijp7ImZpcnN0UGFnZSI6Il9kaXJlY3QiLCJwYWdlIjoiX2RpcmVjdCJ9fQ)","metadata":{}},{"cell_type":"markdown","source":"## Introduction:\nMedical imaging is essential to contemporary healthcare because it offers priceless insights into the inner workings of the human body. Nevertheless, noise in these images might mask information and make diagnosis more difficult. In medical image analysis, denoising is an important preprocessing procedure that tries to enhance image quality by lowering noise while keeping important characteristics. This article explores several denoising methods specifically designed for medical images, emphasizing their approaches, advantages, and drawbacks.\n\n## The Challenge of Noise in Medical Imaging\nPrior to talking about denoising approaches, it's important to know what kinds of noise typically impact medical photographs. During picture acquisition, noise can come from a number of sources, such as ambient conditions, defective sensors, and patient movement. Gaussian noise, speckle noise in ultrasonic images, and Poisson noise in low-dose X-ray imaging are common types of noise seen in medical images.","metadata":{}},{"cell_type":"markdown","source":"## 1. Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n\nimport cv2\nfrom skimage import io, restoration\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-11-08T17:38:43.179028Z","iopub.execute_input":"2023-11-08T17:38:43.179993Z","iopub.status.idle":"2023-11-08T17:38:45.195990Z","shell.execute_reply.started":"2023-11-08T17:38:43.179942Z","shell.execute_reply":"2023-11-08T17:38:45.194689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Function to Load and Preprocess the Image","metadata":{}},{"cell_type":"code","source":"# Function to load and preprocess the image\ndef load_image(path):\n    image = io.imread(path)\n    image = image.astype('float32') / 255.0  # Normalizing the image to [0, 1]\n    return image\n\n# Assuming 'image_path' is the path to your TIFF image\nimage_path = '/kaggle/input/blood-vessel-segmentation/train/kidney_1_voi/images/0000.tif'\noriginal_image = load_image(image_path)\n\nplt.imshow(original_image, cmap='gray')\nplt.title('Original Image')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T17:39:44.732754Z","iopub.execute_input":"2023-11-08T17:39:44.733171Z","iopub.status.idle":"2023-11-08T17:39:45.493694Z","shell.execute_reply.started":"2023-11-08T17:39:44.733134Z","shell.execute_reply":"2023-11-08T17:39:45.492718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Gaussian Blur\n\nThe Gaussian blur is among the most basic and popular denoising methods available. By convolving the image with a Gaussian function, it smooths out high-frequency noise and improves image smoothness. Although Gaussian blur is simple to apply and computationally efficient, it occasionally oversmooths significant details, which is especially undesirable in medical photos where accuracy is essential.","metadata":{}},{"cell_type":"code","source":"# Gaussian blur denoising\ngaussian_blur = cv2.GaussianBlur(original_image, (5, 5), 0)\n\nplt.imshow(gaussian_blur, cmap='gray')\nplt.title('Gaussian Blurred Image')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T17:40:04.055351Z","iopub.execute_input":"2023-11-08T17:40:04.055783Z","iopub.status.idle":"2023-11-08T17:40:04.699062Z","shell.execute_reply.started":"2023-11-08T17:40:04.055748Z","shell.execute_reply":"2023-11-08T17:40:04.697868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Median Filtering\n\nA non-linear technique called \"median filtering\" replaces each pixel's value with the median of the intensities in that pixel's surrounding area. Compared to Gaussian blur, this approach is less likely to cause edge blur and is especially good against salt-and-pepper noise. For more evenly distributed noise, such as Gaussian noise, median filtering might not be as useful.","metadata":{}},{"cell_type":"code","source":"# Median filtering\nmedian_filtered = cv2.medianBlur((original_image * 255).astype(np.uint8), 5)\n\nplt.imshow(median_filtered, cmap='gray')\nplt.title('Median Filtered Image')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T17:50:33.051910Z","iopub.execute_input":"2023-11-08T17:50:33.052347Z","iopub.status.idle":"2023-11-08T17:50:33.664983Z","shell.execute_reply.started":"2023-11-08T17:50:33.052314Z","shell.execute_reply":"2023-11-08T17:50:33.663695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5. Anisotropic Diffusion\nAnisotropic diffusion is an edge-preserving denoising method, commonly referred to as the Perona-Malik filter. By taking into account the image's local structure, it diffuses the picture without smoothing its borders. Anisotropic diffusion is very useful for medical images with significant structural information because of its selective smoothing feature.","metadata":{}},{"cell_type":"code","source":"# Bilateral filter as an alternative to anisotropic diffusion\nbilateral_filtered = restoration.denoise_bilateral(original_image, sigma_color=0.05, sigma_spatial=15)\n\nplt.imshow(bilateral_filtered, cmap='gray')\nplt.title('Bilateral Filtered Image')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T17:43:53.171103Z","iopub.execute_input":"2023-11-08T17:43:53.171529Z","iopub.status.idle":"2023-11-08T17:49:44.803671Z","shell.execute_reply.started":"2023-11-08T17:43:53.171493Z","shell.execute_reply":"2023-11-08T17:49:44.801871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6. Wavelet Transform\nThe wavelet transform, which breaks down an image into wavelet coefficients at different scales, is an effective method for denoising medical pictures. By using thresholding techniques to adjust these coefficients, noise can be decreased. Wavelet-based denoising can retain little features in the image and is quite flexible when it comes to various noise models.","metadata":{}},{"cell_type":"code","source":"# Wavelet denoising\nwavelet_denoised = restoration.denoise_wavelet(original_image, method='BayesShrink', mode='soft')\n\nplt.imshow(wavelet_denoised, cmap='gray')\nplt.title('Wavelet Denoised Image')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T17:49:44.806222Z","iopub.execute_input":"2023-11-08T17:49:44.807350Z","iopub.status.idle":"2023-11-08T17:49:45.610799Z","shell.execute_reply.started":"2023-11-08T17:49:44.807312Z","shell.execute_reply":"2023-11-08T17:49:45.609656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 7. Non-Local Means\nA more advanced method called the Non-Local Means algorithm uses the average of all the image's pixels, weighted by how similar their neighborhoods are to one another. It can reduce noise and maintain more information than local techniques. It is more computationally demanding, but this makes it very helpful for intricate medical images.","metadata":{}},{"cell_type":"code","source":"# Non-Local Means denoising\nnon_local_means = restoration.denoise_nl_means(original_image, h=1.0, fast_mode=True)\n\nplt.imshow(non_local_means, cmap='gray')\nplt.title('Non-Local Means Denoised Image')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T17:49:45.612616Z","iopub.execute_input":"2023-11-08T17:49:45.613372Z","iopub.status.idle":"2023-11-08T17:49:57.718765Z","shell.execute_reply.started":"2023-11-08T17:49:45.613329Z","shell.execute_reply":"2023-11-08T17:49:57.717528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 8. Visual Comparison\n\nFinally, to visually compare all these methods side by side, you could create a composite plot:","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(15, 10))\nax = axes.ravel()\n\nmethods = ['Original', 'Gaussian Blur', 'Median', 'Bilateral', 'Wavelet', 'Non-Local Means']\nimages = [original_image, gaussian_blur, median_filtered, bilateral_filtered, wavelet_denoised, non_local_means]\n\nfor i, (method, image) in enumerate(zip(methods, images)):\n    ax[i].imshow(image, cmap='gray')\n    ax[i].set_title(method)\n    ax[i].axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T17:50:42.332405Z","iopub.execute_input":"2023-11-08T17:50:42.332886Z","iopub.status.idle":"2023-11-08T17:50:45.090931Z","shell.execute_reply.started":"2023-11-08T17:50:42.332850Z","shell.execute_reply":"2023-11-08T17:50:45.089654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 9. Choosing the Right Technique\nThe kind of noise present in the images, their properties, and the demands of the ensuing image analysis tasks all play a role in determining which denoising technique is best for medical images. To get the greatest outcomes, a variety of techniques are frequently applied.\n\n## 10. Conclusion:\nMedical image denoising is a challenging but crucial process that has a big influence on the results of image analysis and the clinical decisions that follow. It is possible for researchers and clinicians to choose their image preprocessing pipelines with knowledge of the advantages and disadvantages of each denoising technique. Deep learning and other cutting-edge techniques will probably be included more frequently as technology develops, providing even more potent tools for enhancing the quality of medical images.","metadata":{}},{"cell_type":"markdown","source":"## 11. Future Directions\n\nIf you like this notebook then checkout my other starter notebooks for Advanced UNet Model below:\n\n* [UNet using TensorFlow Starter](https://www.kaggle.com/code/salmankhaliq22/unet-tensorflow-starter-sennet-hoa)\n* [Attention UNet using TensorFlow from Scratch](https://www.kaggle.com/code/salmankhaliq22/attention-unet-tensorflow-starter-sennet-hoa)\n* [SegNet using TensorFlow From Scratch](https://www.kaggle.com/code/salmankhaliq22/segnet-tensorflow-starter-sennet-hoa)","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}