Note

This is the documentation for the latest development branch and may refer to features that are not available in released versions. If you are looking for a specific release, use the drop-down menu on the left.

Color Block Detection and Color Tracking Example

Color Block Detection and Color Tracking Example#

Overview#

find_blobs is an image processing function of OpenMV, used to find and identify “blobs” in an image. These blobs refer to regions in the image with similar color or brightness. This function is commonly used in visual detection and recognition applications, such as object tracking, color recognition, etc.

CanMV supports OpenMV algorithms and can also use find_blobs.

Function Explanation#

Basic Usage#

blobs = img.find_blobs([thresholds], area_threshold=area_threshold, pixels_threshold=pixels_threshold, merge=True, margin=0)

Parameter Explanation#

  • thresholds: This is a list containing color ranges, used to define the color range of the blobs to look for. It is usually a tuple containing two or three elements. For example, (100, 200, -64, 127, -128, 127) represents a range in the HSV color space, where the first and second values are Hue, the third and fourth are Saturation, and the last two are Value.

  • area_threshold: The area of the spot threshold. Only spots larger than this value will be returned. Default is 0.

  • pixels_threshold: Minimum blob pixel count. Only blobs with more pixels than this threshold are returned. The default is 0.

  • merge: Whether to merge adjacent blobs. When set to True, adjacent blobs will be merged into one large blob; when set to False, blobs will not be merged. The default value is True.

  • Margin: The margin used to merge the spots. Sets a positive integer, indicating the maximum distance when combining the spots. The default value is 0.

Return Value#

The find_blobs function returns a list containing blob information. Each blob is a Blob object, which typically contains the following attributes:

  • cx and cy: The center coordinates of the blob.

  • x and y: The coordinates of the top-left corner of the blob.

  • w and h: The width and height of the blob.

  • area: The area of the blob (pixel count).

Example#

Only a single color tracking example is listed here. For specific demos, please refer to the routines in the virtual USB drive that comes with the firmware.

# Single-color code tracking example
#
# This example demonstrates single-color code tracking using a CanMV camera.
#
# A color code consists of a blob composed of two or more colors. The example below will
# track colored objects containing only the following colors.
import time, os, gc, sys, math

from media.sensor import *
from media.display import *
from media.media import *

DETECT_WIDTH = 640
DETECT_HEIGHT = 480

# Color tracking thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The following thresholds are generally used to track red/green objects. You may need to adjust them...
thresholds = [(12, 100, -47, 14, -1, 58), # General red threshold -> index is 0, so code == (1 << 0)
              (30, 100, -64, -8, -32, 32)] # General green threshold -> index is 1, so code == (1 << 1)
# When "merge=True", codes are combined with "OR" for "find_blobs"

# Only blobs with a pixel count greater than "pixel_threshold" and an area greater than "area_threshold"
# will be returned by "find_blobs" below. If you change the camera resolution, change "pixel_threshold" and "area_threshold".
# "merge=True" must be set to merge overlapping color blobs to form a color code.

sensor = None

try:
    # Construct a Sensor object using the default configuration
    sensor = Sensor(width = DETECT_WIDTH, height = DETECT_HEIGHT)
    # Reset the sensor
    sensor.reset()
    # Set horizontal mirroring
    # sensor.set_hmirror(False)
    # Set vertical flipping
    # sensor.set_vflip(False)
    # Set channel 0 output size
    sensor.set_framesize(width = DETECT_WIDTH, height = DETECT_HEIGHT)
    # Set channel 0 output format
    sensor.set_pixformat(Sensor.RGB565)

    # Set up the display. If the screen you choose cannot be turned on, refer to k230_canmv_display_module_api_manual in the API documentation for manual configuration. Four display methods are given below.
    # Use HDMI as the display output, configured for VGA
    # Display.init(Display.LT9611, width = 640, height = 480, to_ide = True)

    # Use HDMI as the display output, configured for 1080P
    # Display.init(Display.LT9611, width = 1920, height = 1080, to_ide = True)

    # Use LCD as the display output
    # Display.init(Display.ST7701, to_ide = True)

    # Use IDE as the output
    Display.init(Display.VIRT, width = DETECT_WIDTH, height = DETECT_HEIGHT, fps = 100)

    # Initialize the media manager
    MediaManager.init()
    # Start the sensor
    sensor.run()

    fps = time.clock()

    while True:
        fps.tick()

        # Check whether it should exit
        os.exitpoint()
        img = sensor.snapshot()

        for blob in img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100, merge=True):
            if blob.code() == 3: # r/g code == (1 << 1) | (1 << 0)
                # These values depend on the blob not being round; otherwise, they will be unstable
                # if blob.elongation() > 0.5:
                #     img.draw_edges(blob.min_corners(), color=(255,0,0))
                #     img.draw_line(blob.major_axis_line(), color=(0,255,0))
                #     img.draw_line(blob.minor_axis_line(), color=(0,0,255))
                # These values are stable at all times
                img.draw_rectangle([v for v in blob.rect()])
                img.draw_cross(blob.cx(), blob.cy())
                # Note - blob rotation is unique and limited to 0-180
                img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)

        # Draw the results on the screen
        Display.show_image(img)
        gc.collect()

        print(fps.fps())
except KeyboardInterrupt as e:
    print(f"user stop")
except BaseException as e:
    print(f"Exception '{e}'")
finally:
    # Stop the sensor
    if isinstance(sensor, Sensor):
        sensor.stop()
    # Destroy the display
    Display.deinit()

    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)

    # Release the media buffers
    MediaManager.deinit()

Message

For specific interface definitions, please refer to find_blobs

Comments list
Comments
Log in