Focus on a part of an image by applying Gaussian blur and gradient masks.
- Core Image
The tilt-shift effect simulates sharpening or focusing a portion of an image while blurring the rest. Using this technique, you can draw attention to the focused region.
You specify the region to blur by applying a mask image; the shape and values of the mask image determine the location and strength of the blurring. Creating this effect involves the following steps:
To focus on a strip across the image, create two linear gradients representing the portions of the image to blur.
To focus on a circular region in the image, create a radial gradient centered on the region to keep sharp.
Composite the gradients into a mask.
Apply Core Image's
CIMaskedfilter to the original image, inputting the created mask.
Focusing on a Strip of the Image
You can build your mask in any shape, but the general strategy remains the same: leave the mask transparent where you want the original image to stay sharp and focused. This section covers focusing on a horizontal strip.
To build a mask that leaves out a stripe, create linear gradients from a single color, such as green or gray. Our goal is to build the mask shown in Figure 2.
The linear gradients cause the blur to taper smoothly as it approaches the focused stripe of the image. The Core Image
CILinear generates filters of the desired color. The linear gradient has four parameters:
The starting point of the gradient ramp, expressed as a
The endpoint of the gradient ramp, expressed as a
The starting point gradient color, expressed as a
The endpoint gradient color, expressed as a
Compute the start and stop points of the gradient as fractions of the image height, as obtained through
extent. For this particular mask and example image, focus on the area near the middle, in the second quarter of the image. Set the linear gradient's
input to reflect the region through which the gradient tapers.
The lower gradient should complement the upper gradient, so that their combined coverage delineates the entire area to blur. Express the starting
input y-value at
0 of the image height and taper to
0, where the top gradient began. This leaves a gap in the combined mask through which the sharp image will show.
Creating a Mask by Compositing Linear Gradients
To create a mask that dilineates where and how strong a blur to apply, combine the two linear gradients.
Since the gradients themselves are
CIFilter objects, compositing them is as simple as concatenating their filter outputs to a compositing filter. Use the built-in
CIAddition to composite two images additively.
The resulting mask is now ready to be applied as part of the
Focusing on a Circular Region
In order to focus on a circular region of an image, you can create a Core Image
CIFilter with the name
The filter takes two parameters,
CIVectorpointing to the center of the region you want to leave unblurred.
inputto a fraction of the image's dimension, like
0in this example. You can tweak this parameter to determine the size of the sharp region.
inputto a larger fraction of the image's dimension, like
0in this example. Tweaking this parameter changes the extent of the blur's tapering effect; a larger value makes the blur more gradual, whereas a smaller value makes the image transition more abruptly from sharp (at
input) to blur (at
As with the linear gradients, set
inputto transparency, and
inputto a solid opaque color, to indicate the blur's gradation from nonexistent to full.
This yields a circular mask to composite using the
Without accessing the output image, keep this filter to chain with the other components.
Masking the Blurred Image to Apply Selective Focus
The final step is applying your choice of mask with the input image. The
CIFilter accomplishes this task with the following input parameters:
Set to the original, unprocessed
10, and experiment to find the right degree of blur desired. (The default is
The resulting image shows the original image with portions blurred out according to the mask applied. The linear gradient mask results in an output image focused on a strip, and the radial gradient mask results in an output image focused on a circular region.