tbp.monty.frameworks.models.salience.strategies.vocus2#

class ColorChannelSalience(center_sigma: float = 2.0, surround_sigma: float = 3.0, n_scales: int = 2, max_octaves: int | None = None, combine: PyramidCombine = <function pyramid_combine_mean>, collapse: PyramidCollapse = <function pyramid_collapse_mean>, operating_limits: OperatingLimits | None = None)[source]#

Bases: object

__init__(center_sigma: float = 2.0, surround_sigma: float = 3.0, n_scales: int = 2, max_octaves: int | None = None, combine: PyramidCombine = <function pyramid_combine_mean>, collapse: PyramidCollapse = <function pyramid_collapse_mean>, operating_limits: OperatingLimits | None = None)[source]#

Create a ColorChannelSalience with safe operating limits.

ColorChannelSalience was designed and tested to be used within provided safe operating limits. Safe operating limits will raise a ValueError if the parameters are outside of the safe operating limits. Some are checked at construction time, others are checked at runtime and subject to RuntimeContext.suppress_runtime_errors. To opt-out of using safe operating limits, use the without_operating_limits class method instead of constructing directly.

Parameters:
  • center_sigma (float) – The center sigma for the center/surround pyramids.

  • surround_sigma (float) – The surround sigma for the center/surround pyramids.

  • n_scales (int) – The number of pyramid scales.

  • max_octaves (int | None) – The maximum number of pyramid octaves to construct.

  • combine (PyramidCombine) – The function to combine the on/off pyramids into a single pyramid.

  • collapse (PyramidCollapse) – The function to collapse the combined pyramid into a single image.

  • operating_limits (OperatingLimits | None) – The operating limits to use.

process(ctx: RuntimeContext, image: NDArray[float32]) → tuple[NDArray[float32], Pyramid][source]#

Compute salience for a single color channel.

Parameters:
  • ctx (RuntimeContext) – The runtime context.

  • image (NDArray[single]) – Must be float32 and in the range [0, 1].

Return type:

tuple[NDArray[single], Pyramid]

Returns:

A tuple of the feature map and the center pyramid.

Raises:

ValueError – If operating limits reject the image size.

class NoOperatingLimits(*args, **kwargs)[source]#

Bases: OperatingLimits

static validate(min_image_dim_size: int, center_sigma: float, surround_sigma: float) → ValueError | None[source]#
Return type:

ValueError | None

class Vocus2(color: ColorChannelSalience, depth: DepthSalience | None = None, orientation: OrientationSalience | None = None, color_space_converter: ColorSpaceConverter = <function rgb_to_opponent>, combine: MapCombine | None = None, normalize: Normalize = <function range_normalize>)[source]#

Bases: SalienceStrategy

Vocus2-based salience strategy.

This class is based on (VOCUS2)[GeeeG/VOCUS2]. It implements most of VOCUS2’s salience-based processing features but also extends it to operate on depth data.

classmethod from_config(config: Vocus2SalienceConfig, color_space_converter: ColorSpaceConverter = <function rgb_to_opponent>, combine: MapCombine | None = None, normalize: Normalize = <function range_normalize>) → Vocus2[source]#

Create a Vocus2 salience strategy from a configuration.

Since Vocus2 uses color, depth, and orientation that all need to be configured in a compatible way, this method creates the necessary components and configures them all at once from a single configuration.

Parameters:
Return type:

Vocus2

Returns:

A Vocus2 salience strategy.

__init__(color: ColorChannelSalience, depth: DepthSalience | None = None, orientation: OrientationSalience | None = None, color_space_converter: ColorSpaceConverter = <function rgb_to_opponent>, combine: MapCombine | None = None, normalize: Normalize = <function range_normalize>)[source]#
class Vocus2SalienceConfig(center_sigma: float = 3.0, surround_sigma: float = 5.0, n_scales: int = 2, max_octaves: int = 5, use_depth: bool = True, use_orientation: bool = True) → None[source]#

Bases: object

