ppu-ocv
Version:
A type-safe, modular, chainable image processing library built on top of OpenCV.js with a fluent API leveraging pipeline processing.
86 lines (85 loc) • 3.45 kB
TypeScript
import type { BoundingBox, Canvas, Points } from "./index";
import { cv } from "./index";
export interface ContoursOptions {
/** The contour retrieval mode. (cv.RETR_...) */
mode: cv.RetrievalModes;
/** The contour approximation method. (cv.CHAIN_...) */
method: cv.ContourApproximationModes;
}
export declare class Contours {
private contours;
/** The constructor for the Contours class. It takes an image and options as parameters. */
/**
* @param img - The image to find contours in.
* @param options.mode - The contour retrieval mode. (cv.RETR_...)
* @param options.method - The contour approximation method. (cv.CHAIN_...)
* @example
* const contours = new Contours(image, {
* mode: cv.RETR_EXTERNAL,
* method: cv.CHAIN_APPROX_SIMPLE,
* });
*/
constructor(img: cv.Mat, options?: Partial<ContoursOptions>);
/**
* Get the all of contours found in the image.
* @returns The number of contours found in the image (cv.MatVector).
*/
getAll(): cv.MatVector;
/**
* Get contour at a specific index.
* @param index - The index of the contour to get.
* @returns The contour at the specified index (cv.Mat).
*/
getFromIndex(index: number): cv.Mat;
/**
* Get the rectangle that bounds the contour.
* @param contour - The contour to get the bounding rectangle for.
* @returns The bounding rectangle for the contour (cv.Rect).
*/
getRect(contour: cv.Mat): cv.Rect;
/**
* Iterate over all contours and call the callback function for each contour.
* @param callback - The callback function to call for each contour.
* The callback function takes a contour as a parameter.
* @returns void
*/
iterate(callback: (contour: cv.Mat) => any): Contours;
/**
* Get the largest contour area.
* @returns The largest contour area (cv.Mat).
*/
getLargestContourArea(): cv.Mat | null;
/**
* Get four corner points for a given contour.
* Useful for perspective transformation (warp).
* @param options.canvas - The canvas to get the corner points for.
* @param options.contour - The contour to get the corner points for. If not provided, the largest contour area will be used.
* @returns The four corner points of the contour (topLeft, topRight, bottomLeft, bottomRight) and the bounding box.
*/
getCornerPoints(options: {
canvas: Canvas;
contour?: cv.Mat;
}): {
points: Points;
bbox: BoundingBox;
};
/**
* Approximates a rectangular contour from a given contour using the Douglas-Peucker algorithm.
*
* This method simplifies the contour by reducing the number of points,
* which is useful for detecting rectangle-like shapes.
*
* @param options.threshold - Approximation accuracy as a factor of arc length.
* A lower value results in a more accurate approximation with more points. (default: 0.02)
* @param options.contour - Optional input contour. If not provided, the largest contour area will be used.
* @returns The approximated contour as a `cv.Mat`. Returns an empty `cv.Mat` if no contour is available.
*/
getApproximateRectangleContour(options?: {
threshold?: number;
contour?: cv.Mat;
}): cv.Mat | undefined;
/**
* Delete the contours object.
*/
destroy(): void;
}