Skip to content

CascadeClassifier

xobjdetectclassOpenCV 5.0.0
import { CascadeClassifier } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

ARGUMENTSConstructor or factory
CLASSCascadeClassifier
RETURN TYPEOwned native handle
Call structure. A void return can still write to destination arguments. The parameter descriptions define inputs, outputs and ownership.

Native object: release it with using or delete(). Factories can return null; check before calling methods.

example: samples/facedetect.cpp This program demonstrates usage of the Cascade classifier class \image html Cascade_Classifier_Tutorial_Result_Haar.jpg "Sample screenshot" width=321 height=254 Cascade classifier class for object detection.

Constructors and members

static new

Loads a classifier from a file.

new(filename: EmbindString): CascadeClassifier;
2 available overloads
new(): CascadeClassifier;
new(filename: EmbindString): CascadeClassifier;
filename

Name of the file from which the classifier is loaded.

Returns

The CascadeClassifier result.

static convert

Convert a legacy Haar cascade file to the newer cascade serialization format in the virtual filesystem.

convert(oldcascade: EmbindString, newcascade: EmbindString): boolean;
oldcascade

oldcascade argument (EmbindString).

newcascade

newcascade argument (EmbindString).

Returns

The boolean result.

clone

Create another handle to the same native object. This retains the object without copying its pixels or algorithm state; dispose both handles separately.

clone(): this;
Returns

The this result.

empty

Checks whether the classifier has been loaded.

empty(): boolean;
Returns

The boolean result.

load

Loads a classifier from a file.

load(filename: EmbindString): boolean;
filename

Name of the file from which the classifier is loaded. The file may contain an old HAAR classifier trained by the haartraining application or a new cascade classifier trained by the traincascade application.

Returns

The boolean result.

read

Reads a classifier from a FileStorage node.

Note: The file may contain a new cascade classifier (trained by the traincascade application) only.

read(node: FileNode): boolean;
node

node argument (FileNode).

Returns

The boolean result.

detectMultiScale

Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.

detectMultiScale(image: Mat, objects: RectVector, scaleFactor: number, minNeighbors: number, flags: number, minSize: Size, maxSize: Size): void;
6 available overloads
detectMultiScale(image: Mat, objects: RectVector): void;
detectMultiScale(image: Mat, objects: RectVector, scaleFactor: number): void;
detectMultiScale(image: Mat, objects: RectVector, scaleFactor: number, minNeighbors: number): void;
detectMultiScale(image: Mat, objects: RectVector, scaleFactor: number, minNeighbors: number, flags: number): void;
detectMultiScale(image: Mat, objects: RectVector, scaleFactor: number, minNeighbors: number, flags: number, minSize: Size): void;
detectMultiScale(image: Mat, objects: RectVector, scaleFactor: number, minNeighbors: number, flags: number, minSize: Size, maxSize: Size): void;
image

Matrix of the type CV_8U containing an image where objects are detected.

objects

Output destination, filled by the native operation. Vector of rectangles where each rectangle contains the detected object, the rectangles may be partially outside the original image.

scaleFactor

Parameter specifying how much the image size is reduced at each image scale.

minNeighbors

Parameter specifying how many neighbors each candidate rectangle should have to retain it.

flags

Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.

minSize

Minimum possible object size. Objects smaller than that are ignored.

maxSize

Maximum possible object size. Objects larger than that are ignored. If maxSize == minSize model is evaluated on single scale.

detectMultiScale2

Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.

detectMultiScale2(image: Mat, objects: RectVector, numDetections: IntVector, scaleFactor: number, minNeighbors: number, flags: number, minSize: Size, maxSize: Size): void;
6 available overloads
detectMultiScale2(image: Mat, objects: RectVector, numDetections: IntVector): void;
detectMultiScale2(image: Mat, objects: RectVector, numDetections: IntVector, scaleFactor: number): void;
detectMultiScale2(image: Mat, objects: RectVector, numDetections: IntVector, scaleFactor: number, minNeighbors: number): void;
detectMultiScale2(image: Mat, objects: RectVector, numDetections: IntVector, scaleFactor: number, minNeighbors: number, flags: number): void;
detectMultiScale2(image: Mat, objects: RectVector, numDetections: IntVector, scaleFactor: number, minNeighbors: number, flags: number, minSize: Size): void;
detectMultiScale2(image: Mat, objects: RectVector, numDetections: IntVector, scaleFactor: number, minNeighbors: number, flags: number, minSize: Size, maxSize: Size): void;
image

