基于求解器的大规模约束线性最小二乘法
此示例说明如何通过求解大规模有界线性最小二乘优化问题来恢复模糊图像。该示例使用基于求解器的方法。有关基于问题的方法,请参阅基于问题的大规模约束线性最小二乘法。
问题
这是一张人们坐在一辆车里的照片,车牌很有趣。
load optdeblur [m,n] = size(P); mn = m*n; imshow(P) title(sprintf('Original Image, size %d-by-%d, %d pixels',m,n,mn))
问题是拍摄这张照片的模糊版本并尝试将其去模糊。起始图像是黑白的,这意味着它由 m x n 矩阵 P 中从 0 到 1 的像素值组成。
添加运动模糊
通过对每个像素及其上下 5 个像素求平均值来模拟垂直运动模糊的效果。构建一个稀疏矩阵 D
,用单个矩阵乘法进行模糊处理。
blur = 5; mindex = 1:mn; nindex = 1:mn; for i = 1:blur mindex=[mindex i+1:mn 1:mn-i]; nindex=[nindex 1:mn-i i+1:mn]; end D = sparse(mindex,nindex,1/(2*blur+1));
画出 D 的图。
cla axis off ij xs = 31; ys = 15; xlim([0,xs+1]); ylim([0,ys+1]); [ix,iy] = meshgrid(1:(xs-1),1:(ys-1)); l = abs(ix-iy)<=5; text(ix(l),iy(l),'x') text(ix(~l),iy(~l),'0') text(xs*ones(ys,1),1:ys,'...'); text(1:xs,ys*ones(xs,1),'...'); title('Blurring Operator D (x = 1/11)')
将图像 P 乘以矩阵 D 以创建模糊图像 G。
G = D*(P(:));
figure
imshow(reshape(G,m,n));
title('Blurred Image')
图像不再清晰;您无法再看清车牌号。
去模糊图像
为了去模糊,假设您知道模糊算子 D。您能多好地去除模糊并恢复原始图像 P?
最简单的方法是求解 x 的最小二乘问题:
约束条件为 。
这个问题以模糊矩阵 D 为给定,并尝试找到使 Dx 最接近 G = DP 的 x。为了使解能够表示合理的像素值,将解限制在 0 到 1 之间。
lb = zeros(mn,1); ub = 1 + lb; sol = lsqlin(D,G,[],[],[],[],lb,ub);
Minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
xpic = reshape(sol,m,n);
figure
imshow(xpic)
title('Deblurred Image')
去模糊后的图像比模糊后的图像清晰得多。您可以再次读取车牌号。然而,去模糊后的图像有一些伪影,例如右下方路面区域的水平带。也许可以通过正则化来去除这些伪影。
正则化
正则化是使解平滑的一种方法。正则化方法有很多。对于简单的方法,在目标函数中添加一个项,如下所示:
约束条件为 。
项 使得所得的二次问题更加稳定。采取 并再次求解问题。
addI = speye(mn); sol2 = lsqlin(D+0.02*addI,G,[],[],[],[],lb,ub);
Minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
xpic2 = reshape(sol2,m,n);
figure
imshow(xpic2)
title('Deblurred Regularized Image')
显然,这种简单的正则化并不能消除伪影。