__init__(center_sigma: float = 3.0, surround_sigma: float = 5.0, n_scales: int = 2, max_octaves: int = 5, use_depth: bool = True, use_orientation: bool = True) → None#
center_sigma: float = 3.0#
max_octaves: int = 5#
n_scales: int = 2#
surround_sigma: float = 5.0#
use_depth: bool = True#
use_orientation: bool = True#

tbp.monty.frameworks.models.salience.strategies.vocus2.images#

class ColorSpaceConverter(*args, **kwargs)[source]#

Bases: Protocol

__init__(*args, **kwargs)#
gaussian_blur(image: NDArray[float32], sigma: float, truncate: float = 2.5) → NDArray[float32][source]#
Return type:

NDArray[single]

resize(image: NDArray[float32], shape: tuple[int, int], interpolation: int = 3) → NDArray[float32][source]#
Return type:

NDArray[single]

rgb_to_lab(image: NDArray[uint8]) → NDArray[float32][source]#

Returns the CIE Lab color space of the image.

This implementation comes from (VOCUS2)[GeeeG/VOCUS2].

Return type:

NDArray[single]

rgb_to_opponent(image: NDArray[uint8]) → NDArray[float32][source]#

Returns the Opponent color space of the image shifted and scaled to [0, 1].

This implementation comes from (VOCUS2)[GeeeG/VOCUS2].

Return type:

NDArray[single]

rgb_to_opponent_codi(image: NDArray[uint8]) → NDArray[float32][source]#

Returns the Opponent color space of the image (Klein/Frintrop DAGM 2012).

This implementation comes from (VOCUS2)[GeeeG/VOCUS2].

Return type:

NDArray[single]

tbp.monty.frameworks.models.salience.strategies.vocus2.pyramids#

class Pyramid(data: NDArray[object_]) → None[source]#

Bases: object

__init__(data: NDArray[object_]) → None#
apply(func: Callable) → Pyramid[source]#
Return type:

Pyramid

data: NDArray[object_]#
property flat: Iterator[NDArray[float32]]#
property n_octaves: int#
property n_scales: int#
property shape: tuple[int, int]#
property size: int#
class PyramidCollapse(*args, **kwargs)[source]#

Bases: Protocol

__init__(*args, **kwargs)#
class PyramidCombine(*args, **kwargs)[source]#

Bases: Protocol

__init__(*args, **kwargs)#
center_surround_pyramids(image: NDArray[float32], center_sigma: float, surround_sigma: float, n_scales: int, max_octaves: int | None = None) → tuple[Pyramid, Pyramid][source]#

Build center and surround pyramids.

Parameters:
  • image (NDArray[single]) – The image to build the pyramids from.

  • center_sigma (float) – The sigma for the center pyramid.

  • surround_sigma (float) – The sigma for the surround pyramid.

  • n_scales (int) – The number of scales in each pyramid.

  • max_octaves (int | None) – An optional maximum number of levels in the pyramids.

Return type:

tuple[Pyramid, Pyramid]

Returns:

A tuple of center and surround pyramids.

Raises:

ValueError – If center sigma is greater than or equal to surround sigma.

gaussian_pyramid(image: NDArray[float32], sigma: float, n_scales: int, max_octaves: int | None = None) → Pyramid[source]#

Build multi-scale pyramid following Lowe 2004.

This implementation is based on code in the (VOCUS2 C++ implementation)[GeeeG/VOCUS2] from Lowe, D. G. (2004). “Distinctive image features from scale-invariant keypoints”. International Journal of Computer Vision. 60 (2): 91-110.

This creates a 2D pyramid structure: - Dimension 1 (octaves): Different resolutions (each half the previous) - Dimension 2 (scales): Different smoothing levels within each octave

Parameters:
  • image (NDArray[single]) – Input image (single channel, float32)

  • sigma (float) – Base sigma for Gaussian smoothing

  • n_scales (int) – Number of scales in each octave

  • max_octaves (int | None) – Maximum number of levels in the pyramid

Return type:

Pyramid

Returns:

2D object-type array with shape (n_octaves, n_scales)

Raises:

ValueError – If image has size 0 or is not 2D.

Note that sigmas = [sigma * (2.0 ** (s / n_scales)) for s in range(pyr.size)]

