This example shows off how to use readout window control to readout a small part of a camera

# This work is licensed under the MIT license.
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# This example shows off how to use readout window control to readout a small part of a camera
# sensor pixel array at a very high speed and move that readout window around.

# This example is was designed and tested on the OpenMV Cam H7 Plus using the OV5640 sensor.

import csi
import time

EXPOSURE_MICROSECONDS = 1000
TRACKING_THRESHOLDS = [(128, 255)]  # When you lower the exposure you darken everything.

SEARCHING_RESOLUTION = csi.VGA
SEARCHING_AREA_THRESHOLD = 16
SEARCHING_PIXEL_THRESHOLD = SEARCHING_AREA_THRESHOLD

TRACKING_RESOLUTION = csi.QQVGA
TRACKING_AREA_THRESHOLD = 256
TRACKING_PIXEL_THRESHOLD = TRACKING_AREA_THRESHOLD

TRACKING_EDGE_TOLERANCE = 0.05  # Blob can move 5% away from the center.

csi0 = csi.CSI()
csi0.reset()  # Reset and initialize the sensor.
csi0.pixformat(csi.GRAYSCALE)  # Set pixel format to GRAYSCALE
csi0.framesize(SEARCHING_RESOLUTION)
csi0.snapshot(time=1000)  # Wait for settings take effect.
clock = time.clock()  # Create a clock object to track the FPS.

csi0.auto_gain(False)  # Turn off as it will oscillate.
csi0.auto_exposure(False, exposure_us=EXPOSURE_MICROSECONDS)
csi0.snapshot(time=1000)

# sensor_w and sensor_h are the image sensor raw pixels w/h (x/y are 0 initially).
x, y, sensor_w, sensor_h = csi0.ioctl(csi.IOCTL_GET_READOUT_WINDOW)

while True:
    clock.tick()
    img = csi0.snapshot()

    # We need to find an IR object to track - it's likely to be really bright.
    blobs = img.find_blobs(
        TRACKING_THRESHOLDS,
        area_threshold=SEARCHING_AREA_THRESHOLD,
        pixels_threshold=SEARCHING_PIXEL_THRESHOLD,
    )

    if len(blobs):
        most_dense_blob = max(blobs, key=lambda x: x.density)
        img.draw_rectangle(most_dense_blob.rect)

        def get_mapped_centroid(b):
            # By default the readout window is set the whole sensor pixel array with x/y==0.
            # The resolution you see if produced by taking pixels from the readout window on
            # the camera. The x/y location is relative to the sensor center.
            x, y, w, h = csi0.ioctl(csi.IOCTL_GET_READOUT_WINDOW)

            # The camera driver will try to scale to fit whatever resolution you pass to max
            # width/height that fit on the sensor while keeping the aspect ratio.
            ratio = min(w / float(csi0.width()), h / float(csi0.height()))

            # Reference cx() to the center of the viewport and then scale to the readout.
            mapped_cx = (b.cx - (csi0.width() / 2.0)) * ratio
            # Since we are keeping the aspect ratio there might be an offset in x.
            mapped_cx += (w - (csi0.width() * ratio)) / 2.0
            # Add in our displacement from the sensor center
            mapped_cx += x + (sensor_w / 2.0)

            # Reference cy() to the center of the viewport and then scale to the readout.
            mapped_cy = (b.cy - (csi0.height() / 2.0)) * ratio
            # Since we are keeping the aspect ratio there might be an offset in y.
            mapped_cy += (h - (csi0.height() * ratio)) / 2.0
            # Add in our displacement from the sensor center
            mapped_cy += y + (sensor_h / 2.0)

            return (mapped_cx, mapped_cy)  # X/Y location on the sensor array.

        def center_on_blob(b, res):
            mapped_cx, mapped_cy = get_mapped_centroid(b)

            # Switch to the res (if res was unchanged this does nothing).
            csi0.framesize(res)

            # Construct readout window. x/y are offsets from the center.
            x = int(mapped_cx - (sensor_w / 2.0))
            y = int(mapped_cy - (sensor_h / 2.0))
            w = csi0.width()
            h = csi0.height()

            # Focus on the centroid.
            csi0.ioctl(csi.IOCTL_SET_READOUT_WINDOW, (x, y, w, h))

            # See if we are hitting the edge.
            new_x, new_y, w, h = csi0.ioctl(csi.IOCTL_GET_READOUT_WINDOW)

            # You can use these error values to drive servos to move the camera if you want.
            x_error = x - new_x
            y_error = y - new_y

            if x_error < 0:
                print("-X Limit Reached ", end="")
            if x_error > 0:
                print("+X Limit Reached ", end="")
            if y_error < 0:
                print("-Y Limit Reached ", end="")
            if y_error > 0:
                print("+Y Limit Reached ", end="")

        center_on_blob(most_dense_blob, TRACKING_RESOLUTION)

        # This loop will track the blob at a much higher readout speed and lower resolution.
        while True:
            clock.tick()
            img = csi0.snapshot()

            # Find the blob in the lower resolution image.
            blobs = img.find_blobs(
                TRACKING_THRESHOLDS,
                area_threshold=TRACKING_AREA_THRESHOLD,
                pixels_threshold=TRACKING_PIXEL_THRESHOLD,
            )

            # If we loose the blob then we need to find a new one.
            if not len(blobs):
                # Reset resolution.
                csi0.framesize(SEARCHING_RESOLUTION)
                csi0.ioctl(csi.IOCTL_SET_READOUT_WINDOW, (sensor_w, sensor_h))
                break

            # Narrow down the blob list and highlight the blob.
            most_dense_blob = max(blobs, key=lambda x: x.density)
            img.draw_rectangle(most_dense_blob.rect)

            print(
                clock.fps(), "BLOB cx:%d, cy:%d" % get_mapped_centroid(most_dense_blob)
            )

            x_diff = most_dense_blob.cx - (csi0.width() / 2.0)
            y_diff = most_dense_blob.cy - (csi0.height() / 2.0)

            w_threshold = (csi0.width() / 2.0) * TRACKING_EDGE_TOLERANCE
            h_threshold = (csi0.height() / 2.0) * TRACKING_EDGE_TOLERANCE

            # Re-center on the blob if it starts going out of view (costs FPS).
            if abs(x_diff) > w_threshold or abs(y_diff) > h_threshold:
                center_on_blob(most_dense_blob, TRACKING_RESOLUTION)

    print(clock.fps())

results matching ""

    No results matching ""