Limit the effect of vImage operations to rectangular regions of interest.
You can apply vImage operations—for example, blurs and color transforms—to specified rectangular areas in an image, commonly referred to as regions of interest (ROI). Limiting the effect of an operation is useful when, for example, you want to overlay user interface elements on top of a blurred part of an image to make them stand out.
This article walks you through three approaches to apply operations to an ROI:
Apply an in-place operation to an ROI.
Apply an out-of-place operation to an ROI.
Crop and apply an out-of-place operation to an ROI.
The following image is an example of the effects possible when you use the techniques in this article. The image shows a single photograph with a landscape format ROI that's been blurred, and a portrait format ROI that's been desaturated.
The functions described in this article are all implemented as extensions to the
v structure. This means it's extremely simple to use them; the image above was created with just two function calls:
Apply an In-Place Operation to an ROI
For vImage routines that operate in-place (that is, the operation mutates the source buffer's contents), create a mutating function that applies that routine to an ROI. The following code is the function header for a desaturation function based around
Next, check that the supplied ROI is within the bounds of the buffer:
Calculate the first pixel in the source buffer for the ROI. To find this pixel, multiply the
y origin of the ROI by the source buffer's
row, added to the
x origin of the ROI multiplied by the number of bytes that describe each pixel:
Use this start value to define a second
v structure, that references the source buffer's data (that is, the image data is shared between both buffers and not copied) with a size that equals the ROI:
desaturation is now a reference to the data in the source buffer defined by the supplied ROI. Calling
desaturation as the source and destination performs the matrix multiplication on the pixels in the ROI:
To learn more about using matrix multiplication to convert color images to grayscale, see Converting Color Images to Grayscale.
The following shows the result of desaturating an ROI:
Apply an Out-of-Place Operation to an ROI
For vImage routines that don't operate in-place, create a non-mutating function that applies that routine to an ROI and returns a new
v structure that contains the result.
The following code is the function header for a blurring function based around
Perform the same check as Apply an In-Place Operation to an ROI on the ROI size:
v returns a buffer that is the same size as the source, and all pixels outside of the ROI will equal the respective pixels in the source. Create the buffer that the function returns, and copy the source pixels into the new buffer:
Using the same approach as Apply an In-Place Operation to an ROI, calculate the start of the ROI, and create a buffer for the blur operation that references the copied pixels in
The final step is to apply the blur using
blur and return the destination buffer:
The following shows the result of blurring an ROI:
Crop and Apply an Out-of-Place Operation to an ROI
When you need to apply an operation and crop to an ROI, create a simplified version of the function described in Apply an Out-of-Place Operation to an ROI.
The following code is the function header and ROI size check for a function that blurs and crops to a specified ROI:
You will return a buffer the same size as the ROI, so create a destination buffer using the ROI's dimensions:
In this blur and crop function, you don't need to calculate the start pixel. Instead, set the
src parameters of
v to the ROI's origin:
The following shows the result of blurring and cropping an ROI: