YARP
Yet Another Robot Platform
DepthImage.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 "DepthImage.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 {
18YARP_LOG_COMPONENT(DEPTHIMAGE,
19 "yarp.carrier.portmonitor.depthimage_to_mono",
23 nullptr)
24}
25
27{
28 min = 0.2;
29 max = 10.0;
30 inMatrix = nullptr;
31 outMatrix = nullptr;
33 return true;
34}
35
37{
38}
39
41{
42 return false;
43}
44
46{
47 return false;
48}
49
51{
52 auto* img = thing.cast_as<Image>();
53 if(img == nullptr) {
54 yCError(DEPTHIMAGE, "Expected type FlexImage but got wrong data type!");
55 return false;
56 }
57
58 if( img->getPixelCode() == VOCAB_PIXEL_MONO_FLOAT)
59 {
60 return true;
61 }
62
63 yCError(DEPTHIMAGE,
64 "Expected %s, got %s, not doing any conversion!",
66 yarp::os::Vocab32::decode(img->getPixelCode()).c_str() );
67 return false;
68}
69
71{
72 auto* img = thing.cast_as<Image>();
73 inMatrix = reinterpret_cast<float **> (img->getRawImage());
74
76 outImg.setPixelSize(1);
77 outImg.resize(img->width(), img->height());
78
79 outImg.zero();
80 auto* inPixels = reinterpret_cast<float *> (img->getRawImage());
81 unsigned char *pixels = outImg.getRawImage();
82 for(size_t h=0; h<img->height(); h++)
83 {
84 for(size_t w=0; w<img->width(); w++)
85 {
86 float inVal = inPixels[w + (h * img->width())];
87 if (inVal != inVal /* NaN */ || inVal < min || inVal > max) {
88 pixels[w + (h * (img->width() ))] = 0;
89 } else {
90 int val = (int) (255.0 - (inVal * 255.0 / (max - min)));
91 if (val >= 255) {
92 val = 255;
93 }
94 if (val <= 0) {
95 val = 0;
96 }
97 pixels[w + (h * (img->width() ))] = (char) val;
98 }
99 }
100 }
101 th.setPortWriter(&outImg);
102 return th;
103}
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
Definition: DepthImage.cpp:26
bool setparam(const yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are set via YARP admin port.
Definition: DepthImage.cpp:40
bool getparam(yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are requested via YARP admin port.
Definition: DepthImage.cpp:45
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: DepthImage.cpp:70
void destroy() override
This will be called when the portmonitor object destroyes.
Definition: DepthImage.cpp:36
bool accept(yarp::os::Things &thing) override
This will be called when the data reach the portmonitor object.
Definition: DepthImage.cpp:50
static LogCallback printCallback()
Get current print callback.
Definition: Log.cpp:880
static LogType minimumPrintLevel()
Get current minimum print level.
Definition: Log.cpp:833
@ LogTypeReserved
Definition: Log.h:97
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:414
void setPixelSize(size_t imgPixelSize)
Definition: Image.h:419
Base class for storing images.
Definition: Image.h:79
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:542
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:453
void zero()
Set all pixels to 0.
Definition: Image.cpp:446
#define yCError(component,...)
Definition: LogComponent.h:213
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
@ VOCAB_PIXEL_MONO_FLOAT
Definition: Image.h:53
@ VOCAB_PIXEL_MONO
Definition: Image.h:42
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.