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

OpenCV Image Processing & Display (GbE-2 / Python)

Steps for creating a program that uses the common module puclib.py created in HelloWorld to apply filtering (noise reduction, edge detection) to a single frame of image data captured from the GbE-2 using OpenCV (cv2), then displays it on screen and saves the result.

  1. Create the script

    In the same folder as puclib.py, create gbe2_opencv_single_process.py and write the following code.

    gbe2_opencv_single_process.py
    from ctypes import c_uint32, c_ushort, c_ubyte, c_void_p, byref
    import numpy as np
    import cv2
    # Import from the common module puclib.py
    from puclib import (
    load_sdk, PUC_SUCCEEDED, PUC_DETECT_TARGET_GBE2,
    PUC_Q_COUNT, PUC_DETECT_FILTER, PUC_DETECT_INFO, PUC_XFER_DATA_INFO
    )
    def main():
    print("Starting INFINICAM GbE-2 OpenCV image-processing sample...")
    # 1. Load and initialize the SDK
    puclib = load_sdk()
    if puclib.PUC_Initialize() != PUC_SUCCEEDED:
    print("Error: Failed to initialize the SDK.")
    return
    # 2. Search for and open the device
    detect_filter = PUC_DETECT_FILTER(nDeviceTypeFlags=PUC_DETECT_TARGET_GBE2)
    detect_info = PUC_DETECT_INFO()
    if puclib.PUC_DetectDevice2(byref(detect_filter), byref(detect_info)) != PUC_SUCCEEDED or detect_info.nDeviceCount == 0:
    print("Error: No GbE-2 camera was found.")
    return
    h_device = c_void_p()
    if puclib.PUC_OpenDevice2(0, byref(h_device)) != PUC_SUCCEEDED:
    print("Error: Failed to open the device.")
    return
    # 3. Retrieve the resolution, quantization table, and transfer size
    width, height = c_uint32(), c_uint32()
    puclib.PUC_GetResolution(h_device, byref(width), byref(height))
    w, h = width.value, height.value
    q_vals = (c_ushort * PUC_Q_COUNT)()
    puclib.PUC_GetQuantization2(h_device, q_vals)
    xfer_data_size = c_uint32()
    puclib.PUC_GetXferDataSize(h_device, byref(xfer_data_size))
    print(f"Resolution: {w} x {h} pixels")
    # 4. Allocate buffers
    comp_buffer = (c_ubyte * xfer_data_size.value)()
    line_bytes = (w + 3) & ~3 # 4-byte boundary alignment
    decoded_buffer = (c_ubyte * (line_bytes * h))()
    # 5. Capture one frame of data
    xfer_info = PUC_XFER_DATA_INFO()
    xfer_info.pData = comp_buffer
    xfer_info.nDataSize = xfer_data_size.value
    print("Capturing one frame...")
    if puclib.PUC_GetSingleXferData(h_device, byref(xfer_info)) == PUC_SUCCEEDED:
    # 6. Decode processing
    ret = puclib.PUC_DecodeData(
    decoded_buffer, 0, 0, w, h, line_bytes, comp_buffer, q_vals
    )
    if ret == PUC_SUCCEEDED:
    # 7. Convert into a NumPy array (2D monochrome image)
    img_raw = np.ctypeslib.as_array(decoded_buffer).reshape((h, line_bytes))[:, :w]
    # 8. OpenCV image processing
    # Noise reduction with a Gaussian filter
    img_blur = cv2.GaussianBlur(img_raw, (5, 5), 0)
    # Edge detection with the Canny algorithm
    img_edges = cv2.Canny(img_blur, 50, 150)
    # Concatenate the original image and the edge-detection result side by side
    img_combined = np.hstack((img_raw, img_edges))
    # 9. Save the edge-detection result
    filename = "opencv_edges_result.png"
    cv2.imwrite(filename, img_edges)
    print(f"SUCCESS: Saved the edge-detection result ({filename})")
    # 10. Display in an OpenCV window (press any key to exit)
    window_name = "Original Image (Left) vs Canny Edges (Right)"
    cv2.imshow(window_name, img_combined)
    print("Displaying the image... Press any key to exit.")
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    else:
    print(f"Error: Failed to decode the image data. ErrorCode: {ret}")
    else:
    print("Error: Failed to retrieve the image data.")
    # 11. Close
    puclib.PUC_CloseDevice(h_device)
    if __name__ == "__main__":
    main()
  2. Run the script

    Terminal window
    python gbe2_opencv_single_process.py

    When run, an OpenCV display window opens showing the original image on the left and the edges extracted with the Canny algorithm on the right. Pressing any key closes the window, and opencv_edges_result.png is saved in the current directory.


Applying a smoothing filter before edge detection suppresses false detections caused by fine sensor noise during high-sensitivity capture, enabling stable contour rendering.

img_blur = cv2.GaussianBlur(img_raw, (5, 5), 0)

By specifying two thresholds (threshold1, threshold2), only the boundaries (contours) with sharp brightness changes are extracted.

img_edges = cv2.Canny(img_blur, 50, 150)

3. Combining images side by side (np.hstack)

Section titled “3. Combining images side by side (np.hstack)”

As long as NumPy arrays have the same width, height, and channel count, np.hstack (horizontal concatenation) or np.vstack (vertical concatenation) can be used to combine multiple stages of processing results into a single display window.