Edge detection & cropping Pixelnetica™ Document Scanning SDK for Apple iOS

After a page is captured or imported, you crop to the document and correct its perspective so an angled photo becomes a flat, rectangular page. The detected corners are a PxCutout; cropping applies that cutout to a PxPicture.

Ready-to-use: the crop editor

PxUiPageCropScreen (SwiftUI PxUiPageCropScreenView) shows the page with draggable corner handles and rotation, so the user can fine-tune the auto-detected edges. It takes a PxPicture and its cutout and returns the adjusted result.

import DocScanningSDK_UI

// The camera screen returns a UIImage and a cutout; wrap the image in a PxPicture.
let picture = PxPicture(image)

PxUiPageCropScreenView(picture: picture, cutout: cutout) { result in
    switch result {
    case .finished(let image, let cutout):
        // The user-adjusted page and corners.
        break
    case .cancelled:
        break
    @unknown default:
        break
    }
}

This is the natural next step after the camera screen: wrap the UIImage it returns in a PxPicture and pass that with the cutout.

Engine: crop and correct in code

With the core engine you describe the processing on a PxRefineFeatures builder, then apply it to the picture:

import DocScanningSDK

let picture = PxPicture(image)            // from capture or import

let features = PxRefineFeatures()
features.rectify(with: cutout)            // crop + perspective-correct
picture.refine(features)                  // apply the processing (resets the builder)
let corrected = picture.extractImage()    // the flattened page

PxRefineFeatures also offers rectifyAutodetectCutout() (detect the edges and crop in one call) and rectifyWithoutCutout() (process the whole image). A PxCutout exposes the corners through getPoints(_:) / setPoints(_:_:) and reports isDefined / isValid, so you can validate or adjust corners before cropping.

Each instruction method returns the builder, so you can chain steps — for example a colour profile (see Image enhancement).

Tips

  • A cutout from the camera screen is already in the captured image’s coordinate space — pass both straight to the editor or to rectify(with:).
  • Check cutout.isValid before cropping; an undefined or degenerate quad means detection did not find a confident document.

See also

Top