Swift/Metal Image Processing

Yuya Horita
3 min readMar 13, 2019

--

1. Edge Detection

Edge detection is one of spatial filtering.

Edge is the outside limit of an object. At edges, the image’s luminance changes drastically.

Edge detection is to calculate a position luminance changes drastically in the image.

How to calculate the position? Use differential. In continuous domain, the image’s derivatives is

derivatives in continuous domain

where f(x, y) is input image, same as I(i, j) in the previous section. Current calculation will be performed in discrete domain so approximation is needed.

(x, y) means each pixel’s index, gid in metal. So these values’ delta is enough small. The following approximation can be used.

approximated derivatives in discrete domain

This is just a subtraction!! simple.

Think again the following case.

Target pixel is I(2, 2). Then the deltas of both sides of target pixel are

  • Horizontal: I(2, 3) - I(2, 1)
  • Vertical: I(3, 2) - I(1, 2)

The horizontal and vertical kernels become

Try this filter.

SwiftImageProcessor/Shader/Edge/derivatives.metal is prepared as following.

The double for-loops is convolution. The latter part is color conversion.

This generate landscape_derivatives.jpg .

landscape_derivatives.jpg

Could get edges.

Some special filters are defined depending on kernel and prepared in my project.

Prewitt Filter

Sobel Filter

Laplace

Laplace filter is different from Prewitt and Sobel in using second derivatives.

Derivatives is approximated by delta in image processing so second derivatives is

In mathematics, laplacian is defined as following equation

This equation is important, but not much as the below kernel matrix here.

laplace kernel

SwiftImageProcessor/Shader/Edge/laplace.metal is prepared. So try this.

landscape_laplace.jpg

Other Articles

--

--

Yuya Horita

Master of Nuclear Physics, CyberAgent, Inc. FRESH LIVE. M3. Software Engineer. Twitter: https://twitter.com/horita_yuya ,GitHub: https://github.com/horita-yuya