An open API service indexing awesome lists of open source software.

https://github.com/mathusanm6/blur-image

A Python tool for blurring images pixel by pixel using mathematical algorithms and also providing an approximate solution for sharpening the blurred image.
https://github.com/mathusanm6/blur-image

blind-richardson-lucy blur convolve image image-processing kernel-convolution richardson-lucy-deconvolution scipy sharpen sharpen-image

Last synced: about 1 month ago
JSON representation

A Python tool for blurring images pixel by pixel using mathematical algorithms and also providing an approximate solution for sharpening the blurred image.

Awesome Lists containing this project

README

        

# Blur-Image

## Overview

**Blur-Image** is a compact image processing toolkit developed in Python, designed to improve image sharpness through advanced blurring techniques. Based on the Richardson-Lucy deconvolution algorithm, this toolkit allows users to apply different blur kernels and iterative enhancements to improve image quality.

## Features

- **Blurring and sharpening**: Implement Richardson-Lucy deconvolution for image blurring, as well as standard blurring techniques using different kernels.
- **Batch image processing**: Process entire directories of images for bulk blurring and sharpening.
- **Quality measurements**: Calculate and report the Peak Signal-to-Noise Ratio (PSNR) to measure the quality of processed images.
- **Visual logging**: Color-coded console for easy monitoring of processing steps.

## Installation

Clone the repository to your local machine:

```bash
git clone https://github.com/mathusanm6/Blur-Image.git
cd Blur-Image
```

### Prerequisites

Ensure you have Python installed along with the following packages:

- numpy
- scipy
- pillow (PIL)

You can install the required packages via pip:

```bash
pip install numpy scipy pillow
```

## Usage

After tuning the parameters in the `core.py` and `blind_core.py` files, you can run the toolkit using the following command:

```bash
python ./run.sh
```

## Richardson-Lucy Deconvolution

Below are some examples of images processed by the Blur-Image toolkit, showing the original images, the blurred versions, and the deblurred outputs after applying various kernels and iteration counts.

#### Process

Blurring are done using the following kernels:

- Average 3x3
- Average 5x5
- Average 11x11
- Gaussian 3x3, sigma: 1.0
- Gaussian 3x3, sigma: 2.0
- Gaussian 5x5, sigma: 1.0
- Gaussian 5x5, sigma: 2.0

Sharpening are done knowing the kernel used for blurring and the number of iterations using the Richardson-Lucy deconvolution algorithm as follows:

## Richardson-Lucy Deconvolution Algorithm

**Input:** Blurred image `I`, PSF `P`, number of iterations `n_it`
**Output:** Restored image `J`

```python
# Initialize
J0 = I

# Iterative deconvolution
for n in range(1, n_it + 1):
# Convolve Jn with P to obtain a blurred estimation I_estimated
I_estimated = convolve(Jn, P)

# Calculate the relative blur ratio
Ratio = I / (I_estimated + epsilon)

# Convolve this ratio with the mirror of the PSF
Correction = convolve(Ratio, P_mirror)

# Update the estimation
Jn = Jn * Correction

# Return the final restored image
return Jn_it
```

#### Original Image



grayscale flower



(1) Grayscale Flower




tiger



(2) Tiger



#### Processed Images

##### (1) Grayscale Flower



Average 3x3




Average 5x5




Average 11x11




Gaussian 3x3, sigma: 1.0




Gaussian 3x3, sigma: 2.0




Gaussian 5x5, sigma: 1.0




Gaussian 5x5, sigma: 2.0





Blurred Images



blurred flower




blurred flower




blurred flower




blurred flower




blurred flower




blurred flower




blurred flower





Unblurred Images



unblurred flower


5 Iterations




unblurred flower


5 Iterations




unblurred flower


5 Iterations




unblurred flower


5 Iterations




unblurred flower


5 Iterations




unblurred flower


5 Iterations




unblurred flower


5 Iterations









unblurred flower


10 Iterations




unblurred flower


10 Iterations




unblurred flower


10 Iterations




unblurred flower


10 Iterations




unblurred flower


10 Iterations




unblurred flower


10 Iterations




unblurred flower


10 Iterations









unblurred flower


15 Iterations




unblurred flower


15 Iterations




unblurred flower


15 Iterations




unblurred flower


15 Iterations




unblurred flower


15 Iterations




unblurred flower


15 Iterations




unblurred flower


15 Iterations