Matrix of the type CV_8U containing an image where objects are detected.

objects

Output destination, filled by the native operation. Vector of rectangles where each rectangle contains the detected object, the rectangles may be partially outside the original image.

numDetections

Output destination, filled by the native operation. Vector of detection numbers for the corresponding objects. An object's number of detections is the number of neighboring positively classified rectangles that were joined together to form the object.

scaleFactor

Parameter specifying how much the image size is reduced at each image scale.

minNeighbors

Parameter specifying how many neighbors each candidate rectangle should have to retain it.

flags

Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.

minSize

Minimum possible object size. Objects smaller than that are ignored.

maxSize

Maximum possible object size. Objects larger than that are ignored. If maxSize == minSize model is evaluated on single scale.

detectMultiScale3

This function allows you to retrieve the final stage decision certainty of classification. For this, one needs to set outputRejectLevels on true and provide the rejectLevels and levelWeights parameter. For each resulting detection, levelWeights will then contain the certainty of classification at the final stage. This value can then be used to separate strong from weaker classifications.

A code sample on how to use it efficiently can be found below:
    Mat img;
    vector<double> weights;
    vector<int> levels;
    vector<Rect> detections;
    CascadeClassifier model("/path/to/your/model.xml");
    model.detectMultiScale(img, detections, levels, weights, 1.1, 3, 0, Size(), Size(), true);
    cerr << "Detection " << detections[0] << " with weight " << weights[0] << endl;
    
detectMultiScale3(image: Mat, objects: RectVector, rejectLevels: IntVector, levelWeights: DoubleVector, scaleFactor: number, minNeighbors: number, flags: number, minSize: Size, maxSize: Size, outputRejectLevels: boolean): void;
7 available overloads
detectMultiScale3(image: Mat, objects: RectVector, rejectLevels: IntVector, levelWeights: DoubleVector): void;
detectMultiScale3(image: Mat, objects: RectVector, rejectLevels: IntVector, levelWeights: DoubleVector, scaleFactor: number): void;
detectMultiScale3(image: Mat, objects: RectVector, rejectLevels: IntVector, levelWeights: DoubleVector, scaleFactor: number, minNeighbors: number): void;
detectMultiScale3(image: Mat, objects: RectVector, rejectLevels: IntVector, levelWeights: DoubleVector, scaleFactor: number, minNeighbors: number, flags: number): void;
detectMultiScale3(image: Mat, objects: RectVector, rejectLevels: IntVector, levelWeights: DoubleVector, scaleFactor: number, minNeighbors: number, flags: number, minSize: Size): void;
detectMultiScale3(image: Mat, objects: RectVector, rejectLevels: IntVector, levelWeights: DoubleVector, scaleFactor: number, minNeighbors: number, flags: number, minSize: Size, maxSize: Size): void;
detectMultiScale3(image: Mat, objects: RectVector, rejectLevels: IntVector, levelWeights: DoubleVector, scaleFactor: number, minNeighbors: number, flags: number, minSize: Size, maxSize: Size, outputRejectLevels: boolean): void;
image

image argument (Mat).

objects

Output destination, filled by the native operation. objects argument (RectVector).

rejectLevels

Output destination, filled by the native operation. reject levels argument (IntVector).

levelWeights

Output destination, filled by the native operation. level weights argument (DoubleVector).

scaleFactor

scale factor argument (number).

minNeighbors

min neighbors argument (number).

flags

flags argument (number).

minSize

min size argument (Size).

maxSize

max size argument (Size).

outputRejectLevels

output reject levels argument (boolean).

isOldFormatCascade

Return whether the loaded classifier uses the legacy Haar cascade format.

isOldFormatCascade(): boolean;
Returns

The boolean result.

getOriginalWindowSize

Return the original window size configured on this CascadeClassifier object.

getOriginalWindowSize(): Size;
Returns

The Size result.

getFeatureType

Return the feature type configured on this CascadeClassifier object.

getFeatureType(): number;
Returns

The number result.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.