ParticleDetection.utils.helper_funcs

Collection of miscellaneous helper functions.

configure_logging(level: int = logging.INFO)[source]

Configure the default output to stdout by this library.

Parameters:

level (int, optional) – By default logging.INFO.

find_world_transform(calibration_file: str, edges_cam1_dist: ndarray, edges_cam2_dist: ndarray, edges_3D: ndarray, out_json: str)[source]

Find world transformation from camera 1 coordinate system to the desired world coordinate system.

Parameters:
  • calibration_file (str) – Path to a stereo calibration file

  • edges_cam1_dist (np.ndarray(8,2)) –

    Should contain 2D coordinates of box edges (corners) on 1st camera view (not undistorted):

    [front left up, front left down, front right up, front right down,

    back left up, back left down, back right up, back right down], e.g.

    >>> np.array([[27, 36], [30, 904], [1235, 27], [1240, 903],
    ...           [183, 149], [188, 900], [1096, 140], [1098, 790]]
    ...         ).astype(float)
    

  • edges_cam2_dist (np.ndarray(8,2)) –

    Should contain 2D coordinates of box edges (corners) on 2nd camera view (not undistorted):

    [front left up, front left down, front right up, front right down,

    back left up, back left down, back right up, back right down]

  • edges_3D (np.ndarray(8,3)) –

    Should contain 3D coordinates of box edges (in the final world coordinate system). Choose coordinate system, for example with 0 at the center of the box, e.g.

    >>> edges_3D = np.array([[-58,40,40], [-58,-40,40], [58,40,40],
    ...                      [58,-40,40], [-58,40,-40], [-58,-40,-40],
    ...                      [58,40,-40],[58,-40,-40]]).astype(float)
    

  • out_json (str) – Path where the resulting transformation file should be saved in JSON format.

Returns:

  • rot_comb (np.ndarray(3,3)) – Rotation matrix

  • trans_vec (np.ndarray(3,1)) – Translation vector

Notes

The transformation of 3D coordinates can then be performed as

>>> p_world = rot_comb.apply(p_cam1) + trans_vec
line_estimator(segmentation: ndarray) ndarray[source]

Calculates the endpoints of rods from the segmentation mask.

Parameters:

segmentation (ndarray) – Boolean segmentation (bit-)mask.

Returns:

np.ndarray

line_estimator_simple(segmentation: ndarray) ndarray[source]

Calculates the endpoints of rods from the segmentation mask.

Parameters:

segmentation (ndarray) – Boolean segmentation (bit-)mask.

Returns:

np.ndarray

paste_mask_in_image_old(mask: Tensor, box: Tensor, img_h: int, img_w: int, threshold: float = 0.5)[source]

Paste a single mask in an image.

This is a per-box implementation of paste_masks_in_image. This function has larger quantization error due to incorrect pixel modeling and is not used any more.

Parameters:
  • mask (Tensor) – A tensor of shape (Hmask, Wmask) storing the mask of a single object instance. Values are \(\in [0, 1]\).

  • box (Tensor) – A tensor of shape (4, ) storing the x0, y0, x1, y1 box corners of the object instance.

  • img_h (int) – Image height.

  • img_w (int) – Image width.

  • threshold (float) –

    Mask binarization threshold \(\in [0, 1]\).

    Default is 0.5.

Returns:

Tensor – The resized and binarized object mask pasted into the original image plane (a tensor of shape (img_h, img_w)).

Note

This function is copied from detectron2.layers.mask_ops.

rod_endpoints(prediction: DetectionResult, classes: Dict[int, str], method: str = 'simple', expected_particles: int | Dict[int, int] | None = None) Dict[int, ndarray][source]

Calculates the endpoints of rods from the prediction masks.

Parameters:
  • prediction (DetectionResult) – Prediction output of a Detectron2 network. It can also be given as prediction["instances"] as detectron2.structures.Instances or dict, as long as the resulting dict contains at least the same keys as DetectionResult.

  • classes (dict[int, str]) – Dictionary of classes expected/possible in the prediction. The key being the class ID as an integer, that is the output of the inferring network. The value being an arbitrary string associated with the class, e.g. {1: "blue", 2: "green"}.

  • method (str) –

    Selection of endpoint extraction method:

    "simple" -> Creates a bounding box around the masks.

    "advanced" -> Creates Hough lines and clusters these.

    Default is "simple".

  • expected_particles (Union[int, Dict[int, int], None]) –

    The number of expected particles defines how many particles will be in the output per frame. This defines how many particles are maximally detected and also up to which number empty particles will be inserted to match the expected amount.

    int

    Only one amount is defined. The same amount is expected for all classes that will be detected.

    Dict[int, int]

    One amount must be specified per class that is present in prediction. expected_particles[class] = amount

    None

    No restrictions on the amount of particles per class and frame are imposed. How ever many particles were detected will be in the output.

    Default is None.

Returns:

dict[int, np.ndarray]