-
Notifications
You must be signed in to change notification settings - Fork 194
/
DepthImage.cpp
103 lines (88 loc) · 2.67 KB
/
DepthImage.cpp
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
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "DepthImage.h"
#include <algorithm>
#include <cmath>
#include <yarp/os/LogComponent.h>
#include <yarp/sig/Image.h>
using namespace yarp::os;
using namespace yarp::sig;
namespace {
YARP_LOG_COMPONENT(DEPTHIMAGE,
"yarp.carrier.portmonitor.depthimage_to_mono",
yarp::os::Log::minimumPrintLevel(),
yarp::os::Log::LogTypeReserved,
yarp::os::Log::printCallback(),
nullptr)
}
bool DepthImageConverter::create(const yarp::os::Property& options)
{
min = 0.2;
max = 10.0;
inMatrix = nullptr;
outMatrix = nullptr;
outImg.setPixelCode(VOCAB_PIXEL_MONO);
return true;
}
void DepthImageConverter::destroy()
{
}
bool DepthImageConverter::setparam(const yarp::os::Property& params)
{
return false;
}
bool DepthImageConverter::getparam(yarp::os::Property& params)
{
return false;
}
bool DepthImageConverter::accept(yarp::os::Things& thing)
{
auto* img = thing.cast_as<Image>();
if(img == nullptr) {
yCError(DEPTHIMAGE, "Expected type FlexImage but got wrong data type!");
return false;
}
if( img->getPixelCode() == VOCAB_PIXEL_MONO_FLOAT)
{
return true;
}
yCError(DEPTHIMAGE,
"Expected %s, got %s, not doing any conversion!",
yarp::os::Vocab32::decode(VOCAB_PIXEL_MONO_FLOAT).c_str(),
yarp::os::Vocab32::decode(img->getPixelCode()).c_str() );
return false;
}
yarp::os::Things& DepthImageConverter::update(yarp::os::Things& thing)
{
auto* img = thing.cast_as<Image>();
inMatrix = reinterpret_cast<float **> (img->getRawImage());
outImg.setPixelCode(VOCAB_PIXEL_MONO);
outImg.setPixelSize(1);
outImg.resize(img->width(), img->height());
outImg.zero();
auto* inPixels = reinterpret_cast<float *> (img->getRawImage());
unsigned char *pixels = outImg.getRawImage();
for(size_t h=0; h<img->height(); h++)
{
for(size_t w=0; w<img->width(); w++)
{
float inVal = inPixels[w + (h * img->width())];
if (inVal != inVal /* NaN */ || inVal < min || inVal > max) {
pixels[w + (h * (img->width() ))] = 0;
} else {
int val = (int) (255.0 - (inVal * 255.0 / (max - min)));
if (val >= 255) {
val = 255;
}
if (val <= 0) {
val = 0;
}
pixels[w + (h * (img->width() ))] = (char) val;
}
}
}
th.setPortWriter(&outImg);
return th;
}