Time for action – creating the bokeh effect in an image

We will now work on another night image taken in Berlin, Germany. We will try to isolate the light bulb soldier and perform blurring in the other areas of the image. The function we will use will be able to handle both grayscale and color images. Let's see the function:

function [output] = Bokeh(input, radius)

% Function that performs blurring on the whole image except a user defined
% ROI,using a disk kernel. The effect resembles the bokeh effect.
% Inputs:
%        input  - Input image
%        radius – User's choice of radius for the disk kernel
% Output:
%        output - Output image (only user-defined ROI stays in focus)

kernel = fspecial('disk',radius);       % Create disk kernel
disp('Select area to keep in focus!')   % Display message to user
mask = roipoly(input);           % User selects area of interest
output = [];                     % Start with an empty image
for i = 1:size(input,3)          % Covering the case of color images
    cropped = input(:,:,i);      % Perform per-channel processing
    channel = input(:,:,i);             % Replica of channel
    cropped(mask == 1) = 0;             % Keep only ROI outside mask
    cropped = imfilter(cropped,kernel); % Perform blurring out of ROI
    channel(mask==0) = cropped(mask==0); % Only keep ROI unaffected
    output = cat(3,output,channel);  % Concatenate channels
end

Now, we can use our function on the image described:

  1. Let's start by loading our image:
    >> img = imread('soldier.jpg'),
  2. Then, we must call our Bokeh function, giving it the input image name and the radius of the filter (we will use 15 pixels):
    >> [output] = Bokeh(soldier,15);

    We get the following message displayed by our function:

    Select area to keep in focus!

  3. Now we select the area we want to keep in focus:
    Time for action – creating the bokeh effect in an image
  4. Now, let's show our original image and our artistic result next to each other:
    >> subplot(1,2,1),imshow(img),title('Original')
    >> subplot(1,2,2),imshow(output),title('Bokeh effect')
    Time for action – creating the bokeh effect in an image

What just happened?

In this example, you got to learn a new way to process your images so that you add an artistic effect to them. The process of ROI selection covered in previous chapters was used so that we select a region we want to remain unaffected. Then, for all channels of the image (whether it has one or three), we perform blurring with a disk kernel, which is a good approximation of the bokeh effect caused by the rendering of out-of-focus light sources by a photographic lens. This way, you can make the area outside your selection seem naturally out-of-focus. In a similar manner, you can add other effects in selected parts of your image. You just have to be careful of what regions you select to take place in the processing (set to 1 in your mask) and which ones you want to keep unaffected (set to zero in your mask).

Have a go hero – add a motion effect in your image

Now it is your turn to take the wheel. Try to alter the Bokeh function we wrote to perform blurring using the motion filter instead of the disk kernel. The motion filter adds a feel of motion to your images; the larger the kernel, the faster the motion. Wouldn't it be fun if the cars in our soldier image seemed to move? Let's try it. You could base your code on the function we created earlier. Its definition is given below:

function [output] = Motion(input,len)

% Function that performs motion blurring on a user defined
% ROI,using the motionkernel. The effect resembles a local motion.
% Inputs:
%        input  - Input image
%        len    – User's choice of length for the motion in pixels
%        theta  – User's choice of angle for the motion in degrees
% Output:
%        output - Output image (only user-defined ROI appears to
  move)

To check if your function works, you should type:

>> img = imread('soldier.jpg'),
>> [output] = Motion(soldier,25,0);

Then, you should be able to use the mouse to define the ROI you want to appear as moving:

Have a go hero – add a motion effect in your image

When displayed next to the original image, the final result should look something like this:

Have a go hero – add a motion effect in your image
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset