YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
DepthImage2.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "DepthImage2.h"
7
8#include <algorithm>
9#include <cmath>
10
12#include <yarp/sig/Image.h>
13
14using namespace yarp::os;
15using namespace yarp::sig;
16
17namespace {
19 "yarp.carrier.portmonitor.depthimage_to_rgb",
23 nullptr)
24}
25
26void getHeatMapColor(float value, unsigned char& r, unsigned char& g, unsigned char& b)
27{
28 const int NUM_COLORS = 5;
29 static float color[NUM_COLORS][3] = { {0,0,1}, {0,1,0}, {1,1,0}, {1,0,0}, {0,0,0} };
30
31 int idx1;
32 int idx2;
33 float fractBetween = 0;
34
35 if(value <= 0) { idx1 = idx2 = 0; }
36 else if(value >= 1) { idx1 = idx2 = NUM_COLORS-1; }
37 else
38 {
39 value = value * (NUM_COLORS-1);
40 idx1 = floor(value);
41 idx2 = idx1+1;
42 fractBetween = value - float(idx1);
43 }
44
45 r = ((color[idx2][0] - color[idx1][0])*fractBetween + color[idx1][0])*255;
46 g = ((color[idx2][1] - color[idx1][1])*fractBetween + color[idx1][1])*255;
47 b = ((color[idx2][2] - color[idx1][2])*fractBetween + color[idx1][2])*255;
48}
49
51{
52 min = 0.2;
53 max = 10.0;
55 return true;
56}
57
59{
60}
61
63{
64 return false;
65}
66
68{
69 return false;
70}
71
73{
74 auto* img = thing.cast_as<Image>();
75 if(img == nullptr)
76 {
77 yCError(DEPTHIMAGE2, "Expected type FlexImage but got wrong data type!");
78 return false;
79 }
80
81 if( img->getPixelCode() == VOCAB_PIXEL_MONO_FLOAT)
82 {
83 return true;
84 }
85
87 "Expected %s, got %s, not doing any conversion!",
89 yarp::os::Vocab32::decode(img->getPixelCode()).c_str() );
90 return false;
91}
92
94{
95 yarp::sig::Image* img = thing.cast_as<Image>();
96
98 outImg.setPixelSize(3);
99 outImg.resize(img->width(), img->height());
100 outImg.zero();
101
102 auto* inPixels = reinterpret_cast<float *> (img->getRawImage());
103 unsigned char *outPixels = outImg.getRawImage();
104 for(size_t h=0; h<img->height(); h++)
105 {
106 for(size_t w=0; w<img->width(); w++)
107 {
108 float inVal = inPixels[w + (h * img->width())];
109 if (inVal != inVal /* NaN */ || inVal < min || inVal > max)
110 {
111 outPixels[w*3 + (h * (img->width()*3)) + 0] = 0;
112 outPixels[w*3 + (h * (img->width()*3)) + 1] = 0;
113 outPixels[w*3 + (h * (img->width()*3)) + 2] = 0;
114 }
115 else
116 {
117 float dist = inVal / (max - min);
118 unsigned char r,g,b;
119 getHeatMapColor (dist, r, g, b);
120
121 outPixels[w*3 + (h * (img->width()*3 )) + 0] = r;
122 outPixels[w*3 + (h * (img->width()*3 )) + 1] = g;
123 outPixels[w*3 + (h * (img->width()*3 )) + 2] = b;
124 }
125 }
126 }
127 th.setPortWriter(&outImg);
128 return th;
129}
void getHeatMapColor(float value, unsigned char &r, unsigned char &g, unsigned char &b)
@ VOCAB_PIXEL_MONO_FLOAT
Definition Image.h:53
@ VOCAB_PIXEL_MONO
Definition Image.h:42
@ VOCAB_PIXEL_RGB
Definition Image.h:44
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
bool setparam(const yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are set via YARP admin port.
bool getparam(yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are requested via YARP admin port.
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...
void destroy() override
This will be called when the portmonitor object destroyes.
bool accept(yarp::os::Things &thing) override
This will be called when the data reach the portmonitor object.
A mini-server for performing network communication in the background.
static LogCallback printCallback()
Get current print callback.
Definition Log.cpp:873
static LogType minimumPrintLevel()
Get current minimum print level.
Definition Log.cpp:833
@ LogTypeReserved
Definition Log.h:98
A class for storing options and configuration information.
Definition Property.h:33
Base class for generic things.
Definition Things.h:18
T * cast_as()
Definition Things.h:53
void setPortWriter(yarp::os::PortWriter *writer)
Set the reference to a PortWriter object.
Definition Things.cpp:26
void setPixelCode(int imgPixelCode)
Definition Image.h:366
void setPixelSize(size_t imgPixelSize)
Definition Image.h:371
Base class for storing images.
Definition Image.h:79
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:479
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition Image.cpp:402
void zero()
Set all pixels to 0.
Definition Image.cpp:395
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition Vocab.cpp:33
An interface to the operating system, including Port based communication.