Asia/Jakarta
Posts

Color Image Denoising with Salt-and-Pepper Noise and Median Filter in Python

Color Image Denoising with Salt-and-Pepper Noise and Median Filter in Python
June 25, 2025
This project is a color image quality enhancement application built with Python and OpenCV, developed and run in Google Colab. It implements a Salt-and-Pepper noise simulation on color images and applies a Median Filter to reduce the noise, followed by a quantitative evaluation using Peak Signal-to-Noise Ratio (PSNR) to measure the effectiveness of the filtering process. The entire pipeline — from image upload, noise addition, filtering, result visualization, to exporting the output — is encapsulated in a single interactive Jupyter Notebook.
  • Salt-and-Pepper Noise Simulation: Adds configurable Salt-and-Pepper noise to color images by randomly setting pixels to 255 (salt) or 0 (pepper) across all RGB channels, simulating real-world image degradation scenarios.
  • Median Filter Denoising: Applies OpenCV's medianBlur() with a configurable kernel size to remove Salt-and-Pepper noise while preserving edge details, with higher kernel sizes producing stronger but blurrier smoothing.
  • PSNR Quality Evaluation: Calculates Peak Signal-to-Noise Ratio (PSNR) in decibels for both the noisy and filtered images against the original, providing a quantitative metric to evaluate filtering effectiveness — a PSNR above 30 dB indicates high similarity to the original.
  • Interactive Image Upload: Uses Google Colab's files.upload() to allow users to upload any image directly in the notebook environment without needing a local setup.
  • Result Visualization: Displays the original, noisy, and filtered images side-by-side using Matplotlib subplots, with PSNR values shown in the figure title for immediate comparison.
  • Output Export: Saves both the noisy and filtered images as separate files using OpenCV's imwrite() with proper BGR-to-RGB conversion, ensuring correct color output.
  • Python: Core programming language for all image processing logic, numerical computation, and pipeline orchestration.
  • OpenCV (cv2): Used for image reading, BGR-to-RGB conversion, median filtering via medianBlur(), and exporting output images via imwrite().
  • NumPy: Handles array operations for noise generation, pixel manipulation, and MSE calculation in the PSNR formula.
  • Matplotlib: Used to render the three-panel comparison visualization (original, noisy, filtered) with PSNR values as the plot title.
  • Google Colab: Cloud-based notebook environment providing the interactive file upload widget and GPU-accessible runtime for development and testing.
The main challenge in this project was correctly handling color images throughout the noise and filtering pipeline — OpenCV reads images in BGR format while Matplotlib displays in RGB, requiring explicit color space conversion at every display and export step to avoid incorrect color output. Implementing Salt-and-Pepper noise for color images also required careful handling of all three channels simultaneously, ensuring that noisy pixels appear as pure white or black rather than colored artifacts. Calculating PSNR provided a concrete lesson in quantitative image quality assessment — learning that a PSNR above 30 dB indicates strong similarity to the original, and that a higher filtered PSNR compared to the noisy PSNR confirms the median filter's effectiveness. Overall, this project reinforced the importance of understanding data types, color spaces, and measurable evaluation metrics in digital image processing pipelines. The full source code is available on GitHub. Feel free to view, download, or develop it further.
Bash
git clone https://github.com/Afrizal236/penghilang-noise-foto.git
On this page