Example of acquisition from Camera API
This example shows how to acquire data from a sensor using perClass Mira Camera API. The sensor used is Headwall MV.C VNIR. The example is generic and should work unchanged with other line-scan cameras when linked to the respective perClass acquisition plugin.
The example shows how to:
- inquire on the version of acquisition library (line 13)
- initialize the acquisition plugin (line 20)
- scan for available devices and return their names (line 25-31)
- open a device (line 33)
- test if a device is a line-scan or snapshot (line 36)
- setup wavelength resampling to specific output wavelengths irrespective of a device (line 43-49)
- initialize acquisition (line 51)
- query frame geometry, data type and layout (60-72)
- set exposure and frame rate (lines 74-76)
- acquire 100 frames, store data in memory (line 94-100)
In order to compile the example with command line Microsoft Visual C/C++ compiler use:
> cl ex05.c -I "C:\Program Files\perClass Mira\lib" "C:\Program Files\perClass Mira\lib\miraacq_ximea_1.7.1.lib"
Note, that we point to the lib sub-dir of perClass Mira installation for includes and directly link with the acuisition plugin for your camera (Ximea plugin for MV.C VNIR).
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include "miraacq.h"
- int main(int argc,const char* argv[])
- {
- int res=MIRA_OK;
- uint16_t* pBuf=NULL;
- FILE* fid=NULL;
- int api,rev,step;
- const char* str=miraacq_GetAPIVersion(&api,&rev,&step);
- printf("Example of acquiring data using perClass Mira Acquisition Plugin\nVersion: %d.%d.%d (%s)\n",api,rev,step,str);
- const char* str2=miraacq_GetVersion();
- printf("Version: '%s'\n",str2);
- makernel* pma=miraacq_Init(".");
- printf("Init: %s", miraacq_GetErrorMsg(pma));
- if( pma==NULL ) {
- goto Error;
- }
- MIRAACQ_CHECK( miraacq_ScanDevices(pma) );
- const int devCount=miraacq_GetDeviceCount(pma);
- printf("\n%d devices:\n",devCount);
- for(int i=0;i<devCount;i++) {
- printf("%d : %s\n",i, miraacq_GetDeviceName(pma,i));
- }
- int deviceInd=0;
- MIRAACQ_CHECK( miraacq_OpenDevice(pma,deviceInd) );
- printf("Device opened: %d '%s'\n",deviceInd,miraacq_GetDeviceName(pma,deviceInd));
- int isSnapshot=miraacq_DeviceIsSnapshot(pma);
- if( isSnapshot ) {
- printf("Line-scan device required by this example\n");
- MIRAACQ_CHECK( miraacq_CloseDevice(pma,deviceInd) );
- goto Error;
- }
- printf("Setting resampling from 400 to 1000, step 2\n");
- int bands=(1000-400)/2;
- MIRAACQ_CHECK( miraacq_SetResamplingWavelengthCount(pma,bands) );
- for(int i=0;i<bands;i++) {
- MIRAACQ_CHECK( miraacq_SetResamplingWavelength(pma,i,400+(2*i)) );
- }
- MIRAACQ_CHECK( miraacq_SetResampling(pma,1) );
- printf("Initializing the acquisition...");
- res=miraacq_InitializeAcquisition(pma);
- printf("done res=%d\n",res);
- fflush(0);
- if( res!=MIRA_OK ) {
- MIRAACQ_CHECK( miraacq_CloseDevice(pma,deviceInd) );
- goto Error;
- }
- printf("Geometry: width=%d bands=%d lines=%d dataType=%d dataLayout=%d frameSize=%d\n",
- miraacq_GetFrameWidth(pma),
- miraacq_GetFrameBands(pma),
- miraacq_GetFrameHeight(pma),
- miraacq_GetFrameDataType(pma),
- miraacq_GetFrameDataLayout(pma),
- miraacq_GetFrameSize(pma) );
- if( miraacq_GetFrameDataType(pma)!=ACQ_DATATYPE_UINT16 ) {
- printf("UINT16 data type expected by this example\n");
- MIRAACQ_CHECK(miraacq_CloseDevice(pma,deviceInd));
- goto Error;
- }
- MIRAACQ_CHECK( miraacq_SetExposure(pma,4.0) );
- MIRAACQ_CHECK( miraacq_SetFrameRate(pma,100.0) );
- const int frames=100;
- int frameSize=miraacq_GetFrameSize(pma);
- pBuf=malloc( frames*frameSize );
- if( pBuf==NULL ) {
- MIRAACQ_CHECK(miraacq_CloseDevice(pma,deviceInd));
- goto Error;
- }
- MIRAACQ_CHECK( miraacq_StartAcquisition(pma) );
- uint16_t* ptr=pBuf;
- size_t frameID=0;
- // offset to the next frame in uint16 units (a frame has width x bands units)
- int nextFrameOffset= miraacq_GetFrameWidth(pma)*miraacq_GetFrameBands(pma);
- printf("Acquiring %d frames:\n",frames);
- for(int i=0;i<frames;i++) {
- MIRAACQ_CHECK( miraacq_GetFrame(pma,ptr,&frameID,1000) );
- ptr+=nextFrameOffset;
- }
- printf("Stopping acquisition\n");
- MIRAACQ_CHECK( miraacq_StopAcquisition(pma) );
- printf("Writing data to file:\n");
- FILE* pFile=fopen("out.bin","wb");
- if( pFile==NULL ) {
- printf("Cannot open file for writing\n");
- MIRAACQ_CHECK(miraacq_CloseDevice(pma,deviceInd));
- goto Error;
- }
- size_t countWritten=fwrite(pBuf,(size_t)frameSize,frames,pFile);
- printf("%zu bytes written to file\n",countWritten*frameSize);
- fclose(pFile);
- Error:
- if( res!=MIRA_OK ) {
- printf("Error %d: %s",miraacq_GetErrorCode(pma),miraacq_GetErrorMsg(pma));
- }
- miraacq_Release(pma);
- if( pBuf!=NULL) free(pBuf);
- return 0;
- }