Loading
 
Support Support Home » APIs » XiAPI » Sample code

Sample code

Look up table (LUT) example

// xiSample.cpp

#include "stdafx.h" 

#ifdef WIN32
#include "xiApi.h"       // Windows
#else
#include <m3api/xiApi.h> // Linux, OSX
#endif
#include <memory.h>

#define HandleResult(res,place) if (res!=XI_OK) {printf("Error after %s (%d)\n",place,res);goto finish;}

int _tmain(int argc, _TCHAR* argv[])
{
    // image buffer
    XI_IMG image;
    memset(&image,0,sizeof(image));
    image.size = sizeof(XI_IMG);

    // Sample for XIMEA API V4.05
    HANDLE xiH = NULL;
    XI_RETURN stat = XI_OK;

    // Retrieving a handle to the camera device 
    printf("Opening first camera...\n");
    stat = xiOpenDevice(0, &xiH);
    HandleResult(stat,"xiOpenDevice");

    int maxIndex, minIndex;
    int maxValue, minValue;

    stat = xiGetParamInt(xiH, XI_PRM_LUT_INDEX XI_PRM_INFO_MIN, &minIndex);
    HandleResult(stat,"xiGetParam");
    stat = xiGetParamInt(xiH, XI_PRM_LUT_INDEX XI_PRM_INFO_MAX, &maxIndex);
    HandleResult(stat,"xiGetParam");
    stat = xiGetParamInt(xiH, XI_PRM_LUT_VALUE XI_PRM_INFO_MIN, &minValue);
    HandleResult(stat,"xiGetParam");
    stat = xiGetParamInt(xiH, XI_PRM_LUT_VALUE XI_PRM_INFO_MAX, &maxValue);
    HandleResult(stat,"xiGetParam");

    printf("max index %d \n", maxIndex);
    printf("max value %d \n", maxValue);

    for (int i = 0; i < maxIndex; i++){
            stat = xiSetParamInt(xiH, XI_PRM_LUT_INDEX, i);
            HandleResult(stat,"xiSetParam");
            stat = xiSetParamInt(xiH, XI_PRM_LUT_VALUE, maxValue - i);
            HandleResult(stat,"xiSetParam");
        }

    stat = xiSetParamInt(xiH, XI_PRM_LUT_EN, 1);
    HandleResult(stat,"xiSetParamLUT");

    // Setting "exposure" parameter (10ms=10000us)
    stat = xiSetParamInt(xiH, XI_PRM_EXPOSURE, 10000);
    HandleResult(stat,"xiSetParam (exposure set)");

    // Note:
    // The default parameters of each camera might be different in different API versions
    // In order to ensure that your application will have camera in expected state,
    // please set all parameters expected by your application to required value.

    printf("Starting acquisition...\n");
    stat = xiStartAcquisition(xiH);
    HandleResult(stat,"xiStartAcquisition");

    #define EXPECTED_IMAGES 100
    for (int images=0;images < EXPECTED_IMAGES;images++)
    {
        // getting image from camera
        stat = xiGetImage(xiH, 5000, &image);
        HandleResult(stat,"xiGetImage");
        unsigned char pixel = *(unsigned char*)image.bp;
        printf("Image %d (%dx%d) received from camera. First pixel value: %d\n", images, (int)image.width, (int)image.height, pixel);
    }

    printf("Stopping acquisition...\n");
    xiStopAcquisition(xiH);
    xiCloseDevice(xiH);
finish:
    printf("Done\n");
#ifdef WIN32
    Sleep(2000);
#endif
    return 0;
}

Performance measuring example

// xiCameraPerformaceCheck.cpp : program that measures the maximum frame rate for the current setup
//

#include "stdafx.h" 

#ifdef WIN32
#include "xiApi.h" 
#else
#include <m3api/xiApi.h>
#endif

#include <time.h>    

#define HandleResult(res,place) if (res!=XI_OK) {printf("Error after %s (%d)",place,res);goto finish;}

