Essential Camera Configuration and Vision Primitives for OpenMV

Camera Hardware Initialization and Configuration

Proper setup of the image sensor is required before capturing frames. The following routiness manage hardware state and image acquisition parameters:

  • sensor.reset(): Reinitializes the camera module to its default state.
  • sensor.set_pixformat(fmt): Defines the color depth. Use sensor.GRAYSCALE for 8-bit monochrome data or sensor.RGB565 for 16-bit color output.
  • sensor.set_framesize(size): Adjusts the output resolution (e.g., sensor.QVGA).
  • sensor.skip_frames(count=15): Discards a specified number of frames immediately after configuration changes to allow the sensor's auto-exposure and white balance algorithms to stabilize.
  • sensor.snapshot(): Captures a single frame and returns an image object for processing.
  • sensor.set_auto_gain(enable): Toggles automatic gain control.
  • sensor.set_auto_whitebal(enable): Toggles automatic white balance adjustment.
  • sensor.set_auto_exposure(enable, exposure_us): Controls automatic exposure. When disabled, exposure_us manually sets the shutter duration in microseconds.
  • sensor.set_windowing(roi): Crops the sensor output to a specific region of interest. The roi tuple follows the (x, y, width, height) structure.
  • sensor.set_hmirror(state) / sensor.set_vflip(state): Applies horizontal mirroring or vertical flipping to the raw sensor data.

Frame Annotation and Drawing Primitives

The image class provides built-in methods for overlaying graphics directly onto captured frames. These operations modify the pixel buffer in place.

  • img.draw_line(coords, color): Renders a line segment. coords expects (x1, y1, x2, y2).
  • img.draw_rectangle(bounds, color): Outlines a rectangular area using (x, y, w, h).
  • img.draw_circle(cx, cy, r, color): Plots a circle centered at (cx, cy) with radius r.
  • img.draw_cross(cx, cy, size, color): Draws a crosshair marker.
  • img.draw_string(cx, cy, message, color): Overlays ASCII text at the specified coordinates.

Drawing Implementation Example:

import sensor
import image

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(15)

frame = sensor.snapshot()
primary_color = (255, 255, 0)  # Yellow
secondary_color = (0, 255, 255) # Cyan

# Render geometric overlays
frame.draw_line((20, 20, 150, 150), color=primary_color)
frame.draw_rectangle((40, 60, 80, 50), color=secondary_color)
frame.draw_circle(160, 120, 25, color=primary_color)
frame.draw_cross(80, 40, size=8, color=secondary_color)
frame.draw_string(10, 200, "Vision Overlay Test", color=primary_color)

Color Blob Detection and Filtering

Machine vision tasks frequently rely on isolating regions based on color thresholds. The find_blobs() method scans the frame and returns a list of matching regions.

Key Configuration Parameters:

  • thresholds: A tuple or list of tuples defining the acceptable color range (typically LAB format: L_MIN, L_MAX, A_MIN, A_MAX, B_MIN, B_MAX).
  • x_stride / y_stride: Minimum pixel step size during the scan. Increasing these values improves performence but may miss narrow features.
  • invert: When set to True, the algorithm searches for colors outside the provided threshold range.
  • area_threshold / pixels_threshold: Filters out detected regions that fall below the specified bounding box area or total pixel count.
  • merge: Enables automatic combination of overlapping or adjacent detected regions into a single bounding box.

Blob Tracking Implementation:

import sensor
import image

# Define LAB color range for detection
target_thresholds = [(30, 100, -64, -8, -32, 32)]

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(15)

current_frame = sensor.snapshot()

# Execute color segmentation with filtering
detected_regions = current_frame.find_blobs(
    target_thresholds,
    pixels_threshold=50,
    area_threshold=50,
    merge=True,
    margin=5
)

highlight_color = (0, 0, 255)  # Blue

for region in detected_regions:
    if region.code() == 1:  # Verify threshold match index
        bounds = region.rect()
        centroid = region.cx(), region.cy()

        # Analyze and annotate the detected blob
        current_frame.draw_rectangle(bounds, color=highlight_color, thickness=2)
        current_frame.draw_cross(centroid[0], centroid[1], size=6, color=highlight_color)
        current_frame.draw_string(bounds[0], bounds[1] - 10, f"Area:{region.pixels()}", color=highlight_color)

Tags: OpenMV MicroPython Computer Vision Image Processing Embedded Systems

Posted on Sat, 11 Jul 2026 16:20:02 +0000 by jaygattis