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

OpenCV image processing & display

This sample captures one image from the INFINICAM GbE-2 and processes it with OpenCV. It blurs the image to reduce noise, then runs edge detection (finding the “outlines” where brightness changes sharply), and shows the result next to the original image in a window.

  1. Enter and save the program

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

    import cv2
    import numpy as np
    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:
    # Receive one frame of compressed data and decode it into an image (a NumPy array)
    image = cam.decoder().decode(cam.grab())
    finally:
    cam.close()
    # With a color camera, convert the image to grayscale before processing
    if image.ndim == 3:
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 1. Blur (reduces fine noise)
    blurred = cv2.GaussianBlur(image, (5, 5), 0)
    # 2. Edge detection (finds "outlines" where the brightness changes sharply)
    edges = cv2.Canny(blurred, 50, 150)
    # Show the original image and the edge image side by side in one window
    result = np.hstack((image, edges))
    cv2.imshow("Original (left) / Edges (right) - press any key", result)
    print("Select the window and press any key to exit.")
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.imwrite("opencv_edges_result.png", result)
    print("Saved: opencv_edges_result.png")
  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 opencv_process.py

    A window opens showing the original image on the left and the edges (white lines) on the right. Click the window to select it, then press any key: the window closes and opencv_edges_result.png is saved.

    Connecting to the camera...
    Select the window and press any key to exit.
    Saved: opencv_edges_result.png

blurred = cv2.GaussianBlur(image, (5, 5), 0)

This blurs the image slightly using the surrounding 5 × 5 pixels. Camera images contain fine graininess (noise). If you run edge detection as is, the noise also gets picked up as outlines, so you blur first. A larger (5, 5) gives a stronger blur.

edges = cv2.Canny(blurred, 50, 150)

This extracts places where the brightness changes sharply as white lines (value 255). The 2 numbers are thresholds: places where the brightness changes strongly (150 or more) become edges, and slightly weaker places (50 or more) connected to them are included too.

result = np.hstack((image, edges))
cv2.imshow("...", result)
cv2.waitKey(0)
  • np.hstack() joins NumPy arrays side by side. Images of the same height can be placed next to each other as they are.
  • cv2.imshow() shows the image in a window, and cv2.waitKey(0) waits until a key is pressed (0 means “wait forever”).