Skip to content
⚡ Developer documentation for INFINICAM, the real-time high-speed camera (UC-1.1 / GbE-2)

SingleImage + ROI decoding

This sample decodes only the region you need (ROI: Region of Interest) of a received image. It takes less time than decoding the whole image, so it speeds up processing when you already know where to look. It also compares the time with decoding the whole image.

  1. Enter and save the program

    Save the following code as single_image_roi.py in C:\infinicam_projects\python.

    import time
    import cv2
    from pypuclib import CameraFactory, PUCException
    print("Connecting to the camera...")
    try:
    # Connect to the GbE-2 at this IP address
    cam = CameraFactory().create(ip="192.168.0.10")
    except PUCException as e:
    print("Error: could not connect to the camera.", e)
    raise SystemExit(1)
    try:
    reso = cam.resolution()
    print("Full size:", reso.width, "x", reso.height)
    # Receive the compressed data for one frame
    data = cam.grab()
    decoder = cam.decoder()
    # The region to decode (ROI). All four values must be multiples of 8
    roi_x, roi_y = 200, 152 # Top-left position
    roi_width, roi_height = 400, 296 # Width and height
    print(f"ROI: position ({roi_x}, {roi_y}), size {roi_width} x {roi_height}")
    # Compare the time to decode the whole image with the time to decode only the ROI
    start = time.perf_counter()
    full_image = decoder.decode(data)
    full_ms = (time.perf_counter() - start) * 1000
    start = time.perf_counter()
    roi_image = decoder.decode(data, roi_x, roi_y, roi_width, roi_height)
    roi_ms = (time.perf_counter() - start) * 1000
    finally:
    cam.close()
    print(f"Full decode: {full_ms:.2f} ms / ROI decode: {roi_ms:.2f} ms")
    filename = "roi_captured_image.png"
    cv2.imwrite(filename, roi_image)
    print(f"Saved: {filename} (size: {roi_image.shape[1]} x {roi_image.shape[0]})")
  2. Run it

    Run the following 2 lines, one at a time, in Command Prompt (or PowerShell).

    Terminal window
    cd C:\infinicam_projects\python
    Terminal window
    python single_image_roi.py

    If you see the following and a 400 × 296 image roi_captured_image.png is created, it worked. The decoding times depend on your PC.

    Connecting to the camera...
    Full size: 800 x 600
    ROI: position (200, 152), size 400 x 296
    Full decode: 1.28 ms / ROI decode: 0.25 ms
    Saved: roi_captured_image.png (size: 400 x 296)

roi_image = decoder.decode(data, roi_x, roi_y, roi_width, roi_height)

When you pass decode() the top-left position (x, y), width, and height after the received data, it decodes only that region. The resulting image has the size you specified (400 × 296).

An image decoded by ROI has exactly the same values as the same region cut out of the fully decoded image (full_image[152:448, 200:600]).

start = time.perf_counter()
...
full_ms = (time.perf_counter() - start) * 1000

time.perf_counter() is a clock for precise timing. The difference between its values before and after a step is the time taken (in seconds). Multiplying by 1,000 gives milliseconds (ms).

At 1,000 fps you have only 1 ms to process each frame. If decoding the whole image takes 1.5 ms, that alone means you fall behind.

  • You know where the object will appear (part of a conveyor belt, a window on a machine, and so on)
  • You want to analyze only part of the image in detail

In cases like these, ROI decoding greatly reduces decoding time. You can also combine it with SingleImage + DC component extraction: first find the object’s position in the whole image with the DC components, then decode only the area around it in detail with ROI.