laplacian_pyramid(pyr: Pyramid) → Pyramid[source]#

Build a multiscale Laplacian pyramid.

Parameters:

pyr (Pyramid) – The pyramid to build the Laplacian pyramid from.

Return type:

Pyramid

Returns:

A laplacian pyramid. Has one fewer octaves than the input pyramid.

Raises:

ValueError – If input pyramid doesn’t have at least two octaves.

pyramid_collapse(pyr: Pyramid, reduce: Callable[[Sequence[NDArray[float32]]], NDArray[float32]]) → NDArray[float32][source]#

Collapse a pyramid into a single image.

Parameters:
  • pyr (Pyramid) – The pyramid to collapse.

  • reduce (Callable[[Sequence[NDArray[single]]], NDArray[single]]) – The function to use to reduce the pyramid’s planes into one.

Return type:

NDArray[single]

Returns:

A new image.

pyramid_collapse_max(pyr: Pyramid) → NDArray[float32][source]#
Return type:

NDArray[single]

pyramid_collapse_mean(pyr: Pyramid) → NDArray[float32][source]#
Return type:

NDArray[single]

pyramid_combine(pyramids: Sequence[Pyramid], reduce: Callable[[Sequence[NDArray[float32]]], NDArray[float32]]) → Pyramid[source]#

Combine multiple pyramids into a single pyramid.

Parameters:
Return type:

Pyramid

Returns:

A new pyramid.

Raises:

ValueError – If no pyramids are provided.

pyramid_combine_max(pyramids: Sequence[Pyramid]) → Pyramid[source]#
Return type:

Pyramid

pyramid_combine_mean(pyramids: Sequence[Pyramid]) → Pyramid[source]#
Return type:

Pyramid

pyramid_octave_shapes(image_shape: tuple[int, int], max_octaves: int | None = None) → list[tuple[int, int]][source]#

Compute the shapes of the pyramid levels.

Parameters:
  • image_shape (tuple[int, int]) – The shape of the plane from which the pyramid will be built.

  • max_octaves (int | None) – The maximum number of levels in the pyramid.

Return type:

list[tuple[int, int]]

Returns:

A list of resolutions, each being the image shape for a pyramid’s octave.

tbp.monty.frameworks.models.salience.strategies.vocus2.vocus2#

class ColorChannelSalience(center_sigma: float = 2.0, surround_sigma: float = 3.0, n_scales: int = 2, max_octaves: int | None = None, combine: PyramidCombine = <function pyramid_combine_mean>, collapse: PyramidCollapse = <function pyramid_collapse_mean>, operating_limits: OperatingLimits | None = None)[source]#

Bases: object

__init__(center_sigma: float = 2.0, surround_sigma: float = 3.0, n_scales: int = 2, max_octaves: int | None = None, combine: PyramidCombine = <function pyramid_combine_mean>, collapse: PyramidCollapse = <function pyramid_collapse_mean>, operating_limits: OperatingLimits | None = None)[source]#

Create a ColorChannelSalience with safe operating limits.

ColorChannelSalience was designed and tested to be used within provided safe operating limits. Safe operating limits will raise a ValueError if the parameters are outside of the safe operating limits. Some are checked at construction time, others are checked at runtime and subject to RuntimeContext.suppress_runtime_errors. To opt-out of using safe operating limits, use the without_operating_limits class method instead of constructing directly.

Parameters:
  • center_sigma (float) – The center sigma for the center/surround pyramids.

  • surround_sigma (float) – The surround sigma for the center/surround pyramids.

  • n_scales (int) – The number of pyramid scales.

  • max_octaves (int | None) – The maximum number of pyramid octaves to construct.

  • combine (PyramidCombine) – The function to combine the on/off pyramids into a single pyramid.

  • collapse (PyramidCollapse) – The function to collapse the combined pyramid into a single image.

  • operating_limits (OperatingLimits | None) – The operating limits to use.

process(ctx: RuntimeContext, image: NDArray[float32]) → tuple[NDArray[float32], Pyramid][source]#

Compute salience for a single color channel.

Parameters:
  • ctx (RuntimeContext) – The runtime context.

  • image (NDArray[single]) – Must be float32 and in the range [0, 1].

