WaterDropletPerturber

class nrtk.impls.perturb_image.generic.water_droplet_perturber.WaterDropletPerturber(size_range: Sequence[float] = [0.0, 1.0], num_drops: int = 20, blur_strength: float = 0.25, psi: float = 1.5707963267948966, n_air: float = 1.0, n_water: float = 1.33, f_x: int = 400, f_y: int = 400, seed: int | None = None, box_alignment_mode: str = 'extent')

Implements the physics-based, photorealistic water/rain droplet model.

The WaterDropletPerturber class simulates the effects of rain/water droplets on an image similar to the rain drops on a window, car-windshield, etc. The equations defined for this model are based on the dynamics, geometry and photometry of a water/rain droplet.

Attributes:
size_range (Sequence[float]): Range of size multiplier values used for computing

the size of the water droplet.

num_drops (int): Target number of water droplets. blur_strength (float): Strength of Gaussian blur operation. psi (float): Angle between the camera line-of-sight and glass plane (radians). n_air (float): Density of air. n_water (float): Density of water. f_x (int): Camera focal length in x direction (cm). f_y (int): Camera focal length in y direction (cm). seed (int): Random seed for reproducibility.

Methods

blur

Blur the area within the boundaries of the droplets

from_config

Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments.

get_config

Returns the current configuration of the WaterDropletPerturber instance.

get_default_config

Generate and return a default configuration dictionary for this class.

get_impls

Discover and return a set of classes that implement the calling class.

get_type_string

Returns the fully qualified type string of the PerturbImage class or its subclass.

is_usable

Checks if the necessary dependencies (OpenCV, Scipy and Shapely) are available.

perturb

Applies the Water Droplet perturbation effect to the provided input image.

render

Rendering the image with the Water Droplet effect.

__init__(size_range: Sequence[float] = [0.0, 1.0], num_drops: int = 20, blur_strength: float = 0.25, psi: float = 1.5707963267948966, n_air: float = 1.0, n_water: float = 1.33, f_x: int = 400, f_y: int = 400, seed: int | None = None, box_alignment_mode: str = 'extent') None

Initializes the WaterDropletPerturber.

Parameters:
  • size_range – Range of size multiplier values used for computing the size of the water droplet.

  • num_drops – Target number of water droplets.

  • blur_strength – Strength of Gaussian blur operation.

  • psi – Angle between the line-of-sight and glass plane (radians).

  • n_air – Density of air.

  • n_water – Density of water.

  • f_x – Camera focal length in x direction (cm).

  • f_y – Camera focal length in y direction (cm).

  • seed – Random seed for reproducibility.

  • box_alignment_mode

    Mode for how to handle how bounding boxes change. Should be one of the following options:

    extent: a new axis-aligned bounding box that encases the transformed misaligned box extant: a new axis-aligned bounding box that is encased inside the transformed misaligned box median: median between extent and extant

    Default value is extent

If any of the parameters are absent, the following values will be set as defaults:

size_range = (0.0, 1.0) num_drops = 20 blur_strength = 0.25 psi = 90.0 / 180.0 * np.pi n_air = 1.0 n_water = 1.33 f_x = 400 f_y = 400 seed = None

Raises:

ImportError if OpenCV, Scipy or Shapely is not found, install via

pip install nrtk[waterdroplet,graphics] or pip install nrtk[waterdroplet,headless].

blur(image: ndarray, rain_image: ndarray, mask: ndarray) ndarray

Blur the area within the boundaries of the droplets

Args:

image (np.ndarray): Input Image. rain_image (np.ndarray): Image rendered with Water droplet effect. mask (np.ndarray): Image mask.

Returns:

np.ndarray: Output of blur operation applied on the rain_image.

get_config() dict[str, Any]

Returns the current configuration of the WaterDropletPerturber instance.

Returns:

dict[str, Any]: Configuration dictionary with current settings.

classmethod is_usable() bool

Checks if the necessary dependencies (OpenCV, Scipy and Shapely) are available.

Returns:

bool: True if OpenCV, Scipy and Shapely are available.

perturb(image: np.ndarray, boxes: Iterable[tuple[AxisAlignedBoundingBox, dict[Hashable, float]]] | None = None, additional_params: dict[str, Any] | None = None) tuple[np.ndarray, Iterable[tuple[AxisAlignedBoundingBox, dict[Hashable, float]]] | None]

Applies the Water Droplet perturbation effect to the provided input image.

Args:

image (np.ndarray): The image to be perturbed. boxes (Iterable[tuple[AxisAlignedBoundingBox, dict[Hashable, float]]], optional): Bounding boxes

for detections in input image

additional_params (dict[str, Any], optional): Additional parameters, if applicable.

Returns:

np.ndarray: The perturbed image. Iterable[tuple[AxisAlignedBoundingBox, dict[Hashable, float]]]: Bounding boxes

scaled to perturbed image shape.

render(image: ndarray) tuple[ndarray, ndarray]

Rendering the image with the Water Droplet effect.

Args:

image (np.ndarray): Input Image.

Returns:

np.ndarray: Image rendered with Water Droplet effect. np.ndarray: Image mask.