clf;
clc,clear,close all;
I=imread('peppers.png'); % 读入图像
I1=rgb2gray(I); % 图像灰度转换
figure;imshow(I);title('Original Image');
figure;imshow(I1);title('Original Grayimage');
PSF=fspecial('gaussian',7,10); % 设置高斯低通滤波器
Blurred=imfilter(I1,PSF,'symmetric','conv'); % 图像卷积计算,symmetric图像大小通过镜像反射其边界来扩展
figure;imshow(Blurred); title('Blurred Image');
UNDERPSF = ones(size(PSF)-4); % 欠尺寸PSF,比原PSF小4
[J1,P1] = deconvblind(Blurred,UNDERPSF); % J1表示图形,P1表示PSF模型
figure;imshow(J1);title('Deblurring with Undersized PSF'); % 欠尺寸PSF,图像复原,比原PSF小4
OVERPSF = padarray(UNDERPSF,[4 4],'replicate','both'); % 过尺寸PSF,比原PSF大4
[J2,P2] = deconvblind(Blurred,OVERPSF); % padarray填充图像或填充数组
figure;imshow(J2);title('Deblurring with Oversized PSF'); % 过尺寸图像复原,比原PSF大4
INITPSF = padarray(UNDERPSF,[2 2],'replicate','both'); % 全尺寸PSF
[J3,P3] = deconvblind(Blurred,INITPSF); % 图像复原,原尺寸PSF
figure;imshow(J3);title('Deblurring with INITPSF'); % 设置图像标题
figure;
subplot(221);imshow(PSF,[],'InitialMagnification','fit');title('True PSF'); % 图像标题显示
subplot(222);imshow(P1,[],'InitialMagnification','fit');title('Reconstructed Undersized PSF'); % 图像标题显示
subplot(223);imshow(P2,[],'InitialMagnification','fit');title('Reconstructed Oversized PSF'); % 图像标题显示
subplot(224);imshow(P3,[],'InitialMagnification','fit');title('Reconstructed true PSF'); % 图像标题显示
WEIGHT = edge(I1,'sobel',0.1); % 图像边缘显示
se = strel('disk',2);
WEIGHT = 1-double(imdilate(WEIGHT,se)); % imdilate函数用于对图像实现膨胀操作
WEIGHT([1:3 end-[0:2]],:) = 0; % 上下边缘
WEIGHT(:,[1:3 end-[0:2]]) = 0; % 左右边缘
figure;imshow(WEIGHT);title('Weight array');
[J,P] = deconvblind(Blurred,INITPSF,30,[],WEIGHT); % 图像复原
figure;
imshow(J);
title('Deblurred Image');
P1 = 2;
P2 = 2;
FUN = @(PSF) padarray(PSF(P1+1:end-P1,P2+1:end-P2),[P1 P2]); % padarray填充图像或填充数组。
[JF,PF] = deconvblind(Blurred,OVERPSF,30,[],WEIGHT,FUN); % 图像复原
figure; % 新建窗口
imshow(JF); % 显示处理后的图像
title('Deblurred Image'); % 去模糊图像