Shallow depth of field rendering in MATLAB

Alec Jacobson

December 27, 2014

weblog/

cheburashka shallow depth of field

I tried a little experiment with creating lens blur effects using the simple 3D plotter.

Here's a little chuck of code to move the camera around a point aimed at a target point in the scene and accumulate the result in a buffer.

t = tsurf(F,V,'EdgeColor','none','SpecularStrength',0,'CData',C,fphong);
camproj('persp');
im = [];
count = 0;
sam = 50;
w = 0.2;
target = camtarget;
pos = campos;
[THETA,PHI] = meshgrid(w*linspace(-2*pi,2*pi,sam),w*linspace(-2*pi,2*pi,sam));
prev_theta = 0;
prev_phi = 0;
for s = randperm(numel(THETA))
  theta = THETA(s);
  phi = PHI(s);
  camorbit(-prev_theta,-prev_phi);
  prev_theta = theta;
  prev_phi = phi;
  camorbit(theta,phi);
  frame = getframe(gcf);
  frame = im2double(frame.cdata);
  count = count+1;
  if isempty(im)
    im = zeros(size(frame));
  end
  im = im+frame;
end
im = imtrim(im/count,'Threshold',0);
close all;
imshow(im);

You can get away with fewer samples if the "size of the lens" is smaller. This one uses w=0.1; sam=10:

cheburashka less shallow depth of field