Sample code¶
Look up table (LUT) example¶
#include "stdafx.h"
#ifdef WIN32
#include "xiApi.h"
#else
#include <m3api/xiApi.h>
#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[])
{
XI_IMG image;
memset(&image,0,sizeof(image));
image.size = sizeof(XI_IMG);
HANDLE xiH = NULL;
XI_RETURN stat = XI_OK;
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");
stat = xiSetParamInt(xiH, XI_PRM_EXPOSURE, 10000);
HandleResult(stat,"xiSetParam (exposure set)");
printf("Starting acquisition...\n");
stat = xiStartAcquisition(xiH);
HandleResult(stat,"xiStartAcquisition");
#define EXPECTED_IMAGES 100
for (int images=0;images < EXPECTED_IMAGES;images++)
{
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¶
#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[])
{
XI_IMG image;
memset(&image,0,sizeof(image));
image.size = sizeof(XI_IMG);
HANDLE xiH = NULL;
XI_RETURN stat = XI_OK;
printf("Opening first camera...\n");
stat = xiOpenDevice(0, &xiH);
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)");
XI_IMG_FORMAT format=XI_RAW8;
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);
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++)
{
stat=xiGetImage(xiH, 2000, &image);
HandleResult(stat,"xiGetImage");
}
clock_t end_time=clock();
stat=xiStopAcquisition(xiH);
HandleResult(stat,"xiStopAcquisition");
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);
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);
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:
if (xiH)
xiCloseDevice(xiH);
printf("Done\n");
#ifdef WIN32
Sleep(10000);
#endif
return 0;
}