Neobotics Foundation Inc.
These docs are public and open source.Edit on GitHub

RACECAR_UTILS

The racecar_utils module provides helper functions for working with numbers, images, and the LiDAR. The LiDAR helpers are on the rc.lidar page.

NUMBERS

racecar_utils.clamp(value: float, min: float, max: float)returns float

Clamps a value between a minimum and a maximum.

racecar_utils.remap_range(val: float, old_min: float, old_max: float, new_min: float, new_max: float, saturate: bool = False)returns float

Remaps a value from one range to another. With saturate, the result is clamped to the new range.

IMAGES

racecar_utils.crop(image: NDArray, top_left_inclusive: tuple[float, float], bottom_right_exclusive: tuple[float, float])returns NDArray

Returns the rectangle of the image between the two (row, column) corners.

racecar_utils.stack_images_horizontal(image_0: NDArray, image_1: NDArray)returns NDArray

Joins two images side by side. stack_images_vertical joins them top to bottom.

COLOR CONTOURS

A contour is the outline of a connected region of one color. These are the functions behind line following and cone tracking.

racecar_utils.find_contours(color_image: NDArray, hsv_lower: tuple[int, int, int], hsv_upper: tuple[int, int, int])returns list[NDArray]

Finds all contours of the given HSV color range in the image.

racecar_utils.get_largest_contour(contours: list[NDArray], min_area: int = 30)returns NDArray | None

Returns the largest contour with area greater than min_area, or None if there is none.

racecar_utils.get_contour_center(contour: NDArray)returns tuple[int, int] | None

Returns the (row, column) center of a contour.

racecar_utils.get_contour_area(contour: NDArray)returns float

Returns the area of a contour in pixels.

racecar_utils.draw_contour(color_image: NDArray, contour: NDArray, color: tuple[int, int, int] = ColorBGR.green.value)returns None

Draws a contour outline onto the image, in place.

racecar_utils.draw_circle(color_image: NDArray, center: tuple[int, int], color: tuple[int, int, int] = ColorBGR.yellow.value, radius: int = 6)returns None

Draws a circle onto the image, in place.

AR MARKERS

racecar_utils.get_ar_markers(color_image: NDArray, potential_colors: list[tuple[tuple[int, int, int], tuple[int, int, int], str]] = None, marker_type: int = cv.aruco.DICT_6X6_250)returns list[ARMarker]

Finds ArUco markers in the image. Each marker carries its id and corner positions.

racecar_utils.draw_ar_markers(color_image: NDArray, markers: list[ARMarker], color: tuple[int, int, int] = ColorBGR.green.value)returns None

Draws detected markers onto the image, in place.

EXAMPLE USAGE

python
import racecar_core import racecar_utils rc = racecar_core.create_racecar() BLUE = ((90, 50, 50), (120, 255, 255)) # HSV range def start(): pass def update(): image = rc.camera.get_color_image() contours = racecar_utils.find_contours(image, BLUE[0], BLUE[1]) largest = racecar_utils.get_largest_contour(contours) if largest is not None: center = racecar_utils.get_contour_center(largest) # steer toward the contour: map its column to a steering angle angle = racecar_utils.remap_range(center[1], 0, image.shape[1], -1, 1) rc.drive.set_speed_angle(0.3, angle) else: rc.drive.stop() rc.set_start_update(start, update) rc.go()

For full documentation, visit the racecar-neo-library documentation.