Return type:

tuple[NDArray[single], Pyramid]

Returns:

A tuple of the feature map and the center pyramid.

Raises:

ValueError – If operating limits reject the image size.

class DepthSalience(center_sigma: float = 2.0, surround_sigma: float = 3.0, n_scales: int = 2, max_octaves: int | None = None, collapse: PyramidCollapse = <function pyramid_collapse_mean>, operating_limits: OperatingLimits | None = None)[source]#

Bases: object

__init__(center_sigma: float = 2.0, surround_sigma: float = 3.0, n_scales: int = 2, max_octaves: int | None = None, collapse: PyramidCollapse = <function pyramid_collapse_mean>, operating_limits: OperatingLimits | None = None)[source]#
process(ctx: RuntimeContext, image: NDArray[float32]) → NDArray[float32][source]#

Compute salience for a depth channel.

Parameters:
Return type:

NDArray[single]

Returns:

A DepthSalienceResult object.

Raises:

ValueError – If operating limits reject the image size.

class MapCombine(*args, **kwargs)[source]#

Bases: Protocol

__init__(*args, **kwargs)#
class NoOperatingLimits(*args, **kwargs)[source]#

Bases: OperatingLimits

static validate(min_image_dim_size: int, center_sigma: float, surround_sigma: float) → ValueError | None[source]#
Return type:

ValueError | None

class Normalize(*args, **kwargs)[source]#

Bases: Protocol

__init__(*args, **kwargs)#
class OperatingLimits(*args, **kwargs)[source]#

Bases: Protocol

static validate(min_image_dim_size: int, center_sigma: float, surround_sigma: float) → ValueError | None[source]#
Return type:

ValueError | None

__init__(*args, **kwargs)#
class OrientationSalience(period: float, sigma: float | None = None, phase: float = 1.5707963267948966, gamma: float = 0.75, n_orientations: int = 4, combine: MapCombine = <function map_mean>, collapse: PyramidCollapse = <function pyramid_collapse_mean>)[source]#

Bases: object

static make_kernels(period: float, sigma: float, phase: float = 1.5707963267948966, gamma: float = 0.75, n_orientations: int = 4) → dict[str, NDArray[float32]][source]#
Return type:

dict[str, NDArray[single]]

__init__(period: float, sigma: float | None = None, phase: float = 1.5707963267948966, gamma: float = 0.75, n_orientations: int = 4, combine: MapCombine = <function map_mean>, collapse: PyramidCollapse = <function pyramid_collapse_mean>)[source]#

Computes orientation salience.

Parameters:
  • period (float) – wavelength. Good default is center_sigma * 2

  • sigma (float | None) – mask sigma. Good default is 0.3 * period

  • phase (float) – phase. Good default is 90 degrees (pi / 2) for edge detection, 0 for strip detection.

  • gamma (float) – Eccentricity. Good default is 0.75

  • n_orientations (int) – number of orientations. Good default is 4

  • combine (MapCombine) – function to combine the feature maps. Good default is map_mean.

  • collapse (PyramidCollapse) – function to collapse the feature pyramids. Good default is pyramid_collapse_mean.

process(ctx: RuntimeContext, pyr: Pyramid) → NDArray[float32][source]#
Return type:

NDArray[single]

class SafeOperatingLimits(min_image_dim_size: int = 64, max_fractional_center_sigma: float = 0.1, max_fractional_surround_sigma: float = 0.5, min_fractional_sigma_separation: float = 0.02) → None[source]#

Bases: OperatingLimits

Within these limits, Vocus2 was tested and has desired and predictable behavior.

Outside of these limits, Vocus2 may work as intended, may work not as expected, or may not work at all. Use with caution.

The meaning of a smoothing kernel (sigma) size depends upon the resolution of the image. For example, a smoothing kernel size of three may be considered large for a 64x64 image, but small for a 128x128 image. This matters because the smoothing kernel size is what determines the _scale_ of the feature that is extracted from the image.

