SingleImage + DC Component Extraction
These are the steps to create a program that captures one frame of compressed data from a GbE-2 and extracts and decodes only the DC component (the average brightness of each 8x8 block), quickly generating a lightweight preview image at 1/8 the width and height, and saving it in the PGM format (Portable Graymap).
-
Write the source code
Open the main file (e.g.
GbE2_SingleImage_DC.cpp) and replace its contents with the following code.#include <stdio.h>#include <stdlib.h>#include <PUCLIB.h> // Load the PUCLIB headerint main() {printf("Starting INFINICAM GbE-2 DC component extraction...\n");// 1. Initialize the SDKPUCRESULT ret = PUC_Initialize();if (ret != PUC_SUCCEEDED) {printf("Error: Failed to initialize the SDK. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 2. Search for a GbE-2 device on the networkPUC_DETECT_FILTER filter = { 0 };filter.nDeviceTypeFlags = PUC_DETECT_TARGET_GBE2; // Specify GbE-2PUC_DETECT_INFO detectInfo = { 0 };ret = PUC_DetectDevice2(&filter, &detectInfo);if (ret != PUC_SUCCEEDED || detectInfo.nDeviceCount == 0) {printf("Error: No GbE-2 camera was found. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 3. Open the first detected devicePUC_HANDLE hDevice = NULL;ret = PUC_OpenDevice2(0, &hDevice);if (ret != PUC_SUCCEEDED) {printf("Error: Failed to open the device. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 4. Get the resolution of the full imageUINT32 fullWidth = 0, fullHeight = 0;PUC_GetResolution(hDevice, &fullWidth, &fullHeight);printf("Full resolution: %u x %u pixels\n", fullWidth, fullHeight);// 5. Calculate the DC component image size (becomes a block size that is 1/8 the width and height)UINT32 dcWidth = fullWidth / 8;UINT32 dcHeight = fullHeight / 8;printf("DC image resolution (1/8 scale): %u x %u pixels\n", dcWidth, dcHeight);// 6. Get the transfer data size and allocate buffersUINT32 xferDataSize = 0;PUC_GetXferDataSize(hDevice, &xferDataSize);// Allocate the buffer for the full compressed dataunsigned char* pCompressedBuffer = (unsigned char*)malloc(xferDataSize);// Allocate the buffer for the DC-decoded data (1/64 the pixel size is sufficient)unsigned long dcDecodedSize = (unsigned long)dcWidth * dcHeight;unsigned char* pDcDecodedBuffer = (unsigned char*)malloc(dcDecodedSize);if (pCompressedBuffer == NULL || pDcDecodedBuffer == NULL) {printf("Error: Memory allocation failed.\n");free(pCompressedBuffer);free(pDcDecodedBuffer);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}// 7. Capture one frame's worth of compressed dataPUC_XFER_DATA_INFO xferDataInfo = { 0 };xferDataInfo.pData = pCompressedBuffer;xferDataInfo.nDataSize = xferDataSize;printf("Capturing...\n");ret = PUC_GetSingleXferData(hDevice, &xferDataInfo);if (ret == PUC_SUCCEEDED) {// 8. Extract and decode only the DC component (1/8-scale image)// Arguments: output buffer, start block X, start block Y, number of blocks X, number of blocks Y, compressed dataret = PUC_DecodeDCData(pDcDecodedBuffer, 0, 0, dcWidth, dcHeight, pCompressedBuffer);if (ret == PUC_SUCCEEDED) {// 9. Save the extracted DC image in PGM (P5) formatconst char* filename = "dc_captured_image.pgm";FILE* fp = NULL;if (fopen_s(&fp, filename, "wb") == 0 && fp != NULL) {// Write the PGM header (use the DC image size for the width and height)fprintf(fp, "P5\n%u %u\n255\n", dcWidth, dcHeight);// Write the DC-decoded datafwrite(pDcDecodedBuffer, sizeof(unsigned char), dcDecodedSize, fp);fclose(fp);printf("SUCCESS: Saved the DC component extraction image (PGM) (%s)\n", filename);} else {printf("Error: Failed to save the file.\n");}} else {printf("Error: Failed to decode the DC component. ErrorCode: %d\n", ret);}} else {printf("Error: Failed to capture the image data. ErrorCode: %d\n", ret);}// 10. Free the memory and close the devicefree(pCompressedBuffer);free(pDcDecodedBuffer);PUC_CloseDevice(hDevice);return EXIT_SUCCESS;} -
Build and run
- Build with
Ctrl+Shift+B. - Run with
Ctrl+F5. - If
dc_captured_image.pgm, scaled down to 1/8 the width and height, is output in the execution directory, it has succeeded.
- Build with
Benefits and Use Cases of DC Component Extraction (Fast Preview)
Section titled “Benefits and Use Cases of DC Component Extraction (Fast Preview)”Extremely fast, low-load preview generation
Section titled “Extremely fast, low-load preview generation”With INFINICAM’s fixed-length compression (DCT-based), the leading data of each 8x8 block (the DC component) contains the average brightness information for that entire block.
Because the DC component can be extracted without performing the complex decoding computation of an inverse DCT, the computational cost is extremely low, allowing a thumbnail of the full image (a 1/8-scale image) to be generated instantly with almost no CPU load.
Main use cases
Section titled “Main use cases”- Real-time monitor display: When you want to keep CPU resources focused on primary processing and analysis, while still drawing a lightweight preview for a monitoring screen
- Detecting the position of a Region of Interest (ROI): Building a pipeline that uses fast DC expansion to detect the position of an object within the full image, then fully decodes only the identified region using ROI decoding
