robokudo.utils.cv_helper

OpenCV helper utilities for Robokudo.

This module provides helper functions for working with OpenCV images and operations. It supports:

  • Image cropping and ROI handling

  • ROS-OpenCV image conversion

  • Color space conversions

  • Bounding box operations

  • Image scaling and resizing

  • Drawing utilities

Functions

crop_image(→ numpy.typing.NDArray)

Crop region from image using coordinates and dimensions.

crop_image_roi(→ numpy.typing.NDArray)

Crop region from image using ROI object.

get_scaled_color_image_for_depth_image(...)

Scale color image to match depth image resolution.

get_scale_coordinates(→ typing_extensions.Tuple[int, int])

Scale coordinates based on color-to-depth ratio.

get_hsv_for_rgb_color(→ numpy.typing.NDArray)

Convert RGB color to HSV color space.

get_hsv_for_bgr_color(→ numpy.typing.NDArray)

Convert BGR color to HSV color space.

draw_rectangle_around_center(→ numpy.typing.NDArray)

Draw filled rectangle centered at given point.

clamp_bounding_rect(→ cv2.typing.Rect)

Clamp bounding rectangle to image boundaries.

rect_outside_image(→ bool)

Check whether a bounding rectangle is completely outside an image.

sanity_checks_bounding_rects(→ bool)

Check if bounding rectangle is valid for image dimensions.

adjust_roi(→ typing_extensions.Tuple[int, int, int, int])

Adjusts the ROI by a given offset while respecting image boundaries, keeping the same center point.

adjust_image_roi(→ None)

Adjusts the ImageROI's ROI by a given offset while respecting image boundaries,

adjust_mask(→ numpy.typing.NDArray)

Adjusts the mask by a given offset. When expanding, the new areas are filled with the specified fill value.

Module Contents

robokudo.utils.cv_helper.crop_image(image: numpy.typing.NDArray, xy: tuple, wh: tuple) numpy.typing.NDArray

Crop region from image using coordinates and dimensions.

Based on https://stackoverflow.com/a/67799880

Parameters:
  • image – Input image

  • xy – Top-left corner coordinates (x,y)

  • wh – Width and height of crop region (w,h)

Returns:

Cropped image region

robokudo.utils.cv_helper.crop_image_roi(image: numpy.typing.NDArray, roi: robokudo.types.cv.ImageROI) numpy.typing.NDArray

Crop region from image using ROI object.

Parameters:
  • image – Input image

  • roi – Region of interest

Returns:

Cropped image region

robokudo.utils.cv_helper.get_scaled_color_image_for_depth_image(cas: robokudo.cas.CAS, color_image: numpy.typing.NDArray) numpy.typing.NDArray

Scale color image to match depth image resolution.

If the color2depth ratio is not 1,1, we will usually scale the color image to the same size the depth image has. This method will get check if a conversion is required and will return the correct size. This is usually called before o3d.geometry.RGBDImage.create_from_color_and_depth is called, so that depth and color image match.

Parameters:
  • cas – The CAS where the COLOR2DEPTH_RATIO is stored.

  • color_image – The color image to be adjusted

Returns:

A resized copy of the color image if a resize necessary. Otherwise, the unchanged color_image

is returned. :raises RuntimeError: If color-to-depth ratio not set in CAS

robokudo.utils.cv_helper.get_scale_coordinates(color2depth_ratio: typing_extensions.Tuple[int, int], coordinates: typing_extensions.Tuple[int, int]) typing_extensions.Tuple[int, int]

Scale coordinates based on color-to-depth ratio.

Parameters:
  • color2depth_ratio – Scale factors (x_scale, y_scale)

  • coordinates – Input coordinates (x, y)

Returns:

Scaled coordinates

Raises:

RuntimeError – If color-to-depth ratio not provided

robokudo.utils.cv_helper.get_hsv_for_rgb_color(rgb: typing_extensions.Tuple[int, int, int]) numpy.typing.NDArray

Convert RGB color to HSV color space.