For this reason, there are no suitable minimum and maximum sigmas that are appropriate for all image resolutions. Instead, we express the sigma ranges as fractions of the smallest image dimension (hence “fractional_” throughout). For example, an image with resolution of 64x96, has a smallest image dimension of 64. The maximum fractional center sigma of 0.1 would mean a 6.4 pixels (0.1 * 64) is the highest value that a center sigma can take and still be within safe operating limits.

min_image_dim_size#

The minimum (smallest) dimension of the images being processed.

max_fractional_center_sigma#

The largest (fractional) center sigma that is still within safe operating limits.

max_fractional_surround_sigma#

The largest (fractional) surround sigma that is within safe operating limits.

min_fractional_sigma_separation#

We expect that the surround sigma will be greater than the center sigma. However, the surround sigma should be meaningfully bigger than the center sigma. This is the minimum meaningful separation between the center and surround sigmas. As explained above, this is expressed as a fraction of the smallest image dimension.

static validate(min_image_dim_size: int, center_sigma: float, surround_sigma: float) → ValueError | None[source]#
Return type:

ValueError | None

__init__(min_image_dim_size: int = 64, max_fractional_center_sigma: float = 0.1, max_fractional_surround_sigma: float = 0.5, min_fractional_sigma_separation: float = 0.02) → None#
max_fractional_center_sigma: float = 0.1#
max_fractional_surround_sigma: float = 0.5#
min_fractional_sigma_separation: float = 0.02#
min_image_dim_size: int = 64#
class Vocus2(color: ColorChannelSalience, depth: DepthSalience | None = None, orientation: OrientationSalience | None = None, color_space_converter: ColorSpaceConverter = <function rgb_to_opponent>, combine: MapCombine | None = None, normalize: Normalize = <function range_normalize>)[source]#

Bases: SalienceStrategy

Vocus2-based salience strategy.

This class is based on (VOCUS2)[GeeeG/VOCUS2]. It implements most of VOCUS2’s salience-based processing features but also extends it to operate on depth data.

classmethod from_config(config: Vocus2SalienceConfig, color_space_converter: ColorSpaceConverter = <function rgb_to_opponent>, combine: MapCombine | None = None, normalize: Normalize = <function range_normalize>) → Vocus2[source]#

Create a Vocus2 salience strategy from a configuration.

Since Vocus2 uses color, depth, and orientation that all need to be configured in a compatible way, this method creates the necessary components and configures them all at once from a single configuration.

Parameters:
Return type:

Vocus2

Returns:

A Vocus2 salience strategy.

__init__(color: ColorChannelSalience, depth: DepthSalience | None = None, orientation: OrientationSalience | None = None, color_space_converter: ColorSpaceConverter = <function rgb_to_opponent>, combine: MapCombine | None = None, normalize: Normalize = <function range_normalize>)[source]#
class Vocus2SalienceConfig(center_sigma: float = 3.0, surround_sigma: float = 5.0, n_scales: int = 2, max_octaves: int = 5, use_depth: bool = True, use_orientation: bool = True) → None[source]#

Bases: object

__init__(center_sigma: float = 3.0, surround_sigma: float = 5.0, n_scales: int = 2, max_octaves: int = 5, use_depth: bool = True, use_orientation: bool = True) → None#
center_sigma: float = 3.0#
max_octaves: int = 5#
n_scales: int = 2#
surround_sigma: float = 5.0#
use_depth: bool = True#
use_orientation: bool = True#
class WeightedMean(weights: dict[str, float])[source]#

Bases: MapCombine

__init__(weights: dict[str, float])[source]#
class WeightedSum(weights: dict[str, float])[source]#

Bases: MapCombine

__init__(weights: dict[str, float])[source]#
map_max(maps: FeatureMaps) → NDArray[float32][source]#
Return type:

NDArray[single]

map_mean(maps: FeatureMaps) → NDArray[float32][source]#
Return type:

NDArray[single]

map_sum(maps: FeatureMaps) → NDArray[float32][source]#
Return type:

NDArray[single]

no_normalize(salience_map: NDArray[float32]) → NDArray[float32][source]#
Return type:

NDArray[single]

range_normalize(salience_map: NDArray[float32]) → NDArray[float32][source]#
Return type:

NDArray[single]