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.
-
Enter and save the program
Save the following code as
single_image_roi.pyinC:\infinicam_projects\python.import timeimport cv2from 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:reso = cam.resolution()print("Full size:", reso.width, "x", reso.height)# Receive the compressed data for one framedata = cam.grab()decoder = cam.decoder()# The region to decode (ROI). All four values must be multiples of 8roi_x, roi_y = 200, 152 # Top-left positionroi_width, roi_height = 400, 296 # Width and heightprint(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 ROIstart = time.perf_counter()full_image = decoder.decode(data)full_ms = (time.perf_counter() - start) * 1000start = time.perf_counter()roi_image = decoder.decode(data, roi_x, roi_y, roi_width, roi_height)roi_ms = (time.perf_counter() - start) * 1000finally: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]})") -
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 single_image_roi.pyIf you see the following and a 400 × 296 image
roi_captured_image.pngis created, it worked. The decoding times depend on your PC.Connecting to the camera...Full size: 800 x 600ROI: position (200, 152), size 400 x 296Full decode: 1.28 ms / ROI decode: 0.25 msSaved: roi_captured_image.png (size: 400 x 296)
How the program works
Section titled “How the program works”1. Decode a specified region
Section titled “1. Decode a specified region”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]).
2. Measure the time
Section titled “2. Measure the time”start = time.perf_counter()...full_ms = (time.perf_counter() - start) * 1000time.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).
When ROI decoding helps
Section titled “When ROI decoding helps”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.