int _tmain(int argc, _TCHAR* argv[])
{
    // image buffer
    XI_IMG image;
    memset(&image,0,sizeof(image));
    image.size = sizeof(XI_IMG);

    // camera handle
    HANDLE xiH = NULL;
    XI_RETURN stat = XI_OK;

    // disabling auto bandwidth calculation     

    HandleResult(stat,"xiSetParam (XI_PRM_AUTO_BANDWIDTH_CALCULATION)");

    // Retrieving a handle to the camera device 
    printf("Opening first camera...\n");
    stat = xiOpenDevice(0, &xiH);

    // Reading out bandwidth info
    int available_bandwidth=0;
    stat = xiGetParamInt(xiH, XI_PRM_AVAILABLE_BANDWIDTH, &available_bandwidth);
    HandleResult(stat,"xiGetParam (XI_PRM_AVAILABLE_BANDWIDTH)");
#define BANDWIDTH_RATIO 0.90
    stat = xiSetParamInt(xiH, XI_PRM_LIMIT_BANDWIDTH,(int)(BANDWIDTH_RATIO*available_bandwidth));
    HandleResult(stat,"xiGetParam (XI_PRM_LIMIT_BANDWIDTH)");

    // Setting up the acquisition

    XI_IMG_FORMAT format=XI_RGB32;
    int output_bitdepth=8;

    stat = xiSetParamInt(xiH, XI_PRM_IMAGE_DATA_FORMAT,format);
    HandleResult(stat,"xiSetParam (XI_PRM_IMAGE_DATA_FORMAT)");

    stat = xiSetParamInt(xiH, XI_PRM_OUTPUT_DATA_BIT_DEPTH,output_bitdepth);
    HandleResult(stat,"xiSetParam (image format)");

        stat = xiSetParamInt(xiH, XI_PRM_EXPOSURE,100);
    HandleResult(stat,"xiSetParam (XI_PRM_EXPOSURE)");

    printf("Measuring performance for image_data_format =  %d output_bitdepth = %d with bandwidth limit set to %d Mbit/s  (available bandwidth is %d Mbit/s)\n\n",(int)format,output_bitdepth,(int)(BANDWIDTH_RATIO*available_bandwidth),available_bandwidth);

    // Start measurement

    stat=xiStartAcquisition(xiH);
    HandleResult(stat,"xiStartAcquisition");
    clock_t start_time=clock();
#define NUMBER_OF_FRAMES 500
    for (int frame=0;frame< NUMBER_OF_FRAMES;frame++)
    {
        // getting image from camera
        stat=xiGetImage(xiH, 2000, &image);
        HandleResult(stat,"xiGetImage");
    }
    clock_t end_time=clock();

    stat=xiStopAcquisition(xiH);
    HandleResult(stat,"xiStopAcquisition");

    // Calculating performance parameters

    double t_sec = (double)(end_time - start_time)/CLOCKS_PER_SEC;

    int transport_skipped=0;int transport_transferred=0;int api_skipped=0;

    stat = xiSetParamInt(xiH, XI_PRM_COUNTER_SELECTOR,XI_CNT_SEL_TRANSPORT_SKIPPED_FRAMES); // Number of skipped frames on transport layer (e.g. when image gets lost while transmission). Occur when capacity of transport channel does not allow to transfer all data.
    HandleResult(stat,"xiSetParam (XI_PRM_COUNTER_SELECTOR)");
    stat = xiGetParamInt(xiH, XI_PRM_COUNTER_VALUE,&transport_skipped);
    HandleResult(stat,"xiGetParamInt (XI_PRM_COUNTER_VALUE)");

    stat = xiSetParamInt(xiH, XI_PRM_COUNTER_SELECTOR,XI_CNT_SEL_TRANSPORT_TRANSFERRED_FRAMES);
    HandleResult(stat,"xiSetParam (XI_PRM_COUNTER_SELECTOR)");
    stat = xiGetParamInt(xiH, XI_PRM_COUNTER_VALUE,&transport_transferred);
    HandleResult(stat,"xiGetParamInt (XI_PRM_COUNTER_VALUE)");

    stat = xiSetParamInt(xiH, XI_PRM_COUNTER_SELECTOR,XI_CNT_SEL_API_SKIPPED_FRAMES);// Number of skipped frames on API layer. Occur when application does not process the images as quick as they are received from the camera.
    HandleResult(stat,"xiSetParam (XI_PRM_COUNTER_SELECTOR)");
    stat = xiGetParamInt(xiH, XI_PRM_COUNTER_VALUE,&api_skipped);
    HandleResult(stat,"xiGetParamInt (XI_PRM_COUNTER_VALUE)");

    printf("proccessed    %d images at %.2f fps\n", NUMBER_OF_FRAMES,NUMBER_OF_FRAMES/t_sec); 
    printf("transferred    %d images at %.2f fps\n", transport_transferred,transport_transferred/t_sec); 
    printf("images lost on transport    %d\n", transport_skipped);
    printf("images skipped by api        %d\n", api_skipped);

finish:
    // Close device
    if (xiH)
        xiCloseDevice(xiH);
    printf("Done\n");
#ifdef WIN32
    Sleep(10000);
#endif
    return 0;
}