Version 8.8.5
 
Loading...
Searching...
No Matches
examples/linux/externalProbe/IRExternalProbe.cpp

Linux example: This example shows the usage of the external reference probe BR 20AR.

Linux example: This example shows the usage of the external reference probe BR 20AR.

/******************************************************************************
* Example showing the usage of the library with a external referece probe *
* -------------------------------------------------------- *
* This example shows the usage of the external BR20AR. The comparison of *
* the temperature values happens within the onThermalFrame callback. *
* The temperature values of the BR20AR itself can be accessed through the *
* first pif analog input value in °C, if use_external_probe in the *
* configuration file is set to 1. *
******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <signal.h>
// Optris device interfaces
#include "IRDevice.h"
// Optris imager interfaces
#include "IRImager.h"
// Optris logging interface
#include "IRLogger.h"
// Optris raw image file writer
#include "IRFileWriter.h"
// evo Frame Rate Counter
#include "FramerateCounter.h"
// Image converter
#include "ImageBuilder.h"
//--Code for displaying image -----------------
#include <opencv2/opencv.hpp>
using namespace std;
using namespace evo;
unsigned int measurementFieldTopLeftU = 10;
unsigned int measurementFieldTopLeftV = 10;
unsigned int measurementFieldWidth = 100;
unsigned int measurementFieldHeight = 100;
bool _keepCapturing = true;
IRImager _imager;
unsigned int _lastReferenceCounterHW = 0;
float _tempAvgMeasurementField = 0;
float _tempAvgProbe = 0;
char _measurementsCount = 0;
unsigned char *_paletteImage = NULL;
void sigHandler(int dummy = 0)
{
_keepCapturing = false;
}
void onThermalFrame(unsigned short *thermal, unsigned int w, unsigned int h, IRFrameMetadata meta, void *arg)
{
cout << "Frameid: " << meta.counter << endl;
if (meta.pifAIs.size() == 0)
{
std::cout << "Please enable external probe through setting IRDeviceParams::useExternalProbeForReferencing to true!" << std::endl;
return;
}
//Temperature of external probe (BR20AR) is accessible on first analog input, if IRDeviceParams::useExternalProbeForReferencing is set to true
float tempExternalProbe = meta.pifAIs[0];
//check for inavalid temperature
if (tempExternalProbe > 511)
{
std::cout << "Check connection to external probe!" << std::endl;
return;
}
// Running average of probe
_tempAvgProbe = (_tempAvgProbe * 4 + tempExternalProbe) / 5.f;
cout << "Reference Temperature of external Probe: " << tempExternalProbe << " °C" << endl;
//create palette image
evo::ImageBuilder iBuilder(true, _imager.getTemprangeDecimal());
iBuilder.setData(w, h, thermal);
unsigned int paletteWidth = iBuilder.getStride();
unsigned int paletteHeight = h;
if (_paletteImage == NULL)
_paletteImage = new unsigned char[paletteWidth * h * 3];
iBuilder.convertTemperatureToPaletteImage(_paletteImage);
//--Code for displaying image -----------------
cv::Mat cv_img(cv::Size(paletteWidth, paletteHeight), CV_8UC3, &_paletteImage[0], cv::Mat::AUTO_STEP);
//draw red box to visualize reference measurement field
cv::rectangle(cv_img,
cv::Rect2i(measurementFieldTopLeftU, measurementFieldTopLeftV, measurementFieldWidth, measurementFieldHeight),
cv::Scalar(255, 0, 0));
cv::cvtColor(cv_img, cv_img, CV_BGR2RGB);
cv::imshow("palette image", cv_img);
//only if flag is open and temperature values are available
if (meta.flagState == 0 && thermal[0] > 0 && thermal[0] != 0xcdcd)
{
if (_measurementsCount < 5)
_measurementsCount++;
//calculate mean temp of external probe:
float meanTempMeasurementField = iBuilder.getMeanTemperature(
measurementFieldTopLeftU, measurementFieldTopLeftV, measurementFieldTopLeftU + measurementFieldWidth, measurementFieldTopLeftV + measurementFieldHeight);
//Running average of measurement field
_tempAvgMeasurementField = (std::abs(_tempAvgMeasurementField - meanTempMeasurementField) < 0.5f) ? ((_tempAvgMeasurementField * 4 + meanTempMeasurementField) / 5.f) : meanTempMeasurementField;
float tempDiff = std::abs(_tempAvgMeasurementField - _tempAvgProbe);
cout << "Camera avg. Temperature of external probe: " << _tempAvgMeasurementField << endl;
//calculate time diff since last reference using average frame time and hardware frame count
long long avgTimePerFrame = _imager.getAvgTimePerFrame(); // Time is in 1/100 nano seconds
long long diffTime = avgTimePerFrame * ((long int)meta.counterHW - _lastReferenceCounterHW);
//set wait interval between twe references
const long long waitInterval = 500 /*ms*/ * 1000 * 10; // Time is in 1/100 nano seconds -> Wait 0.5s before next reference
//set new reference temperatur if temperature difference is too big and last time exceeds wait time
if (_measurementsCount > 4 && (diffTime < 0 || diffTime > waitInterval) && tempDiff >= ((_imager.getTemprangeDecimal() <= 1) ? 0.2f : 0.04f))
{
cout << "setReferenceTemperature: " << tempDiff << " " << _tempAvgProbe << " " << _tempAvgMeasurementField << endl;
_imager.setReferenceTemperature(_tempAvgProbe, _tempAvgMeasurementField);
_lastReferenceCounterHW = meta.counterHW;
}
}
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
cout << "usage: " << argv[0] << " <xml configuration file>" << endl;
return -1;
}
signal(SIGINT, sigHandler);
IRLogger::setVerbosity(IRLOG_ERROR, IRLOG_OFF);
IRDeviceParamsReader::readXML(argv[1], params);
IRDevice *dev = IRDevice::IRCreateDevice(params);
if (dev)
{
if (_imager.init(&params, dev->getFrequency(), dev->getWidth(), dev->getHeight(), dev->controlledViaHID()))
{
if (_imager.getWidth() != 0 && _imager.getHeight() != 0)
{
cout << "Thermal channel: " << _imager.getWidth() << "x" << _imager.getHeight() << "@" << _imager.getMaxFramerate() << "Hz" << endl;
if (!dev)
{
cout << "NO DEV" << endl;
exit(1);
}
// Start UVC streaming
if (dev->isOpen())
{
// Enter loop in order to pass raw data to Optris image processing library.
// Processed data are supported by the frame callback function.
double timestamp;
unsigned char *bufferRaw = new unsigned char[dev->getRawBufferSize()];
char nmea[GPSBUFFERSIZE];
memset(nmea, 0, GPSBUFFERSIZE * sizeof(*nmea));
_imager.forceFlagEvent(1000.0);
_imager.setThermalFrameCallback(onThermalFrame);
dev->startStreaming();
do
{
dev->getFrame(bufferRaw);
_imager.process(bufferRaw, NULL);
} while (cv::waitKey(1) != 'q');
}
else
{
cout << "Error occurred in starting stream ... aborting. You may need to reconnect the camera." << endl;
}
}
}
else
{
cout << "Error: Image streams not available or wrongly configured. Check connection of camera and config file." << endl;
}
delete dev;
}
return 0;
}
std::size_t size() const
Returns size of array.
Definition: IRArray.h:184
Wrapper for PI driver and image processing library.
Definition: IRImager.h:141
short getTemprangeDecimal() const
Returns the number of decimal places in thermal data.
bool init(IRDeviceParams *params, unsigned int frequency, unsigned int width, unsigned int height, bool useHID, unsigned short hwRev=0, unsigned short fwRev=0)
Initializing routine, to be called after instantiation.
void setThermalFrameCallback(fptrIRThermalFrame callback)
Set callback function to be called for new frames.
unsigned int getHeight() const
Get image height of thermal channel.
void forceFlagEvent(float time=0.f)
Force shutter flag event manually (close/open cycle)
void process(unsigned char *buffer, void *arg=NULL)
Process raw data.
void setReferenceTemperature(float referenceTemperature, float measuredTemperature, float tAmbient=-999.f)
Set a reference temperature to a known reference source inside the view of the camera to improve came...
long long getAvgTimePerFrame() const
Get average time per frame.
float getMaxFramerate() const
Get maximum frame rate of device return frame rate (in frames/second)
unsigned int getWidth() const
Get image width of thermal channel.
Image creation module for displaying purposes.
Definition: ImageBuilder.h:66
Structure containing device parameters.
Definition: IRDeviceParams.h:125
bool useExternalProbeForReferencing
Definition: IRDeviceParams.h:148
Structure containing meta data acquired from the PI imager.
Definition: IRDeviceParams.h:98
EnumFlagState flagState
Definition: IRDeviceParams.h:108
unsigned int counter
Definition: IRDeviceParams.h:104
unsigned int counterHW
Definition: IRDeviceParams.h:105
IRArray< float > pifAIs
Definition: IRDeviceParams.h:114