Skip to main content

Filtering by Detection Area

CropDetectionFilter lets you restrict detection to specific areas of the image. You can specify multiple detection areas for text and barcodes separately, and use getCropIndex() to identify which area each detection belongs to.

Difference from CropRect

CropRect is set in ScanOptions and specifies a single area to crop from the input image. CropDetectionFilter, on the other hand, is a post-detection filter set in ModelSettings. It lets you specify multiple areas separately for text and barcodes and classify detection results by area.

How It Works​

  1. The OCR engine detects text and barcodes across the entire image.
  2. Each detection is checked for overlap with the specified areas.
  3. Overlapping detections are returned clipped to the area boundary.
  4. Each detection is assigned a cropIndex (the index of its area).
note

If textCrops / barcodeCrops is empty, detections of that type are not filtered and are all passed through.

CropDetectionFilter Parameters​

ParameterType (iOS / Android)DefaultDescription
textCrops[CGRect] / List<RectF>[]List of detection areas for text
barcodeCrops[CGRect] / List<RectF>[]List of detection areas for barcodes
combineHorizontalBool / booleanfalseWhether to combine horizontally adjacent text detections within each area (text only)
combineHorizontalRatio / combineHorizontalThresholdDouble / float0.9Threshold for combining (vertical IoU)

Each area is specified in normalized coordinates (0.0–1.0). The coordinate system is the same as getBoundingBox() of Detection, with the origin at the top left.

Coordinate differences between iOS and Android

On iOS, CGRect is specified as (x, y, width, height), whereas on Android, RectF is specified as (left, top, right, bottom). Note that the notation differs even when representing the same area.

Creating and Setting the Filter​

Create a CropDetectionFilter, set it in ModelSettings, and pass it to useModel.

// Specify two detection areas for text
let textCrops = [
CGRect(x: 0.1, y: 0.1, width: 0.8, height: 0.2), // Upper area
CGRect(x: 0.1, y: 0.6, width: 0.8, height: 0.2), // Lower area
]

let cropDetectionFilter = CropDetectionFilter(
textCrops: textCrops,
barcodeCrops: []
)

let modelSettings = ModelSettings()
modelSettings.cropDetectionFilter = cropDetectionFilter

Getting the Area Index of a Detection​

Use the getCropIndex() method of Detection to get which detection area a detection belongs to. Indices are assigned from 0 independently for textCrops and barcodeCrops. If no filter is set, -1 is returned.

let scanResult = try edgeOCR.scan(
sampleBuffer,
scanOption: scanOptions,
viewBounds: viewBounds)

for detection in scanResult.getTextDetections() {
let text = detection.getText()
let cropIndex = detection.getCropIndex()
// cropIndex == 0: detected in the upper area
// cropIndex == 1: detected in the lower area
print("Text: \(text), Area: \(cropIndex)")
}

Per-Area Settings with CropSpec​

With CropSpec, you can configure individually for each area whether to return multiple detections.

CropSpec fieldType (iOS / Android)Description
rectCGRect / RectFDetection area
returnMultipleBool / booleantrue: return all detections in the area / false: return only the one with the largest area

When you use the simple constructor (textCrops: [CGRect] / List<RectF>), returnMultiple is automatically set to false.

let textCropSpecs = [
// Upper area: return multiple text detections
CropDetectionFilter.CropSpec(
rect: CGRect(x: 0.1, y: 0.1, width: 0.8, height: 0.2),
returnMultiple: true
),
// Lower area: return only the one with the largest area
CropDetectionFilter.CropSpec(
rect: CGRect(x: 0.1, y: 0.6, width: 0.8, height: 0.2),
returnMultiple: false
),
]

let cropDetectionFilter = CropDetectionFilter(
textCropSpecs: textCropSpecs,
barcodeCropSpecs: []
)

Horizontal Combining​

When combineHorizontal is set to true, horizontally adjacent text detections within each detection area are combined into one. This feature applies only to text detection, not to barcode detection.

Combining is determined using the one-dimensional vertical IoU (Intersection over Union). When the vertical IoU of two detections exceeds the threshold, their bounding boxes are merged.

let cropDetectionFilter = CropDetectionFilter(
textCrops: textCrops,
barcodeCrops: [],
combineHorizontal: true,
combineHorizontalRatio: 0.5
)