large-凯发k8网页登录
this example shows how to recover a blurred image by solving a large-scale bound-constrained linear least-squares optimization problem. the example uses the problem-based approach. for the solver-based approach, see .
the problem
here is a photo of people sitting in a car having an interesting license plate.
load optdeblur [m,n] = size(p); mn = m*n; figure imshow(p); colormap(gray); axis off image; title([int2str(m) ' x ' int2str(n) ' (' int2str(mn) ') pixels'])
the problem is to take a blurred version of this photo and try to deblur it. the starting image is black and white, meaning it consists of pixel values from 0 through 1 in the m x n matrix p.
add motion
simulate the effect of vertical motion blurring by averaging each pixel with the 5 pixels above and below. construct a sparse matrix d
to blur with a single matrix multiply.
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));
draw a picture of 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) <= blur; 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)')
multiply the image p by the matrix d to create a blurred image g.
g = d*(p(:)); figure imshow(reshape(g,m,n));
the image is much less distinct; you can no longer read the license plate.
deblurred image
to deblur, suppose that you know the blurring operator d. how well can you remove the blur and recover the original image p?
the simplest approach is to solve a least squares problem for x:
subject to .
this problem takes the blurring matrix d as given, and tries to find the x that makes dx closest to g = dp. in order for the solution to represent sensible pixel values, restrict the solution to be from 0 through 1.
x = optimvar('x',mn,'lowerbound',0,'upperbound',1); expr = d*x-g; objec = expr'*expr; blurprob = optimproblem('objective',objec); sol = solve(blurprob);
solving problem using quadprog. 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.x,m,n);
figure
imshow(xpic)
title('deblurred image')
the deblurred image is much clearer than the blurred image. you can once again read the license plate. however, the deblurred image has some artifacts, such as horizontal bands in the lower-right pavement region. perhaps these artifacts can be removed by a regularization.
regularization
regularization is a way to smooth the solution. there are many regularization methods. for a simple approach, add a term to the objective function as follows:
subject to .
the term makes the resulting quadratic problem more stable. take and solve the problem again.
addi = speye(mn);
expr2 = (d 0.02*addi)*x - g;
objec2 = expr2'*expr2;
blurprob2 = optimproblem('objective',objec2);
sol2 = solve(blurprob2);
solving problem using quadprog. 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.x,m,n);
figure
imshow(xpic2)
title('deblurred regularized image')
apparently, this simple regularization does not remove the artifacts.