##### (2) Tiger



Average 3x3




Average 5x5




Average 11x11




Gaussian 3x3, sigma: 1.0




Gaussian 3x3, sigma: 2.0




Gaussian 5x5, sigma: 1.0




Gaussian 5x5, sigma: 2.0





Blurred Images



blurred tiger




blurred tiger




blurred tiger




blurred tiger




blurred tiger




blurred tiger




blurred tiger





Unblurred Images



unblurred tiger


5 Iterations




unblurred tiger


5 Iterations




unblurred tiger


5 Iterations




unblurred tiger


5 Iterations




unblurred tiger


5 Iterations




unblurred tiger


5 Iterations




unblurred tiger


5 Iterations









unblurred tiger


10 Iterations




unblurred tiger


10 Iterations




unblurred tiger


10 Iterations




unblurred tiger


10 Iterations




unblurred tiger


10 Iterations




unblurred tiger


10 Iterations




unblurred tiger


10 Iterations









unblurred tiger


15 Iterations




unblurred tiger


15 Iterations




unblurred tiger


15 Iterations




unblurred tiger


15 Iterations




unblurred tiger


15 Iterations




unblurred tiger


15 Iterations




unblurred tiger


15 Iterations



## Blind Richardson-Lucy Deconvolution

### Process

Sharpening are done not knowing the kernel used for blurring and the number of iterations using the Blind Richardson-Lucy deconvolution algorithm as follows:

## Blind Richardson-Lucy Deconvolution Algorithm

**Input:** Blurred image `I`, initial PSF `P`, number of iterations for image `n_it`, number of iterations for PSF `psf_it`
**Output:** Restored image `J`, refined PSF `P`

```python
# Initialize
J0 = I

# Iterative deconvolution
for n in range(1, n_it + 1):
# Convolve Jn with P to obtain a blurred estimation I_estimated
I_estimated = convolve(Jn, P)

# Calculate the relative blur ratio
Ratio = I / (I_estimated + epsilon)

# Convolve this ratio with the mirror of the PSF
Correction = convolve(Ratio, P_mirror)

# Update the estimation
Jn = Jn * Correction

# PSF refinement
for m in range(1, psf_it + 1):
# Convolve Jn+1 with P to obtain a new blurred estimation I_estimated
I_estimated = convolve(Jn, P)

# Calculate the error ratio
E = I / (I_estimated + epsilon)

# Convolve this ratio with the mirror of the restored image Jn+1
PSF_Update = convolve(E, Jn_mirror)

# Update the PSF
P = P * PSF_Update

# Normalize the PSF
P = P / sum(P)

# Return the final restored image and refined PSF
return Jn_it, P
```

### Original Image



blurred tiger



(A) Blurred tiger (Unknown Kernel)



### Processed Images

#### (A) Blurred Tiger (Unknown Kernel)



Average 3x3




Average 5x5




Average 11x11




Gaussian 5x5, sigma: 1.0




Gaussian 5x5, sigma: 2.0









unblurred tiger


15 Iterations, 25 PSF Iterations




unblurred tiger


15 Iterations, 25 PSF Iterations




unblurred tiger


15 Iterations, 25 PSF Iterations




unblurred tiger


15 Iterations, 25 PSF Iterations




unblurred tiger


15 Iterations, 25 PSF Iterations









unblurred tiger


30 Iterations, 25 PSF Iterations




unblurred tiger


30 Iterations, 25 PSF Iterations




unblurred tiger


30 Iterations, 25 PSF Iterations




unblurred tiger


30 Iterations, 25 PSF Iterations




unblurred tiger


30 Iterations, 25 PSF Iterations









unblurred tiger


60 Iterations, 25 PSF Iterations




unblurred tiger


60 Iterations, 25 PSF Iterations




unblurred tiger


60 Iterations, 25 PSF Iterations




unblurred tiger


60 Iterations, 25 PSF Iterations




unblurred tiger


60 Iterations, 25 PSF Iterations









unblurred tiger


120 Iterations, 25 PSF Iterations




unblurred tiger


120 Iterations, 25 PSF Iterations




unblurred tiger


120 Iterations, 25 PSF Iterations




unblurred tiger


120 Iterations, 25 PSF Iterations




unblurred tiger


120 Iterations, 25 PSF Iterations



## License

This project is licensed under the MIT License. See the [LICENSE.md](LICENSE.md) file for details.