Parameters:

rgb – RGB color values (r,g,b)

Returns:

HSV color values

robokudo.utils.cv_helper.get_hsv_for_bgr_color(bgr: typing_extensions.Tuple[int, int, int]) numpy.typing.NDArray

Convert BGR color to HSV color space.

Parameters:

bgr – BGR color values (b,g,r)

Returns:

HSV color values

robokudo.utils.cv_helper.draw_rectangle_around_center(binary_image: numpy.typing.NDArray, x: int, y: int, width: int, height: int, value: int = 255) numpy.typing.NDArray

Draw filled rectangle centered at given point.

Parameters:
  • binary_image – Binary image to draw on

  • x – Center x coordinate

  • y – Center y coordinate

  • width – Rectangle width

  • height – Rectangle height

  • value – Fill value for rectangle

Returns:

Image with drawn rectangle

robokudo.utils.cv_helper.clamp_bounding_rect(bounding_rect: cv2.typing.Rect, image_width: int, image_height: int) cv2.typing.Rect

Clamp bounding rectangle to image boundaries.

Parameters:
  • bounding_rect – Input bounding rectangle (x,y,w,h)

  • image_width – Image width

  • image_height – Image height

Returns:

Clamped bounding rectangle

robokudo.utils.cv_helper.rect_outside_image(bounding_rect: cv2.typing.Rect, image_width: int, image_height: int) bool

Check whether a bounding rectangle is completely outside an image.

A bounding rectangle is considered outside if it has no overlapping area with the image. Rectangles that only touch the image border (zero-area intersection) are treated as outside.

Parameters:
  • bounding_rect – Bounding rectangle to check (x, y, w, h)

  • image_width – Image width

  • image_height – Image height

Returns:

True if the bounding rectangle lies completely outside the image, False otherwise

robokudo.utils.cv_helper.sanity_checks_bounding_rects(bounding_rect: cv2.typing.Rect, image_width: int, image_height: int) bool

Check if bounding rectangle is valid for image dimensions.

Check basic rules of a boundingRect to reject bad ones. Might happen if projections are done on objects that are out-of-view etc.

Parameters:
  • bounding_rect – Bounding rectangle to check (x,y,w,h)

  • image_width – Image width

  • image_height – Image height

Returns:

True if rules have been passed, False otherwise

robokudo.utils.cv_helper.adjust_roi(image: numpy.typing.NDArray, roi: typing_extensions.Tuple[int, int, int, int], offset: int) typing_extensions.Tuple[int, int, int, int]

Adjusts the ROI by a given offset while respecting image boundaries, keeping the same center point.

Parameters:
  • image – The image (OpenCV format).

  • roi – A tuple (x, y, width, height) representing the bounding box.

  • offset – The amount by which to grow or shrink the ROI (can be negative).

Returns:

A tuple (new_x, new_y, new_width, new_height) representing the adjusted ROI.

robokudo.utils.cv_helper.adjust_image_roi(image: numpy.typing.NDArray, image_roi: robokudo.types.cv.ImageROI, offset: int) None

Adjusts the ImageROI’s ROI by a given offset while respecting image boundaries, keeping the same center point. This method uses the adjust_roi function.

Parameters:
  • image – The image this ROI is relative to. Necessary to respect the boundaries properly.

  • image_roi – An object of type ImageROI.

  • offset – The amount by which to grow or shrink the ROI (can be negative).

Returns:

None. The ImageROI’s ROI is adjusted in place.

robokudo.utils.cv_helper.adjust_mask(mask: numpy.typing.NDArray, offset: int, fill_value: int = 0) numpy.typing.NDArray

Adjusts the mask by a given offset. When expanding, the new areas are filled with the specified fill value.

Parameters:
  • mask – The mask corresponding to the ROI (same dimensions as the ROI).

  • offset – The amount by which to grow or shrink the mask (can be negative).

  • fill_value – The value to fill new areas when expanding the mask.

Returns:

The adjusted mask.