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.
-
Enter and save the program
Save the following code as
opencv_process.pyinC:\infinicam_projects\python.import cv2import numpy as npfrom pypuclib import CameraFactory, PUCExceptionprint("Connecting to the camera...")try:# Connect to the GbE-2 at this IP addresscam = 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 processingif 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 windowresult = 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") -
Run it
Run the following 2 lines, one at a time, in Command Prompt (or PowerShell).
Terminal window cd C:\infinicam_projects\pythonTerminal window python opencv_process.pyA 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.pngis saved.Connecting to the camera...Select the window and press any key to exit.Saved: opencv_edges_result.png
How the program works
Section titled “How the program works”1. Blur (Gaussian filter)
Section titled “1. Blur (Gaussian filter)”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.
2. Edge detection (Canny method)
Section titled “2. Edge detection (Canny method)”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.
3. Show images side by side
Section titled “3. Show images side by side”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, andcv2.waitKey(0)waits until a key is pressed (0means “wait forever”).
