SoFunction
Updated on 2024-10-29

Example of python implementation of inverse filtering and Wiener filtering

Constructing a motion fuzzy model

Now assume that the camera does not move, the image f(x,y) moves on the image plane and the image f(x,y) does not change with time except for the movement. Let x0(t) and y0(t) represent the x-component and y-component of the displacement, respectively, then the total exposure of a point on the film during the time T when the shutter is open is the sum of the luminance of a series of corresponding pixels of the image in the process of movement on the point. In other words, a motion blur image is made by superimposing the same image with the original image after a distance delay has been incurred. If the time between the opening and closing of the shutter is ignored, there is:

Since all kinds of motion are superposition of uniform linear motion, thus we only need to consider uniform linear motion. However, due to our own limited level and aiming to explore the idea and direction of finding a way to realize the motion fuzzy recovery method, we failed to construct the model by ourselves, but borrowed the motion fuzzy model established in reference [1]. For the theoretical basis of this model, see reference [1].

Below we describe the model function motion_process(image_size,motion_angle), which contains two parameters: the size of the image size image_size and the angle of the motion motion_angle.

For example, when the motion displacement is 9 and the motion angle is 45 degrees, the model function is constructed as follows:

1. The first step is to create an all-0 matrix of the same size as the image, then find the center_position of the center rows of the all-0 matrix, then calculate the tan and cot values for the angle of the motion, and calculate the offset of the motion.

2. PSF[int(center_position+offset),int(center_position-offset)]=1

3. PSF[int(center_position-offset),int(center_position+offset)]=1

Then the corresponding image of the model is shown below:

The image corresponding to the motion blur model when the motion displacement is 9 and the motion angles are 45°, 30°, and 60°, respectively

import  as graph
import numpy as np
from numpy import fft
import math
import cv2
 
# Simulation motion blur
def motion_process(image_size,motion_angle):
 PSF = (image_size)
 print(image_size)
 center_position=(image_size[0]-1)/2
 print(center_position)
 
 slope_tan=(motion_angle*/180)
 slope_cot=1/slope_tan
 if slope_tan<=1:
  for i in range(15):
   offset=round(i*slope_tan) #((center_position-i)*slope_tan)
   PSF[int(center_position+offset),int(center_position-offset)]=1
  return PSF / () # Normalized brightness for point spread functions
 else:
  for i in range(15):
   offset=round(i*slope_cot)
   PSF[int(center_position-offset),int(center_position+offset)]=1
  return PSF / ()
 
# Motion blurring of images
def make_blurred(input, PSF, eps):
 input_fft = fft.fft2(input)# Perform the Fourier transform of a 2D array
 PSF_fft = fft.fft2(PSF)+ eps
 blurred = fft.ifft2(input_fft * PSF_fft)
 blurred = ((blurred))
 return blurred
 
def inverse(input, PSF, eps):  # Inverse filtering
 input_fft = fft.fft2(input)
 PSF_fft = fft.fft2(PSF) + eps # Noise power, which is known, is considered epsilon
 result = fft.ifft2(input_fft / PSF_fft) # Calculate the Fourier inverse transform of F(u,v)
 result = ((result))
 return result
 
def wiener(input,PSF,eps,K=0.01):  #Wiener filter, K=0.01
 input_fft=fft.fft2(input)
 PSF_fft=fft.fft2(PSF) +eps
 PSF_fft_1=(PSF_fft) /((PSF_fft)**2 + K)
 result=fft.ifft2(input_fft * PSF_fft_1)
 result=((result))
 return result
 
image = ('')
image = (image,cv2.COLOR_BGR2GRAY)
img_h=[0]
img_w=[1]
(1)
("Original Image")
()
(image)  # Display original image
 
(2)
()
# Perform motion blur
PSF = motion_process((img_h,img_w), 60)
blurred = (make_blurred(image, PSF, 1e-3))
 
(231)
("Motion blurred")
(blurred)
 
result = inverse(blurred, PSF, 1e-3) #Inverse filtering
(232)
("inverse deblurred")
(result)
 
result=wiener(blurred,PSF,1e-3)  #Wiener filter
(233)
("wiener deblurred(k=0.01)")
(result)
 
blurred_noisy=blurred + 0.1 * () * \
   .standard_normal() #Add noise, standard_normal to generate random functions.
 
(234)
("motion & noisy blurred")
(blurred_noisy)  # Display images with added noise and motion blur
 
result = inverse(blurred_noisy, PSF, 0.1+1e-3) #Inverse filtering of images with added noise
(235)
("inverse deblurred")
(result)
 
result=wiener(blurred_noisy,PSF,0.1+1e-3)   #Wiener filtering of images with added noise
(236)
("wiener deblurred(k=0.01)")
(result)
 
()

bibliography

[1] He Hongying. Research and implementation of motion blur image recovery algorithm [D]. Master's thesis, Xi'an University of Science and Technology. 2011.

[2] Rafael , Richard , Steven . MATLAB Implementation of Digital Image Processing (2nd Edition) [M]. Ruan Qiuqi , Translated . Beijing: Tsinghua University Press, 2013.

[3] Chen JG. Research on motion blur image restoration algorithm [D]. Master's thesis, Nanchang Aviation University. 2012.

Above this python implementation of inverse filtering and Wiener filtering example is all I have to share with you, I hope to give you a reference, and I hope you support me more.