这是一个非常简单的 Matlab 实现:
function result = RL_deconv(image, PSF, iterations)
% to utilise the conv2 function we must make sure the inputs are double
image = double(image);
PSF = double(PSF);
latent_est = image; % initial estimate, or 0.5*ones(size(image));
PSF_HAT = PSF(end:-1:1,end:-1:1); % spatially reversed psf
% iterate towards ML estimate for the latent image
for i= 1:iterations
est_conv = conv2(latent_est,PSF,'same');
relative_blur = image./est_conv;
error_est = conv2(relative_blur,PSF_HAT,'same');
latent_est = latent_est.* error_est;
end
result = latent_est;
original = im2double(imread('lena256.png'));
figure; imshow(original); title('Original Image')

hsize=[9 9]; sigma=1;
PSF = fspecial('gaussian', hsize, sigma);
blr = imfilter(original, PSF);
figure; imshow(blr); title('Blurred Image')

res_RL = RL_deconv(blr, PSF, 1000);
figure; imshow(res_RL); title('Recovered Image')

您也可以在频域中工作,而不是像上面那样在空间域中工作。在这种情况下,代码将是:
function result = RL_deconv(image, PSF, iterations)
fn = image; % at the first iteration
OTF = psf2otf(PSF,size(image));
for i=1:iterations
ffn = fft2(fn);
Hfn = OTF.*ffn;
iHfn = ifft2(Hfn);
ratio = image./iHfn;
iratio = fft2(ratio);
res = OTF .* iratio;
ires = ifft2(res);
fn = ires.*fn;
end
result = abs(fn);
唯一我不太明白的是 PSF 的这种空间反转是如何工作的以及它的用途。如果有人可以为我解释那将是很酷的!我还在寻找一种用于空间变体 PSF(即空间非齐次点扩散函数)的简单 Matlab RL 实现——如果有人想要,请告诉我!
要消除边缘的伪影,您可以在边缘镜像输入图像,然后裁剪掉镜像位,或者image = edgetaper(image, PSF)
在调用之前使用 Matlab 的RL_deconv
。
顺便说一句,原生 Matlab 实现 deconvlucy.m 有点复杂 - 可以在此处找到该源代码,并使用基本算法的加速版本。