A lightweight computer vision project for automatic shadow removal using iterative shading and reflectance decomposition.
This project removes unwanted shadows from images by decomposing the scene into shading (illumination) and reflectance (intrinsic surface color) components. It uses adaptive thresholding with integral images to identify shadow regions and then iteratively refines the decomposition to recover the true appearance of the scene.
Built entirely with OpenCV and NumPy — no deep learning required.
-
Adaptive Thresholding with Integral Images
- Computes a local threshold for each pixel using an integral image for efficiency.
- Produces a binary mask distinguishing well-lit regions from shadowed regions.
-
Iterative Shading & Reflectance Decomposition
- Initializes reflectance
Rand shadingSfrom the input image. - For each iteration:
- Estimates shading by convolving the masked reflectance with a local averaging filter.
- Updates reflectance as
R = I / (S + ε). - Clips values to valid range and repeats.
- After convergence, applies Otsu-based shadow masking and global intensity correction to produce the final shadow-free image.
- Initializes reflectance
-
Post-Processing
- Otsu thresholding on the final shading map to refine the shadow mask.
- Global mean scaling per channel to restore natural brightness.
- Python 3.7+
- OpenCV (
opencv-python) - NumPy
- Matplotlib
- SciPy
- Seaborn
Install with:
pip install opencv-python numpy matplotlib scipy seaborn-
Clone the repository
git clone https://github.com/pctablet505/Shadow-Removal.git cd Shadow-Removal -
Run the script
python "iterative shading reflectance.py"The script will:
- Read images from the specified input directory
- Remove shadows using the iterative algorithm
- Save side-by-side before/after visualizations to the output directory
-
Adjust parameters
n_iters: Number of shading-reflectance iterations (default:10)dx,dy: Local filter size for shading estimation (default:5)
The algorithm produces shadow-free images by separating illumination changes from the underlying scene content. Sample outputs show clean removal of hard shadows while preserving natural textures and colors.
| Input | Output |
|---|---|
| Original image with cast shadows | Shadow-removed image with uniform illumination |
Note: The script currently uses hardcoded input/output paths. Update the
pathand output folder variables inside the script to point to your own images.
- Inspired by intrinsic image decomposition and classical retinex theory.
- Integral-image adaptive thresholding for fast local binarization.