Contents

perspectiveCorrection()

Transforms an image’s perspective.

Declaration

class func perspectiveCorrection() -> any CIFilter & CIPerspectiveCorrection

Return Value

The adjusted image.

Discussion

This method applies the perspective correction filter to an image. The effect applies a perspective correction transforming nonrectangular area in the source image to a rectangular output image.

The perspective correction filter uses the following properties:

inputImage

An image with the type CIImage.

topLeft

A CGPoint in the input image mapped to the top-left corner of the output image.

topRight

A CGPoint in the input image mapped to the top-right corner of the output image.

bottomLeft

A CGPoint in the input image mapped to the bottom-left corner of the output image.

bottomRight

A CGPoint in the input image mapped to the bottom-right corner of the output image.

The following code creates a filter that corrects the perspective to appear straight:

func perspectiveCorrection(inputImage: CIImage) -> CIImage {
    let perspectiveCorrectionFilter = CIFilter.perspectiveCorrection()
    perspectiveCorrectionFilter.inputImage = inputImage
    perspectiveCorrectionFilter.topRight = CGPoint(x: 0, y: 3024)
    perspectiveCorrectionFilter.topLeft = CGPoint(x: 4032, y: 3024)
    perspectiveCorrectionFilter.bottomRight = CGPoint(x: 200, y: 0)
    perspectiveCorrectionFilter.bottomLeft = CGPoint(x: 4032, y: 0)
    return perspectiveCorrectionFilter.outputImage!
}

[Image]

See Also

Filters