Deconvolution Filters

Deconvolution Kernel Filter in Python

         A deconvolution filter acts as the inverse matrix operation to a convolution operation. When applying a convolution transformation, such as using a Gaussian blur kernel filter, attempting to reverse this operation cannot fully restore the original image. This limitation arises from the averaging of pixel values during convolution, resulting in the loss of certain details. However, deconvolution is crucial in image restoration and deblurring processes.

         In the presented example, we initially apply a Gaussian blur convolution filter and then proceed to restore the original image using a Deconvolution filter:

## gaussian blur 3x3 convolution kernel
[
  [1, 2, 1,],
  [2, 4, 2,],
  [1, 2, 1,],
]
DeconvolutionFilterOptions(ConvolutionFilter.get_gaussian(size, sigma))

         The processed image may not be identical to the original, it does reveal more details following the deblurring process.

Original image
Deconvolution filter
Original vector image
Deconvolution Gaussian blur kernel filter in Python
Deconvolution kernel filter

Python code example

         The comprehensive Python code example below demonstrates the application of the Aspose.Imaging Python API. Utilize the `ConvolutionFilter` class with the get_gaussian() blurring method and then proceed to deblur using the DeconvolutionFilterOptions. In this illustration, image templates in raster PNG and vector SVG formats are loaded from the "templates" folder, and a set of filters are applied from a predefined list.

import os, random
import aspose.pycore as aspycore
from aspose.imaging import Image, RasterImage, VectorImage
from aspose.imaging.imagefilters.complexutils import Complex
from aspose.imaging.imagefilters.convolution import *
from aspose.imaging.imagefilters.filteroptions import *
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
def delete_file(file):
if delete_output:
os.remove(file)
# Example code:
def filter(raster, options, output_path):
raster.filter(raster.bounds, options)
raster.save(output_path)
def get_random_kernel(cols, rows):
custom_kernel = [0.0] * (cols * rows)
for y in range(rows):
for x in range(cols):
custom_kernel[y*cols + x] = random.random()
return custom_kernel
size: int = 5
sigma: float = 1.5
angle: float = 45
custom_kernel = get_random_kernel(size, 7)
custom_complex = ConvolutionFilter.to_complex(custom_kernel)
kernel_filters = [
# convolution filters
ConvolutionFilterOptions(ConvolutionFilter.get_emboss_3x3()),
ConvolutionFilterOptions(ConvolutionFilter.get_emboss_5x5()),
ConvolutionFilterOptions(ConvolutionFilter.get_sharpen_3x3()),
ConvolutionFilterOptions(ConvolutionFilter.get_sharpen_5x5()),
ConvolutionFilterOptions(ConvolutionFilter.get_blur_box(size)),
ConvolutionFilterOptions(ConvolutionFilter.get_blur_motion(size, angle)),
ConvolutionFilterOptions(ConvolutionFilter.get_gaussian(size, sigma)),
ConvolutionFilterOptions(custom_kernel),
GaussianBlurFilterOptions(size, sigma),
SharpenFilterOptions(size, sigma),
MedianFilterOptions(size),
# deconvolution filters
DeconvolutionFilterOptions(ConvolutionFilter.get_gaussian(size, sigma)),
DeconvolutionFilterOptions(custom_kernel),
DeconvolutionFilterOptions(custom_complex),
GaussWienerFilterOptions(size, sigma),
MotionWienerFilterOptions(size, sigma, angle)
]
data_dir = templates_folder
input_paths = [
os.path.join(data_dir, "template.png"),
os.path.join(data_dir, "template.svg")
]
outputs = []
for input_path in input_paths:
for i, options in enumerate(kernel_filters):
with Image.load(input_path) as image:
output_path = f"{input_path}-{i}.png"
if aspycore.is_assignable(image, RasterImage):
raster = aspycore.as_of(image, RasterImage)
filter(raster, options, output_path)
outputs.append(output_path)
elif aspycore.is_assignable(image, VectorImage):
vector = aspycore.as_of(image, VectorImage)
vector_as_png = input_path + ".png"
if not os.path.exists(vector_as_png):
vector.save(vector_as_png)
outputs.append(vector_as_png)
with Image.load(vector_as_png) as png:
filter(aspycore.as_of(png, RasterImage), options, output_path)
outputs.append(output_path)
# Removing all output files
for p in outputs:
delete_file(p)