YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SegmentationImage.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 "SegmentationImage.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.segmentationimage_to_rgb",
23 nullptr)
24}
25
26
28{
29 rgbColor c;
30 c.r = strtol(colorstring.substr(0, 2).c_str(), NULL, 16);
31 c.g = strtol(colorstring.substr(2, 2).c_str(), NULL, 16);
32 c.b = strtol(colorstring.substr(4, 2).c_str(), NULL, 16);
33 return c;
34}
35
37{
39
40 size_t index = 0;
41 colormap[index++] = string2color("000000");
42 colormap[index++] = string2color("00FF00");
43 colormap[index++] = string2color("0000FF");
44 colormap[index++] = string2color("FF0000");
45 colormap[index++] = string2color("01FFFE");
46 colormap[index++] = string2color("FFA6FE");
47 colormap[index++] = string2color("FFDB66");
48 colormap[index++] = string2color("006401");
49 colormap[index++] = string2color("010067");
50 colormap[index++] = string2color("95003A");
51 colormap[index++] = string2color("007DB5");
52 colormap[index++] = string2color("FF00F6");
53 colormap[index++] = string2color("FFEEE8");
54 colormap[index++] = string2color("774D00");
55 colormap[index++] = string2color("90FB92");
56 colormap[index++] = string2color("0076FF");
57 colormap[index++] = string2color("D5FF00");
58 colormap[index++] = string2color("FF937E");
59 colormap[index++] = string2color("6A826C");
60 colormap[index++] = string2color("FF029D");
61 colormap[index++] = string2color("FE8900");
62 colormap[index++] = string2color("7A4782");
63 colormap[index++] = string2color("7E2DD2");
64 colormap[index++] = string2color("85A900");
65 colormap[index++] = string2color("FF0056");
66 colormap[index++] = string2color("A42400");
67 colormap[index++] = string2color("00AE7E");
68 colormap[index++] = string2color("683D3B");
69 colormap[index++] = string2color("BDC6FF");
70 colormap[index++] = string2color("263400");
71 colormap[index++] = string2color("BDD393");
72 colormap[index++] = string2color("00B917");
73 colormap[index++] = string2color("9E008E");
74 colormap[index++] = string2color("001544");
75 colormap[index++] = string2color("C28C9F");
76 colormap[index++] = string2color("FF74A3");
77 colormap[index++] = string2color("01D0FF");
78 colormap[index++] = string2color("004754");
79 colormap[index++] = string2color("E56FFE");
80 colormap[index++] = string2color("788231");
81 colormap[index++] = string2color("0E4CA1");
82 colormap[index++] = string2color("91D0CB");
83 colormap[index++] = string2color("BE9970");
84 colormap[index++] = string2color("968AE8");
85 colormap[index++] = string2color("BB8800");
86 colormap[index++] = string2color("43002C");
87 colormap[index++] = string2color("DEFF74");
88 colormap[index++] = string2color("00FFC6");
89 colormap[index++] = string2color("FFE502");
90 colormap[index++] = string2color("620E00");
91 colormap[index++] = string2color("008F9C");
92 colormap[index++] = string2color("98FF52");
93 colormap[index++] = string2color("7544B1");
94 colormap[index++] = string2color("B500FF");
95 colormap[index++] = string2color("00FF78");
96 colormap[index++] = string2color("FF6E41");
97 colormap[index++] = string2color("005F39");
98 colormap[index++] = string2color("6B6882");
99 colormap[index++] = string2color("5FAD4E");
100 colormap[index++] = string2color("A75740");
101 colormap[index++] = string2color("A5FFD2");
102 colormap[index++] = string2color("FFB167");
103 colormap[index++] = string2color("009BFF");
104 colormap[index++] = string2color("E85EBE");
105 max_colors=index;
106
107 return true;
108}
109
113
115{
116 return false;
117}
118
120{
121 return false;
122}
123
125{
126 yarp::sig::Image* img = thing.cast_as<Image>();
127 if(img == nullptr)
128 {
129 yCError(SEGMENTATIONIMAGE, "SegmentationImageConverter: expected type FlexImage but got wrong data type!");
130 return false;
131 }
132
134 {
135 return true;
136 }
137
139 "SegmentationImageConverter: expected %s or %s, got %s, not doing any conversion!",
142 yarp::os::Vocab32::decode(img->getPixelCode()).c_str() );
143 return false;
144}
145
147{
148 yarp::sig::Image* img = thing.cast_as<Image>();
149 int pixcode = img->getPixelCode();
150
152 outImg.setPixelSize(3);
153 outImg.resize(img->width(), img->height());
154 outImg.zero();
155 unsigned char* outPixels = outImg.getRawImage();
156
157
158
159 for(size_t h=0; h<img->height(); h++)
160 {
161 for(size_t w=0; w<img->width(); w++)
162 {
163 int inVal = 0;
165 {
166 char* inPixels = reinterpret_cast<char*> (img->getRawImage());
167 inVal = (int) inPixels[w + (h * img->width())];
168 }
169 else if (pixcode == VOCAB_PIXEL_MONO16)
170 {
171 short int* inPixels = reinterpret_cast<short int*> (img->getRawImage());
172 inVal = inPixels[w + (h * img->width())];
173 }
174 int index = inVal % max_colors;
175 outPixels[w*3 + (h * (img->width()*3 )) + 0] = colormap[index].r;
176 outPixels[w*3 + (h * (img->width()*3 )) + 1] = colormap[index].g;
177 outPixels[w*3 + (h * (img->width()*3 )) + 2] = colormap[index].b;
178 }
179 }
180 th.setPortWriter(&outImg);
181 return th;
182}
@ VOCAB_PIXEL_MONO16
Definition Image.h:43
@ VOCAB_PIXEL_MONO
Definition Image.h:42
@ VOCAB_PIXEL_RGB
Definition Image.h:44
rgbColor string2color(std::string colorstring)
bool accept(yarp::os::Things &thing) override
This will be called when the data reach the portmonitor object.
void destroy() override
This will be called when the portmonitor object destroyes.
bool setparam(const yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are set 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...
bool getparam(yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are requested via YARP admin port.
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
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
virtual int getPixelCode() const
Gets pixel type identifier.
Definition Image.cpp:390
#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.