SingleImage + DC component extraction
This sample extracts only the DC components from INFINICAM’s compressed data to build an image scaled down to 1/8 in each direction. It takes far less time than decoding the whole image, and still gives you a rough picture of the whole scene. It also compares the time with decoding the whole image.
-
Enter and save the program
Save the following code as
single_image_dc.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()# In the DC image, each 8 x 8 block of pixels becomes one pixelblocks_x = reso.width // 8 # Number of blocks acrossblocks_y = reso.height // 8 # Number of blocks down# Compare the time to decode the whole image with the time to extract only the DC componentsstart = time.perf_counter()full_image = decoder.decode(data)full_ms = (time.perf_counter() - start) * 1000start = time.perf_counter()dc_image = decoder.decodeDC(data, 0, 0, blocks_x, blocks_y)dc_ms = (time.perf_counter() - start) * 1000finally:cam.close()print(f"Full decode: {full_ms:.2f} ms / DC extraction: {dc_ms:.2f} ms")filename = "dc_captured_image.png"cv2.imwrite(filename, dc_image)print(f"Saved: {filename} (size: {dc_image.shape[1]} x {dc_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_dc.pyIf you see the following and a small 100 × 75 image
dc_captured_image.pngis created, it worked. The times depend on your PC.Connecting to the camera...Full size: 800 x 600Full decode: 1.25 ms / DC extraction: 0.18 msSaved: dc_captured_image.png (size: 100 x 75)
How the program works
Section titled “How the program works”1. What DC components are
Section titled “1. What DC components are”INFINICAM compresses images in 8 × 8 pixel blocks. For each block, the compressed data holds a value representing “the brightness of the whole block”, called the DC component.
Lining up just the DC components gives an image scaled down to 1/8 in each direction, with one pixel per block (100 × 75 for 800 × 600). Fine patterns are lost, but you can still tell what is where.
2. Extract the DC components
Section titled “2. Extract the DC components”dc_image = decoder.decodeDC(data, 0, 0, blocks_x, blocks_y)The 4 numbers passed to decodeDC() are in blocks, not pixels.
| Argument | Meaning | In this example |
|---|---|---|
| Second and third | Block position to start from (horizontal, vertical) | 0, 0 (from the top left) |
| Fourth and fifth | Number of blocks to extract (horizontal, vertical) | 100, 75 (the whole image) |
DC components can be extracted without the detailed decoding calculations, so this takes much less time than decoding the whole image.
When DC components help
Section titled “When DC components help”- Display: show a lightweight scaled-down live view and leave the PC’s power for analysis
- Rough detection: quickly find the approximate position of bright or moving objects in the whole image
- Combined with ROI: find an object’s position with DC components first, then decode only the area around it in detail with ROI decoding
