-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathThermalCameraSensor.cc
660 lines (543 loc) · 19.8 KB
/
ThermalCameraSensor.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
* Copyright (C) 2019 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable: 4005)
#pragma warning(disable: 4251)
#endif
#include <ignition/msgs/image.pb.h>
#ifdef _WIN32
#pragma warning(pop)
#endif
#include <algorithm>
#include <mutex>
#include <ignition/common/Console.hh>
#include <ignition/common/Image.hh>
#include <ignition/common/Profiler.hh>
#include <ignition/common/SystemPaths.hh>
#include <ignition/math/Angle.hh>
#include <ignition/math/Helpers.hh>
#include <ignition/transport/Node.hh>
#include "ignition/sensors/ThermalCameraSensor.hh"
#include "ignition/sensors/ImageGaussianNoiseModel.hh"
#include "ignition/sensors/RenderingEvents.hh"
#include "ignition/sensors/SensorFactory.hh"
/// \brief Private data for ThermalCameraSensor
class ignition::sensors::ThermalCameraSensorPrivate
{
/// \brief Save an image
/// \param[in] _data the image data to be saved
/// \param[in] _width width of image in pixels
/// \param[in] _height height of image in pixels
/// \param[in] _format The format the data is in
/// \return True if the image was saved successfully. False can mean
/// that the path provided to the constructor does exist and creation
/// of the path was not possible.
/// \sa ImageSaver
public: bool SaveImage(const uint16_t *_data, unsigned int _width,
unsigned int _height, ignition::common::Image::PixelFormatType _format);
/// \brief Helper function to convert temperature data to thermal image
/// \param[in] _data temperature data
/// \param[out] _imageBuffer resulting thermal image data
/// \param[in] _width width of image
/// \param[in] _height height of image
public: bool ConvertTemperatureToImage(const uint16_t *_data,
unsigned char *_imageBuffer, unsigned int _width, unsigned int _height);
/// \brief node to create publisher
public: transport::Node node;
/// \brief true if Load() has been called and was successful
public: bool initialized = false;
/// \brief Rendering camera
public: ignition::rendering::ThermalCameraPtr thermalCamera;
/// \brief Thermal data buffer.
public: uint16_t *thermalBuffer = nullptr;
/// \brief Thermal data buffer 8 bit.
public: unsigned char *thermalBuffer8Bit = nullptr;
/// \brief Thermal data buffer used when saving image.
public: unsigned char *imgThermalBuffer = nullptr;
/// \brief Thermal data buffer used when saving image.
public: math::Vector2i imgThermalBufferSize =
math::Vector2i::Zero;
/// \brief Pointer to an image to be published
public: ignition::rendering::Image image;
/// \brief Noise added to sensor data
public: std::map<SensorNoiseType, NoisePtr> noises;
/// \brief Event that is used to trigger callbacks when a new image
/// is generated
public: ignition::common::EventT<
void(const ignition::msgs::Image &)> imageEvent;
/// \brief Connection from thermal camera with thermal data
public: ignition::common::ConnectionPtr thermalConnection;
/// \brief Connection to the Manager's scene change event.
public: ignition::common::ConnectionPtr sceneChangeConnection;
/// \brief Just a mutex for thread safety
public: std::mutex mutex;
/// \brief True to save images
public: bool saveImage = false;
/// \brief path directory to where images are saved
public: std::string saveImagePath = "./";
/// \brief Prefix of an image name
public: std::string saveImagePrefix = "./";
/// \brief counter used to set the image filename
public: std::uint64_t saveImageCounter = 0;
/// \brief SDF Sensor DOM object.
public: sdf::Sensor sdfSensor;
/// \brief The point cloud message.
public: msgs::Image thermalMsg;
/// \brief publisher to publish thermal image
public: transport::Node::Publisher thermalPub;
/// \brief Ambient temperature of the environment
public: float ambient = 0.0;
/// \brief Range of ambient temperature.
public: float ambientRange = 0.0;
/// \brief Min temperature the sensor can detect
public: float minTemp = -ignition::math::INF_F;
/// \brief Max temperature the sensor can detect
public: float maxTemp = ignition::math::INF_F;
/// \brief Linear resolution. Defaults to 10mK
public: float resolution = 0.01f;
};
using namespace ignition;
using namespace sensors;
//////////////////////////////////////////////////
ThermalCameraSensor::ThermalCameraSensor()
: CameraSensor(), dataPtr(new ThermalCameraSensorPrivate())
{
}
//////////////////////////////////////////////////
ThermalCameraSensor::~ThermalCameraSensor()
{
this->dataPtr->thermalConnection.reset();
if (this->dataPtr->thermalBuffer)
delete [] this->dataPtr->thermalBuffer;
if (this->dataPtr->thermalBuffer8Bit)
delete[] this->dataPtr->thermalBuffer8Bit;
if (this->dataPtr->imgThermalBuffer)
delete[] this->dataPtr->imgThermalBuffer;
}
//////////////////////////////////////////////////
bool ThermalCameraSensor::Init()
{
return this->CameraSensor::Init();
}
//////////////////////////////////////////////////
bool ThermalCameraSensor::Load(sdf::ElementPtr _sdf)
{
sdf::Sensor sdfSensor;
sdfSensor.Load(_sdf);
return this->Load(sdfSensor);
}
//////////////////////////////////////////////////
bool ThermalCameraSensor::Load(const sdf::Sensor &_sdf)
{
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);
if (!Sensor::Load(_sdf))
{
return false;
}
// Check if this is the right type
if (_sdf.Type() != sdf::SensorType::THERMAL_CAMERA)
{
ignerr << "Attempting to a load a Thermal Camera sensor, but received "
<< "a " << _sdf.TypeStr() << std::endl;
}
if (_sdf.CameraSensor() == nullptr)
{
ignerr << "Attempting to a load a Thermal Camera sensor, but received "
<< "a null sensor." << std::endl;
return false;
}
this->dataPtr->sdfSensor = _sdf;
// Create the thermal image publisher
this->dataPtr->thermalPub =
this->dataPtr->node.Advertise<ignition::msgs::Image>(
this->Topic());
if (!this->dataPtr->thermalPub)
{
ignerr << "Unable to create publisher on topic["
<< this->Topic() << "].\n";
return false;
}
igndbg << "Thermal images for [" << this->Name() << "] advertised on ["
<< this->Topic() << "]" << std::endl;
if (!this->AdvertiseInfo())
return false;
if (this->Scene())
{
this->CreateCamera();
}
this->dataPtr->sceneChangeConnection =
RenderingEvents::ConnectSceneChangeCallback(
std::bind(&ThermalCameraSensor::SetScene, this, std::placeholders::_1));
this->dataPtr->initialized = true;
return true;
}
//////////////////////////////////////////////////
bool ThermalCameraSensor::CreateCamera()
{
const sdf::Camera *cameraSdf = this->dataPtr->sdfSensor.CameraSensor();
if (!cameraSdf)
{
ignerr << "Unable to access camera SDF element\n";
return false;
}
int width = cameraSdf->ImageWidth();
int height = cameraSdf->ImageHeight();
sdf::PixelFormatType pixelFormat = cameraSdf->PixelFormat();
double farPlane = cameraSdf->FarClip();
double nearPlane = cameraSdf->NearClip();
this->PopulateInfo(cameraSdf);
this->dataPtr->thermalCamera = this->Scene()->CreateThermalCamera(
this->Name());
this->dataPtr->thermalCamera->SetImageWidth(width);
this->dataPtr->thermalCamera->SetImageHeight(height);
switch (pixelFormat)
{
case sdf::PixelFormatType::L_INT8:
{
this->dataPtr->thermalCamera->SetImageFormat(rendering::PF_L8);
// sanity check for resolution. The default resolution 10mK is too high
// for 8 bit cameras since it can only capture a temperature range of
// 0 to 2.55 degrees Kelvin ((2^8-1) * 0.01 = 2.55).
if (this->dataPtr->resolution < 1.0)
{
ignwarn << "8 bit thermal camera image format selected. "
<< "The temperature linear resolution needs to be higher "
<< "than 1.0. Defaulting to 3.0, output range = [0, 255*3] K"
<< std::endl;
this->dataPtr->resolution = 3.0;
}
break;
}
// default to 16 bit if format is not recognized
case sdf::PixelFormatType::L_INT16:
default:
this->dataPtr->thermalCamera->SetImageFormat(rendering::PF_L16);
break;
}
this->dataPtr->thermalCamera->SetNearClipPlane(nearPlane);
this->dataPtr->thermalCamera->SetFarClipPlane(farPlane);
this->dataPtr->thermalCamera->SetVisibilityMask(
cameraSdf->VisibilityMask());
this->dataPtr->thermalCamera->SetAmbientTemperature(this->dataPtr->ambient);
this->dataPtr->thermalCamera->SetMinTemperature(this->dataPtr->minTemp);
this->dataPtr->thermalCamera->SetMaxTemperature(this->dataPtr->maxTemp);
this->dataPtr->thermalCamera->SetLinearResolution(this->dataPtr->resolution);
this->AddSensor(this->dataPtr->thermalCamera);
const std::map<SensorNoiseType, sdf::Noise> noises = {
{CAMERA_NOISE, cameraSdf->ImageNoise()},
};
for (const auto & [noiseType, noiseSdf] : noises)
{
// Add gaussian noise to camera sensor
if (noiseSdf.Type() == sdf::NoiseType::GAUSSIAN)
{
this->dataPtr->noises[noiseType] =
NoiseFactory::NewNoiseModel(noiseSdf, "camera");
std::dynamic_pointer_cast<ImageGaussianNoiseModel>(
this->dataPtr->noises[noiseType])->SetCamera(
this->dataPtr->thermalCamera);
}
else if (noiseSdf.Type() != sdf::NoiseType::NONE)
{
ignwarn << "The thermal camera sensor only supports Gaussian noise. "
<< "The supplied noise type[" << static_cast<int>(noiseSdf.Type())
<< "] is not supported." << std::endl;
}
}
math::Angle angle = cameraSdf->HorizontalFov();
if (angle < 0.01 || angle > IGN_PI*2)
{
ignerr << "Invalid horizontal field of view [" << angle << "]\n";
return false;
}
this->dataPtr->thermalCamera->SetAspectRatio(
static_cast<double>(width)/height);
this->dataPtr->thermalCamera->SetHFOV(angle);
// \todo(nkoenig) Port Distortion class
// This->dataPtr->distortion.reset(new Distortion());
// This->dataPtr->distortion->Load(this->sdf->GetElement("distortion"));
this->Scene()->RootVisual()->AddChild(this->dataPtr->thermalCamera);
// Create the directory to store frames
if (cameraSdf->SaveFrames())
{
this->dataPtr->saveImagePath = cameraSdf->SaveFramesPath();
this->dataPtr->saveImagePrefix = this->Name() + "_";
this->dataPtr->saveImage = true;
}
this->dataPtr->thermalConnection =
this->dataPtr->thermalCamera->ConnectNewThermalFrame(
std::bind(&ThermalCameraSensor::OnNewThermalFrame, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::placeholders::_4, std::placeholders::_5));
return true;
}
/////////////////////////////////////////////////
void ThermalCameraSensor::OnNewThermalFrame(const uint16_t *_scan,
unsigned int _width, unsigned int _height,
unsigned int /*_channels*/,
const std::string &/*_format*/)
{
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);
unsigned int samples = _width * _height;
unsigned int thermalBufferSize = samples * sizeof(uint16_t);
if (!this->dataPtr->thermalBuffer)
this->dataPtr->thermalBuffer = new uint16_t[samples];
memcpy(this->dataPtr->thermalBuffer, _scan, thermalBufferSize);
}
/////////////////////////////////////////////////
rendering::ThermalCameraPtr ThermalCameraSensor::ThermalCamera()
{
return this->dataPtr->thermalCamera;
}
/////////////////////////////////////////////////
common::ConnectionPtr ThermalCameraSensor::ConnectImageCallback(
std::function<void(const ignition::msgs::Image &)> _callback)
{
return this->dataPtr->imageEvent.Connect(_callback);
}
/////////////////////////////////////////////////
void ThermalCameraSensor::SetScene(ignition::rendering::ScenePtr _scene)
{
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);
// APIs make it possible for the scene pointer to change
if (this->Scene() != _scene)
{
// TODO(anyone) Remove camera from scene
this->dataPtr->thermalCamera = nullptr;
RenderingSensor::SetScene(_scene);
if (this->dataPtr->initialized)
this->CreateCamera();
}
}
//////////////////////////////////////////////////
bool ThermalCameraSensor::Update(const ignition::common::Time &_now)
{
return this->Update(math::secNsecToDuration(_now.sec, _now.nsec));
}
//////////////////////////////////////////////////
bool ThermalCameraSensor::Update(
const std::chrono::steady_clock::duration &_now)
{
IGN_PROFILE("ThermalCameraSensor::Update");
if (!this->dataPtr->initialized)
{
ignerr << "Not initialized, update ignored.\n";
return false;
}
if (!this->dataPtr->thermalCamera)
{
ignerr << "Camera doesn't exist.\n";
return false;
}
if (!this->dataPtr->thermalPub.HasConnections() &&
this->dataPtr->imageEvent.ConnectionCount() == 0u)
return false;
// generate sensor data - this triggers image callback
this->Render();
if (!this->dataPtr->thermalBuffer)
return false;
unsigned int width = this->dataPtr->thermalCamera->ImageWidth();
unsigned int height = this->dataPtr->thermalCamera->ImageHeight();
auto commonFormat = common::Image::L_INT16;
auto msgsFormat = msgs::PixelFormatType::L_INT16;
auto renderingFormat = rendering::PF_L16;
if (this->dataPtr->thermalCamera->ImageFormat() == rendering::PF_L8)
{
commonFormat = common::Image::L_INT8;
msgsFormat = msgs::PixelFormatType::L_INT8;
renderingFormat = rendering::PF_L8;
}
// create message
this->dataPtr->thermalMsg.set_width(width);
this->dataPtr->thermalMsg.set_height(height);
this->dataPtr->thermalMsg.set_step(
width * rendering::PixelUtil::BytesPerPixel(renderingFormat));
this->dataPtr->thermalMsg.set_pixel_format_type(msgsFormat);
auto stamp = this->dataPtr->thermalMsg.mutable_header()->mutable_stamp();
*stamp = msgs::Convert(_now);
auto frame = this->dataPtr->thermalMsg.mutable_header()->add_data();
frame->set_key("frame_id");
frame->add_value(this->Name());
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);
// \todo(anyone) once ign-rendering supports an image event with unsigned char
// data type, we can remove this check that copies uint16_t data to char array
if (this->dataPtr->thermalCamera->ImageFormat() == rendering::PF_L8)
{
unsigned int len = width * height;
if (!this->dataPtr->thermalBuffer8Bit)
this->dataPtr->thermalBuffer8Bit = new unsigned char[len];
for (unsigned int i = 0; i < len; ++i)
{
this->dataPtr->thermalBuffer8Bit[i] =
static_cast<uint8_t>(this->dataPtr->thermalBuffer[i]);
}
this->dataPtr->thermalMsg.set_data(this->dataPtr->thermalBuffer8Bit,
rendering::PixelUtil::MemorySize(renderingFormat,
width, height));
}
else
{
this->dataPtr->thermalMsg.set_data(this->dataPtr->thermalBuffer,
rendering::PixelUtil::MemorySize(renderingFormat,
width, height));
}
// publish the camera info message
this->PublishInfo(_now);
this->dataPtr->thermalPub.Publish(this->dataPtr->thermalMsg);
// Trigger callbacks.
try
{
this->dataPtr->imageEvent(this->dataPtr->thermalMsg);
}
catch(...)
{
ignerr << "Exception thrown in an image callback.\n";
}
// Save image
if (this->dataPtr->saveImage)
{
this->dataPtr->SaveImage(this->dataPtr->thermalBuffer, width, height,
commonFormat);
}
return true;
}
//////////////////////////////////////////////////
unsigned int ThermalCameraSensor::ImageWidth() const
{
if (!this->dataPtr->thermalCamera)
return 0u;
return this->dataPtr->thermalCamera->ImageWidth();
}
//////////////////////////////////////////////////
unsigned int ThermalCameraSensor::ImageHeight() const
{
if (!this->dataPtr->thermalCamera)
return 0u;
return this->dataPtr->thermalCamera->ImageHeight();
}
//////////////////////////////////////////////////
void ThermalCameraSensor::SetAmbientTemperature(float _ambient)
{
this->dataPtr->ambient = _ambient;
if (this->dataPtr->thermalCamera)
{
this->dataPtr->thermalCamera->SetAmbientTemperature(this->dataPtr->ambient);
}
}
//////////////////////////////////////////////////
void ThermalCameraSensor::SetAmbientTemperatureRange(float _range)
{
this->dataPtr->ambientRange = _range;
if (this->dataPtr->thermalCamera)
{
this->dataPtr->thermalCamera->SetAmbientTemperatureRange(
this->dataPtr->ambientRange);
}
}
//////////////////////////////////////////////////
void ThermalCameraSensor::SetMinTemperature(float _min)
{
this->dataPtr->minTemp = _min;
if (this->dataPtr->thermalCamera)
{
this->dataPtr->thermalCamera->SetMinTemperature(this->dataPtr->minTemp);
}
}
//////////////////////////////////////////////////
void ThermalCameraSensor::SetMaxTemperature(float _max)
{
this->dataPtr->maxTemp = _max;
if (this->dataPtr->thermalCamera)
{
this->dataPtr->thermalCamera->SetMaxTemperature(this->dataPtr->maxTemp);
}
}
//////////////////////////////////////////////////
void ThermalCameraSensor::SetLinearResolution(float _resolution)
{
this->dataPtr->resolution = _resolution;
if (this->dataPtr->thermalCamera)
{
this->dataPtr->thermalCamera->SetLinearResolution(
this->dataPtr->resolution);
}
}
//////////////////////////////////////////////////
bool ThermalCameraSensorPrivate::ConvertTemperatureToImage(
const uint16_t *_data,
unsigned char *_imageBuffer,
unsigned int _width, unsigned int _height)
{
// get min and max of temperature values
const auto [min, max] = // NOLINT
std::minmax_element(&_data[0], &_data[_height * _width]);
// convert temperature to grayscale image
double range = static_cast<double>(max - min);
if (math::equal(range, 0.0))
range = 1.0;
for (unsigned int i = 0; i < _height; ++i)
{
for (unsigned int j = 0; j < _width; ++j)
{
uint16_t temp = _data[i*_width + j];
double t = static_cast<double>(temp-*min) / range;
int r = static_cast<int>(255*t);
int g = r;
int b = r;
int index = i*_width*3 + j*3;
_imageBuffer[index] = r;
_imageBuffer[index+1] = g;
_imageBuffer[index+2] = b;
}
}
return true;
}
//////////////////////////////////////////////////
bool ThermalCameraSensorPrivate::SaveImage(const uint16_t *_data,
unsigned int _width, unsigned int _height,
ignition::common::Image::PixelFormatType /*_format*/)
{
// Attempt to create the directory if it doesn't exist
if (!ignition::common::isDirectory(this->saveImagePath))
{
if (!ignition::common::createDirectories(this->saveImagePath))
return false;
}
if (_width == 0 || _height == 0)
return false;
ignition::common::Image localImage;
if (static_cast<int>(_width) != this->imgThermalBufferSize.X() ||
static_cast<int>(_height) != this->imgThermalBufferSize.Y())
{
delete [] this->imgThermalBuffer;
unsigned int bufferSize = _width * _height * 3;
this->imgThermalBuffer = new unsigned char[bufferSize];
this->imgThermalBufferSize = math::Vector2i(_width, _height);
}
this->ConvertTemperatureToImage(_data, this->imgThermalBuffer,
_width, _height);
std::string filename = this->saveImagePrefix +
std::to_string(this->saveImageCounter) + ".png";
++this->saveImageCounter;
localImage.SetFromData(this->imgThermalBuffer, _width, _height,
common::Image::RGB_INT8);
localImage.SavePNG(
ignition::common::joinPaths(this->saveImagePath, filename));
return true;
}
IGN_SENSORS_REGISTER_SENSOR(ThermalCameraSensor)