Skip to content

findContours

Image processingfunctionOpenCV 5.0.0
import { findContours } from '@banou/opencv-wasm'

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

Finds contours in a binary image.

The function retrieves contours from the binary image. The contours are a useful tool for shape analysis and object detection and recognition. See squares.cpp in the OpenCV sample directory.

Note: Since OpenCV 4.14, when mode is #RETR_LIST and no hierarchy is requested, this function automatically uses the TRUCO parallel algorithm [TRUCO2026], a scalable lock-free method for contour extraction. In all other cases, the sequential [Suzuki85] algorithm is used.

Note: Since opencv 3.2 source image is not modified by this function.

Note: In Python, hierarchy is nested inside a top level array. Use hierarchy[0][i] to access hierarchical elements of i-th contour.

findContours(image: Mat, contours: MatVector, hierarchy: Mat, mode: number, method: number, offset: Point): void;
2 available overloads
findContours(image: Mat, contours: MatVector, hierarchy: Mat, mode: number, method: number): void;
findContours(image: Mat, contours: MatVector, hierarchy: Mat, mode: number, method: number, offset: Point): void;
image

Source, an 8-bit single-channel image. Non-zero pixels are treated as 1's. Zero pixels remain 0's, so the image is treated as binary . You can use #compare, #inRange, #threshold , #adaptiveThreshold, #Canny, and others to create a binary image out of a grayscale or color one. If mode equals to #RETR_CCOMP or #RETR_FLOODFILL, the input can also be a 32-bit integer image of labels (CV_32SC1).

contours

Output destination, filled by the native operation. Detected contours. Each contour is stored as a vector of points (e.g. std::vector<std::vectorcv::Point >).

hierarchy

Output destination, filled by the native operation. Optional output vector (e.g. std::vectorcv::Vec4i), containing information about the image topology. It has as many elements as the number of contours. For each i-th contour contours[i], the elements hierarchy[i][0] , hierarchy[i][1] , hierarchy[i][2] , and hierarchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.

mode

Contour retrieval mode, see #RetrievalModes

method

Contour approximation method, see #ContourApproximationModes

offset

Optional offset by which every contour point is shifted. This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.

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.