SDKs

- iOS 13.0+Beta
- Xcode 11.0+Beta

Framework

- Accelerate

## Overview

Accelerate’s vDSP module provides functions to perform 2D fast Fourier transforms (FFTs) on matrices of data, such as images. You can exploit the amplitude peaks in the frequency domain of periodic patterns, such as halftone screens, to reduce or remove such artifacts from images. The example below shows an image with halftone artifacts (left) and the same image with the halftone artifacts reduced (right):

Using a halftone screen sample that you’ve created programmatically or taken from source material, you’ll follow these steps for halftone descreening of an image:

Converting the image data to a split complex vector

Preparing the FFT setup

Performing forward 2D FFTs on image data

Zeroing the peaks in the halftone sample magnitude

Descreening the source image

Performing inverse 2D FFT on the source image frequency domain data

Generating an image from a split complex vector

The sample works on a single-channel, monochrome image.

### Convert Image Data to a Split Complex Vector

Create a split complex vector—suitable for use with vDSP’s 2D FFT—by copying odd pixels to the real parts and even pixels to the imaginary parts of an array of complex numbers. Use the following code to convert the 8-bit unsigned integer image data to floating-point data that the 2D FFT routine works with:

With the floating-point values populated, create an array of complex numbers, `interleaved`

, that you pass to `convert(interleaved`

, which copies the values into a split complex vector:

### Prepare the 2D FFT Setup

Create a setup object that contains all the information required to perform the forward and inverse 2D FFT operations. Creating this setup object can be expensive, so do it only once—for example, when your app is starting—and reuse it.

The following code creates a setup object suitable for performing forward and inverse 2D FFTs on a 1024 x 1024 pixel image:

### Perform Forward 2D FFTs on Image Data

Use the `transform(input:`

function to perform a forward 2D FFT on the image data, creating the frequency domain representation of the image. Pass `transform(input:`

a split complex structure as the destination, with the same length as the source structure.

The following example shows the code required to perform the FFT on the source image data that populates `source`

. Repeat this step for the halftone sample image, populating `halftone`

.

### Zero the Peaks in the Halftone Sample Magnitude

You can reduce the halftone screen artifacts by manipulating the magnitude of the frequency domain data for the halftone sample. Zero all the samples above a specified threshold in the magnitudes and clamp the data to 0…1. Then multiply the frequency domain data of the source image by the manipulated magnitudes.

The `square`

function computes the magnitude of the complex values representing the halftone sample:

Use the `threshold(_:`

function to set all magnitude values that are over the threshold to -1, and all magnitude values that are less than or equal to the threshold to 1:

You can now clip the magnitude data between 0 and 1. After `clip(_:`

returns, all the originally high-magnitude values in `halftone`

are set to 0, and all the originally low-magnitude values are set to 1:

### Descreen the Source Image

Multiply the source image frequency domain data by the values in `halftone`

to remove or reduce the halftone screen:

### Perform Inverse 2D FFT on Source Image Frequency Domain Data

You can now perform an inverse 2D FFT to generate a spatial domain version of the image. Use the same `fft`

pointer as you used for the forward 2D FFT, but specify the inverse direction:

### Generate an Image from a Split Complex Vector

The last step is to create a displayable image from the spatial domain representation of the treated source image. The final image is generated from 8-bit, unsigned integers. Because single-precision values can exceed the range of an 8-bit, unsigned integer, clamp the values before storing them:

vDSP’s conversion functions allow you to convert the clamped floating-point values to 8-bit, unsigned integers. To prepare the split complex vector for conversion, copy its contents to an array of interleaved complex numbers:

Use `convert`

to convert the floating-point values back to 8-bit unsigned integers suitable for generating an image:

Now that `u`

is populated with the pixel values, generate a `CGImage`

instance from those values: