Skip to content

hfs_HfsSegment

hfsclassOpenCV 5.0.0
import { hfs_HfsSegment } from '@banou/opencv-wasm'

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

ARGUMENTSConstructor or factory
CLASShfs_HfsSegment
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. Inherits Algorithm.

The opencv hfs module contains an efficient algorithm to segment an image. This module is implemented based on the paper Hierarchical Feature Selection for Efficient Image Segmentation, ECCV 2016. The original project was developed by Yun Liu(https://github.com/yun-liu/hfs).

Introduction to Hierarchical Feature Selection

This algorithm is executed in 3 stages:

In the first stage, the algorithm uses SLIC (simple linear iterative clustering) algorithm to obtain the superpixel of the input image.

In the second stage, the algorithm view each superpixel as a node in the graph. It will calculate a feature vector for each edge of the graph. It then calculates a weight for each edge based on the feature vector and trained SVM parameters. After obtaining weight for each edge, it will exploit EGB (Efficient Graph-based Image Segmentation) algorithm to merge some nodes in the graph thus obtaining a coarser segmentation After these operations, a post process will be executed to merge regions that are smaller then a specific number of pixels into their nearby region.

In the third stage, the algorithm exploits the similar mechanism to further merge the small regions obtained in the second stage into even coarser segmentation.

After these three stages, we can obtain the final segmentation of the image. For further details about the algorithm, please refer to the original paper: Hierarchical Feature Selection for Efficient Image Segmentation, ECCV 2016

Constructors and members

static create

: create a hfs object param: height: the height of the input image param: width: the width of the input image param: segEgbThresholdI: parameter segEgbThresholdI param: minRegionSizeI: parameter minRegionSizeI param: segEgbThresholdII: parameter segEgbThresholdII param: minRegionSizeII: parameter minRegionSizeII param: spatialWeight: parameter spatialWeight param: slicSpixelSize: parameter slicSpixelSize param: numSlicIter: parameter numSlicIter

create(height: number, width: number, segEgbThresholdI: number, minRegionSizeI: number, segEgbThresholdII: number, minRegionSizeII: number, spatialWeight: number, slicSpixelSize: number, numSlicIter: number): hfs_HfsSegment | null;
8 available overloads
create(height: number, width: number): hfs_HfsSegment | null;
create(height: number, width: number, segEgbThresholdI: number): hfs_HfsSegment | null;
create(height: number, width: number, segEgbThresholdI: number, minRegionSizeI: number): hfs_HfsSegment | null;
create(height: number, width: number, segEgbThresholdI: number, minRegionSizeI: number, segEgbThresholdII: number): hfs_HfsSegment | null;
create(height: number, width: number, segEgbThresholdI: number, minRegionSizeI: number, segEgbThresholdII: number, minRegionSizeII: number): hfs_HfsSegment | null;
create(height: number, width: number, segEgbThresholdI: number, minRegionSizeI: number, segEgbThresholdII: number, minRegionSizeII: number, spatialWeight: number): hfs_HfsSegment | null;
create(height: number, width: number, segEgbThresholdI: number, minRegionSizeI: number, segEgbThresholdII: number, minRegionSizeII: number, spatialWeight: number, slicSpixelSize: number): hfs_HfsSegment | null;
create(height: number, width: number, segEgbThresholdI: number, minRegionSizeI: number, segEgbThresholdII: number, minRegionSizeII: number, spatialWeight: number, slicSpixelSize: number, numSlicIter: number): hfs_HfsSegment | null;
height

height argument (number).

width

width argument (number).

segEgbThresholdI

seg egb threshold i argument (number).

minRegionSizeI

min region size i argument (number).

segEgbThresholdII

seg egb threshold ii argument (number).

minRegionSizeII

min region size ii argument (number).

spatialWeight

spatial weight argument (number).

slicSpixelSize

slic spixel size argument (number).

numSlicIter

num slic iter argument (number).

Returns

The hfs_HfsSegment | null 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.

setSegEgbThresholdI

: set and get the parameter segEgbThresholdI. This parameter is used in the second stage mentioned above. It is a constant used to threshold weights of the edge when merging adjacent nodes when applying EGB algorithm. The segmentation result tends to have more regions remained if this value is large and vice versa.

setSegEgbThresholdI(c: number): void;
c

c argument (number).

getSegEgbThresholdI

Read the seg egb threshold i.

: set and get the parameter segEgbThresholdI. This parameter is used in the second stage mentioned above. It is a constant used to threshold weights of the edge when merging adjacent nodes when applying EGB algorithm. The segmentation result tends to have more regions remained if this value is large and vice versa.

getSegEgbThresholdI(): number;
Returns

The number result.

setMinRegionSizeI

: set and get the parameter minRegionSizeI. This parameter is used in the second stage mentioned above. After the EGB segmentation, regions that have fewer pixels then this parameter will be merged into it's adjacent region.

setMinRegionSizeI(n: number): void;
n

n argument (number).

getMinRegionSizeI

Read the min region size i.

: set and get the parameter minRegionSizeI. This parameter is used in the second stage mentioned above. After the EGB segmentation, regions that have fewer pixels then this parameter will be merged into it's adjacent region.

getMinRegionSizeI(): number;
Returns

The number result.

setSegEgbThresholdII

: set and get the parameter segEgbThresholdII. This parameter is used in the third stage mentioned above. It serves the same purpose as segEgbThresholdI. The segmentation result tends to have more regions remained if this value is large and vice versa.

setSegEgbThresholdII(c: number): void;
c

c argument (number).

getSegEgbThresholdII

Read the seg egb threshold ii.

: set and get the parameter segEgbThresholdII. This parameter is used in the third stage mentioned above. It serves the same purpose as segEgbThresholdI. The segmentation result tends to have more regions remained if this value is large and vice versa.

getSegEgbThresholdII(): number;
Returns

The number result.

setMinRegionSizeII

: set and get the parameter minRegionSizeII. This parameter is used in the third stage mentioned above. It serves the same purpose as minRegionSizeI

setMinRegionSizeII(n: number): void;
n

n argument (number).

getMinRegionSizeII

Read the min region size ii.

: set and get the parameter minRegionSizeII. This parameter is used in the third stage mentioned above. It serves the same purpose as minRegionSizeI

getMinRegionSizeII(): number;
Returns

The number result.

setSpatialWeight

: set and get the parameter spatialWeight. This parameter is used in the first stage mentioned above(the SLIC stage). It describes how important is the role of position when calculating the distance between each pixel and it's center. The exact formula to calculate the distance is colorDistance + spatialWeight \times spatialDistance. The segmentation result tends to have more local consistency if this value is larger.

setSpatialWeight(w: number): void;
w

w argument (number).

getSpatialWeight

Read the spatial weight.

: set and get the parameter spatialWeight. This parameter is used in the first stage mentioned above(the SLIC stage). It describes how important is the role of position when calculating the distance between each pixel and it's center. The exact formula to calculate the distance is colorDistance + spatialWeight \times spatialDistance. The segmentation result tends to have more local consistency if this value is larger.

getSpatialWeight(): number;
Returns

The number result.

setSlicSpixelSize

: set and get the parameter slicSpixelSize. This parameter is used in the first stage mentioned above(the SLIC stage). It describes the size of each superpixel when initializing SLIC. Every superpixel approximately has slicSpixelSize \times slicSpixelSize pixels in the beginning.

setSlicSpixelSize(n: number): void;
n

n argument (number).

getSlicSpixelSize

Read the slic spixel size.

: set and get the parameter slicSpixelSize. This parameter is used in the first stage mentioned above(the SLIC stage). It describes the size of each superpixel when initializing SLIC. Every superpixel approximately has slicSpixelSize \times slicSpixelSize pixels in the beginning.

getSlicSpixelSize(): number;
Returns

The number result.

setNumSlicIter

: set and get the parameter numSlicIter. This parameter is used in the first stage. It describes how many iteration to perform when executing SLIC.

setNumSlicIter(n: number): void;
n

n argument (number).

getNumSlicIter

Read the num slic iter.

: set and get the parameter numSlicIter. This parameter is used in the first stage. It describes how many iteration to perform when executing SLIC.

getNumSlicIter(): number;
Returns

The number result.

performSegmentGpu

do segmentation gpu param: src: the input image param: ifDraw: if draw the image in the returned Mat. if this parameter is false, then the content of the returned Mat is a matrix of index, describing the region each pixel belongs to. And it's data type is CV_16U. If this parameter is true, then the returned Mat is a segmented picture, and color of each region is the average color of all pixels in that region. And it's data type is the same as the input image

performSegmentGpu(src: Mat, ifDraw: boolean): Mat;
2 available overloads
performSegmentGpu(src: Mat): Mat;
performSegmentGpu(src: Mat, ifDraw: boolean): Mat;
src

src argument (Mat).

ifDraw

if draw argument (boolean).

Returns

The Mat result. Release returned native handles with using or delete(), including handles nested in results.

performSegmentCpu

do segmentation with cpu This method is only implemented for reference. It is highly NOT recommanded to use it.

performSegmentCpu(src: Mat, ifDraw: boolean): Mat;
2 available overloads
performSegmentCpu(src: Mat): Mat;
performSegmentCpu(src: Mat, ifDraw: boolean): Mat;
src

src argument (Mat).

ifDraw

if draw argument (boolean).

Returns

The Mat result. Release returned native handles with using or delete(), including handles nested in results.

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.