(Digital Image Processing MATLAB+Python) Chapter 8 Image Restoration - Sections 5 and 6: Blind deconvolution restoration and geometric distortion correction

1: Blind deconvolution restoration

(1 Overview

Blind deconvolution restoration : When we consider blind deconvolution restoration in image restoration, we can use the following mathematical symbols and equations to describe the problem

  • Original image : We use I to represent the original image, where I is a two-dimensional discrete function. I ( x , y ) I(x, y)I(x,y ) represents the coordinate(x, y) (x, y)(x,The image intensity value at y )
  • Fuzzy kernel : we use HHH represents the unknown blur kernel or point spread function, which is the cause of image blur. H ( u , v ) H(u, v)H(u,v ) represents the blur kernel value in the frequency domain, where(u, v) (u, v)(u,v ) is the coordinate in the frequency domain
  • Blurry image : we use BBB represents the blurred image, also known as blurred image. B ( x , y ) B(x, y)B(x,y ) represents the coordinate(x, y) (x, y)(x,The blurred image intensity value at y )
  • Goal : Our goal is to recover the original image III

In blind deconvolution restoration, we first need to estimate the blur kernel HHH and then apply it to the blurred imageBBB to restore original imageIII

  • Estimating the blur kernel : by observing the known blurred image BBB and unknown original imageIIThe relationship between I , we can use various methods to estimate the blur kernel HHH. _ This can be expressed as the following equation:B ( x , y ) = I ( x , y ) ⊗ H ( x , y ) B(x, y)=I(x, y) \otimes H(x, y)B(x,y)=I(x,y)H(x,y)
  • Restore the original image : Once the blur kernel H is estimated, we can apply it to the blurred image B to restore the original image III. _ This can be expressed as the following equation:I ( x , y ) = B ( x , y ) ⊗ H − 1 ( x , y ) I(x,y)=B(x,y)\otimes H^{-1} (x,y)I(x,y)=B(x,y)H1(x,y)

By solving the above equation, we can achieve blind deconvolution restoration to restore the original image I as accurately as possible. However, in practice, blind deconvolution restoration may face challenges due to factors such as noise and estimation errors, and other technologies and algorithms need to be used for auxiliary processing and improvement.

(2) Procedure

As follows: Maximum likelihood estimation blind restoration filtering for blurred images

Insert image description here


matlab implementation :

deconvblindfunction is a function used for blind deconvolution restoration. It can be used to restore original images from blurred and noise-processed images, and to estimate as far as possible the unknown point spread function (PSF) that causes image blur. Its syntax format is as follows

[J, P] = deconvblind(A, PSF, NUMIT, DAMPAR, WEPS, INITPSF)

Parameter Description:

  • A: Image processed with blur and noise.
  • PSF: estimate of initial point spread function.
  • NUMIT: Number of iterations, controlling the convergence speed of the algorithm.
  • DAMPAR: Damping parameter, used to control the weight of the regularization term to balance deconvolution and constraints.
  • WEPS: Weight threshold, used to determine non-zero elements in the weight matrix.
  • INITPSF: Estimation of initial point spread function.

return value:

  • J: Restored image.
  • P: estimated point spread function
clear,clc,close all;
I=im2double(rgb2gray(imread('flower.jpg')));
PSF=fspecial('gaussian',7,10);%产生一个高斯低通滤波器,模板尺寸为[7 7],滤波器的标准差为10
V=0.0001;%高斯加性噪声的标准差
IF1=imfilter(I,PSF);%原图像通过高斯低通滤波器
BlurredNoisy=imnoise(IF1,'gaussian',0,V);%加入高斯噪声
WT = zeros(size(I));WT(5:end-4,5:end-4) = 1;
INITPSF = ones(size(PSF));
[J,P] = deconvblind(BlurredNoisy,INITPSF,20,10*sqrt(V),WT);
subplot(221),imshow(BlurredNoisy),title('高斯模糊加噪声图像');
subplot(222),imshow(PSF,[]),title('True PSF');
subplot(223),imshow(J),title('Deblurred Image');
subplot(224),imshow(P,[]),title('Recovered PSF');
imwrite(J,'DeblurredI.jpg');


Python implementation :

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图像
image = cv2.imread('flower.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
I = cv2.normalize(gray_image.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)

# 生成高斯模糊核
PSF = cv2.getGaussianKernel(7, 10) * cv2.getGaussianKernel(7, 10).T

# 加入高斯噪声
V = 0.0001
IF1 = cv2.filter2D(I, -1, PSF)
BlurredNoisy = cv2.randn(IF1, 0, V)

# 创建权重矩阵
WT = np.zeros_like(I)
WT[4:-4, 4:-4] = 1

# 初始化PSF
INITPSF = np.ones_like(PSF)

# 盲去卷积复原
J, P = cv2.deconvblind(BlurredNoisy, INITPSF, 20, 10 * np.sqrt(V), WT)

# 显示结果
plt.subplot(221), plt.imshow(BlurredNoisy, cmap='gray')
plt.title('高斯模糊加噪声图像')
plt.subplot(222), plt.imshow(PSF, cmap='gray')
plt.title('True PSF')
plt.subplot(223), plt.imshow(J, cmap='gray')
plt.title('Deblurred Image')
plt.subplot(224), plt.imshow(P, cmap='gray')
plt.title('Recovered PSF')
plt.savefig('DeblurredI.jpg')
plt.show()

2: Geometric distortion correction

(1 Overview

Geometric distortion correction : is a technique used to correct geometric distortion in images . Geometric distortion can be caused by a variety of factors, such as camera perspective, lens distortion, etc. The goal of geometric distortion correction is to restore the geometry and proportions of an image so that it is closer to the original scene. In geometric distortion correction we can describe the problem using the following mathematical symbols and equations

  • Original image : We used III represents the original image, where I is a two-dimensional discrete function. I ( x , y ) I(x, y)I(x,y ) represents the coordinate(x, y) (x, y)(x,The image intensity value at y )
  • Distortion model : We assume that there is a distortion model that maps each pixel in the original image to a new position in the corrected image. This mapping relationship can be expressed as a function, such as F (x, y) F(x, y)F(x,y)
  • Corrected image : We use CCC represents the corrected image, whereCCC is a two-dimensional discrete function. C ( x , y ) C(x, y)C(x,y ) represents the coordinates (x, y) (x, y)in the corrected image(x,The image intensity value at y )
  • Inverse transformation : In order to perform geometric distortion correction, we need to find the inverse transformation of the distortion model, which maps the pixels in the corrected image back to the positions of the original image. This inverse transformation can be expressed as the function F − 1 ( x , y ) F^{-1}(x,y)F1(x,y)
  • Correction process : The correction process involves mapping each pixel in the original image to a new position in the corrected image according to the inverse transformation of the distortion model to obtain the corrected image C. This can be expressed as the following equation: C ( x , y ) = I ( F − 1 ( x , y ) ) C(x,y)=I(F^{-1}(x,y))C(x,y)=I(F1(x,y))

By solving the above equation, we can achieve geometric distortion correction, remapping the pixels in the corrected image back to the position of the original image, thereby restoring the geometry and proportions of the image

(2) Procedure

as follows

Insert image description here


matlab implementation :

clear,clc,close;
Image=im2double(imread('lotus.jpg'));
[h,w,c]=size(Image);
figure,imshow(Image),title('原图');
RI=imrotate(Image,20);
tform=maketform('affine',[1 0.5 0;0.5 1 0; 0 0 1]);
NewImage=imtransform(RI,tform);
figure,imshow(NewImage),title('几何畸变的图像');
imwrite(NewImage,'GDImage.jpg'); 
cpselect(NewImage,Image);
input_points=[709 577;409 270;320 370];
base_points=[487 305;374 41;134 159];
tform=cp2tform(input_points,base_points,'affine');
result=imtransform(NewImage,tform,'XData',[1 w],'YData',[1 h]);
figure,imshow(result),title('校正后的图像');
imwrite(result,'jiaozheng.jpg');

python implementation :

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图像
image = cv2.imread('lotus.jpg')
Image = cv2.normalize(image.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)

# 显示原始图像
plt.figure()
plt.imshow(Image)
plt.title('原图')

# 旋转图像
RI = cv2.rotate(Image, cv2.ROTATE_90_CLOCKWISE)

# 创建仿射变换矩阵
tform = np.array([[1, 0.5, 0], [0.5, 1, 0], [0, 0, 1]])

# 进行几何畸变
NewImage = cv2.warpPerspective(RI, tform, (RI.shape[1], RI.shape[0]))

# 显示几何畸变的图像
plt.figure()
plt.imshow(NewImage)
plt.title('几何畸变的图像')
plt.savefig('GDImage.jpg')

# 特征点对应
input_points = np.array([[709, 577], [409, 270], [320, 370]], dtype=np.float32)
base_points = np.array([[487, 305], [374, 41], [134, 159]], dtype=np.float32)

# 计算仿射变换矩阵
tform = cv2.getAffineTransform(input_points, base_points)

# 进行校正
result = cv2.warpAffine(NewImage, tform, (Image.shape[1], Image.shape[0]))

# 显示校正后的图像
plt.figure()
plt.imshow(result)
plt.title('校正后的图像')
plt.savefig('jiaozheng.jpg')

plt.show()

Guess you like

Origin blog.csdn.net/qq_39183034/article/details/130742734
Recommended