SDKs

- iOS 13.0+
- Xcode 11.2+

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):

This sample walks you through the following steps to implement halftone descreening, using a halftone screen sample that you’ve created programmatically or taken from source material:

Convert the image data to a split complex vector.

Prepare the FFT setup.

Perform forward 2D FFT on image data.

Zero the peaks in the halftone sample magnitude.

Descreen the source image.

Perform inverse 2D FFT on the source image frequency domain data.

Generate 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 with which the 2D FFT routine works:

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:

### Prepare Arrays for Transformed Image Data

Create arrays to receive the forward transformed source image and halftone sample:

### 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 and halftone sample data. After the forward FFT of the halftone sample is is complete, use `square`

function to compute the magnitudes of the complex values representing the halftone sample:

### 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.

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 high-magnitude values in `halftone`

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

### Descreen the Source Image

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

, to remove or reduce the halftone screen:

### Perform Inverse 2D FFT on 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

Your 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 converting them:

Use `convert`

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

Now that `u`

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

instance from those values: