Simd Library Documentation.

Home | Release Notes | Download | Documentation | Issues | GitHub
ContourDetector< A > Struct Template Reference

ContourDetector structure extracts polyline contours from 8-bit gray images. More...

#include <SimdContour.hpp>

Public Types

typedef A< uint8_t > Allocator
 
typedef Simd::View< A > View
 
typedef Simd::Point< ptrdiff_t > Size
 
typedef Simd::Point< ptrdiff_t > Point
 
typedef Rectangle< ptrdiff_t > Rect
 
typedef std::vector< PointContour
 
typedef std::vector< ContourContours
 

Public Member Functions

void Init (Size size)
 
bool Detect (const View &src, Contours &contours, const View &mask=View(), uint8_t indexMin=3, const Rect &roi=Rect(), int gradientThreshold=40, int anchorThreshold=0, int anchorScanInterval=2, int minSegmentLength=2)
 

Detailed Description

template<template< class > class A>
struct Simd::ContourDetector< A >

ContourDetector structure extracts polyline contours from 8-bit gray images.

The structure is a C++ helper for contour extraction. It calculates packed Sobel metrics, optionally filters them by a mask and a rectangular region of interest, selects contour anchors, and routes connected edge points into contours. Call Init() once for the required image size, then call Detect() for one or more images of the same size.

Each contour is an ordered vector of image points. Adjacent points can be connected with line segments, as shown in the example below.

Using example:

#include "Simd/SimdContour.hpp"
#include "Simd/SimdDrawing.hpp"

int main()
{
    typedef Simd::ContourDetector<Simd::Allocator> ContourDetector;

    ContourDetector::View image;
    image.Load("../../data/image/face/lena.pgm");

    ContourDetector contourDetector;

    contourDetector.Init(image.Size());

    ContourDetector::Contours contours;
    contourDetector.Detect(image, contours);

    for (size_t i = 0; i < contours.size(); ++i)
    {
        for (size_t j = 1; j < contours[i].size(); ++j)
            Simd::DrawLine(image, contours[i][j - 1], contours[i][j], uint8_t(255));
    }
    image.Save("result.pgm");

    return 0;
}

Member Typedef Documentation

◆ Allocator

typedef A<uint8_t> Allocator

Allocator type used by internal and external images.

◆ View

typedef Simd::View<A> View

Image type used by the detector. Input and mask images for Detect() must be View::Gray8.

◆ Size

typedef Simd::Point<ptrdiff_t> Size

Image size type.

◆ Point

typedef Simd::Point<ptrdiff_t> Point

Image point type.

◆ Rect

typedef Rectangle<ptrdiff_t> Rect

Rectangle type used to specify a region of interest.

◆ Contour

typedef std::vector<Point> Contour

Ordered chain of contour points in source image coordinates.

◆ Contours

typedef std::vector<Contour> Contours

Vector of detected contours.

Member Function Documentation

◆ Init()

void Init ( Size  size)

Allocates internal buffers and prepares ContourDetector to process Gray8 images of the given size.

The same initialized detector can be reused for several images with the same size.

Parameters
[in]size- a size of input images.

◆ Detect()

bool Detect ( const View src,
Contours contours,
const View mask = View(),
uint8_t  indexMin = 3,
const Rect roi = Rect(),
int  gradientThreshold = 40,
int  anchorThreshold = 0,
int  anchorScanInterval = 2,
int  minSegmentLength = 2 
)

Detects contours in the given Gray8 image.

The source image must have the size passed to Init(). If a mask is specified, it must also be a Gray8 image of this size. Detected contours are appended to contours, so clear this vector before the call if previous results must be discarded.

Parameters
[in]src- a Gray8 input image.
[out]contours- a vector to which detected contours are appended.
[in]mask- an optional Gray8 mask. If it is used, only pixels with mask value not less than indexMin can start or continue contours.
[in]indexMin- a minimal mask value that enables contour detection. By default it is equal to 3.
[in]roi- an optional Region Of Interest. If it is empty, the whole image is processed. Non-empty ROI is clipped by the image rectangle.
[in]gradientThreshold- a minimal gradient magnitude used during contour routing. If this parameter is negative, it is estimated automatically from non-zero metrics in ROI. By default it is equal to 40.
[in]anchorThreshold- a minimal metric difference required to select an anchor point. By default it is equal to 0.
[in]anchorScanInterval- a row and column step used to search anchor points. Larger values skip more candidates and can improve performance. It must be positive. By default it is equal to 2.
[in]minSegmentLength- a minimal accepted contour length in points. A contour is saved only when it contains more than this number of points. By default it is equal to 2.
Returns
true if input images are compatible with the initialized detector; otherwise false.