YARP
Yet Another Robot Platform
DepthImage2.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include "DepthImage2.h"
10 
11 #include <algorithm>
12 #include <cmath>
13 
14 #include <yarp/os/LogComponent.h>
15 #include <yarp/sig/Image.h>
16 
17 using namespace yarp::os;
18 using namespace yarp::sig;
19 
20 namespace {
21 YARP_LOG_COMPONENT(DEPTHIMAGE2,
22  "yarp.carrier.portmonitor.depthimage2",
26  nullptr)
27 }
28 
29 void getHeatMapColor(float value, unsigned char& r, unsigned char& g, unsigned char& b)
30 {
31  const int NUM_COLORS = 5;
32  static float color[NUM_COLORS][3] = { {0,0,1}, {0,1,0}, {1,1,0}, {1,0,0}, {0,0,0} };
33 
34  int idx1;
35  int idx2;
36  float fractBetween = 0;
37 
38  if(value <= 0) { idx1 = idx2 = 0; }
39  else if(value >= 1) { idx1 = idx2 = NUM_COLORS-1; }
40  else
41  {
42  value = value * (NUM_COLORS-1);
43  idx1 = floor(value);
44  idx2 = idx1+1;
45  fractBetween = value - float(idx1);
46  }
47 
48  r = ((color[idx2][0] - color[idx1][0])*fractBetween + color[idx1][0])*255;
49  g = ((color[idx2][1] - color[idx1][1])*fractBetween + color[idx1][1])*255;
50  b = ((color[idx2][2] - color[idx1][2])*fractBetween + color[idx1][2])*255;
51 }
52 
54 {
55  min = 0.2;
56  max = 10.0;
57  outImg.setPixelCode(VOCAB_PIXEL_MONO);
58  return true;
59 }
60 
62 {
63 }
64 
66 {
67  return false;
68 }
69 
71 {
72  return false;
73 }
74 
76 {
77  auto* img = thing.cast_as<Image>();
78  if(img == nullptr)
79  {
80  yCError(DEPTHIMAGE2, "DepthImageConverter: expected type FlexImage but got wrong data type!");
81  return false;
82  }
83 
84  if( img->getPixelCode() == VOCAB_PIXEL_MONO_FLOAT)
85  {
86  return true;
87  }
88 
89  yCError(DEPTHIMAGE2,
90  "DepthImageConverter: expected %s, got %s, not doing any conversion!",
92  yarp::os::Vocab::decode(img->getPixelCode()).c_str() );
93  return false;
94 }
95 
97 {
98  yarp::sig::Image* img = thing.cast_as<Image>();
99 
101  outImg.setPixelSize(3);
102  outImg.resize(img->width(), img->height());
103  outImg.zero();
104 
105  auto* inPixels = reinterpret_cast<float *> (img->getRawImage());
106  unsigned char *outPixels = outImg.getRawImage();
107  for(size_t h=0; h<img->height(); h++)
108  {
109  for(size_t w=0; w<img->width(); w++)
110  {
111  float inVal = inPixels[w + (h * img->width())];
112  if (inVal != inVal /* NaN */ || inVal < min || inVal > max)
113  {
114  outPixels[w*3 + (h * (img->width()*3)) + 0] = 0;
115  outPixels[w*3 + (h * (img->width()*3)) + 1] = 0;
116  outPixels[w*3 + (h * (img->width()*3)) + 2] = 0;
117  }
118  else
119  {
120  float dist = inVal / (max - min);
121  unsigned char r,g,b;
122  getHeatMapColor (dist, r, g, b);
123 
124  outPixels[w*3 + (h * (img->width()*3 )) + 0] = r;
125  outPixels[w*3 + (h * (img->width()*3 )) + 1] = g;
126  outPixels[w*3 + (h * (img->width()*3 )) + 2] = b;
127  }
128  }
129  }
130  th.setPortWriter(&outImg);
131  return th;
132 }
yarp::sig::Image::setPixelCode
void setPixelCode(int imgPixelCode)
Definition: Image.cpp:504
DepthImage2.h
yarp::sig
Signal processing.
Definition: Image.h:25
yarp::os::Log::LogTypeReserved
@ LogTypeReserved
Definition: Log.h:83
DepthImageConverter::create
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
Definition: DepthImage2.cpp:53
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
yarp::os::Things
Base class for generic things.
Definition: Things.h:22
yarp::sig::Image::width
size_t width() const
Gets width of image in pixels.
Definition: Image.h:153
yarp::os::Vocab::decode
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:36
DepthImageConverter::setparam
bool setparam(const yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are set via Yarp admin port.
Definition: DepthImage2.cpp:65
yarp::os::Log::minimumPrintLevel
static LogType minimumPrintLevel()
Get current minimum print level.
Definition: Log.cpp:805
yarp::sig::Image::height
size_t height() const
Gets height of image in pixels.
Definition: Image.h:159
getHeatMapColor
void getHeatMapColor(float value, unsigned char &r, unsigned char &g, unsigned char &b)
Definition: DepthImage2.cpp:29
DepthImageConverter::accept
bool accept(yarp::os::Things &thing) override
This will be called when the data reach the portmonitor object.
Definition: DepthImage2.cpp:75
LogComponent.h
Image.h
yarp::os::Things::cast_as
T * cast_as()
Definition: Things.h:57
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
DepthImageConverter::update
yarp::os::Things & update(yarp::os::Things &thing) override
After data get accpeted in the accept() callback, an instance of that is given to the update function...
Definition: DepthImage2.cpp:96
DepthImageConverter::getparam
bool getparam(yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are requested via Yarp admin port.
Definition: DepthImage2.cpp:70
VOCAB_PIXEL_MONO
@ VOCAB_PIXEL_MONO
Definition: Image.h:48
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::sig::Image
Base class for storing images.
Definition: Image.h:85
VOCAB_PIXEL_RGB
@ VOCAB_PIXEL_RGB
Definition: Image.h:50
yarp::os::Log::printCallback
static LogCallback printCallback()
Get current print callback.
Definition: Log.cpp:852
DepthImageConverter::destroy
void destroy() override
This will be called when the portmonitor object destroyes.
Definition: DepthImage2.cpp:61
yarp::sig::Image::getRawImage
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:534
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
VOCAB_PIXEL_MONO_FLOAT
@ VOCAB_PIXEL_MONO_FLOAT
Definition: Image.h:59