YARP
Yet Another Robot Platform
SegmentationImage.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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 "SegmentationImage.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(SEGMENTATIONIMAGE,
22  "yarp.carrier.portmonitor.segmentationimage",
26  nullptr)
27 }
28 
29 
30 rgbColor string2color(std::string colorstring)
31 {
32  rgbColor c;
33  c.r = strtol(colorstring.substr(0, 2).c_str(), NULL, 16);
34  c.g = strtol(colorstring.substr(2, 2).c_str(), NULL, 16);
35  c.b = strtol(colorstring.substr(4, 2).c_str(), NULL, 16);
36  return c;
37 }
38 
40 {
41  outImg.setPixelCode(VOCAB_PIXEL_MONO);
42 
43  size_t index = 0;
44  colormap[index++] = string2color("000000");
45  colormap[index++] = string2color("00FF00");
46  colormap[index++] = string2color("0000FF");
47  colormap[index++] = string2color("FF0000");
48  colormap[index++] = string2color("01FFFE");
49  colormap[index++] = string2color("FFA6FE");
50  colormap[index++] = string2color("FFDB66");
51  colormap[index++] = string2color("006401");
52  colormap[index++] = string2color("010067");
53  colormap[index++] = string2color("95003A");
54  colormap[index++] = string2color("007DB5");
55  colormap[index++] = string2color("FF00F6");
56  colormap[index++] = string2color("FFEEE8");
57  colormap[index++] = string2color("774D00");
58  colormap[index++] = string2color("90FB92");
59  colormap[index++] = string2color("0076FF");
60  colormap[index++] = string2color("D5FF00");
61  colormap[index++] = string2color("FF937E");
62  colormap[index++] = string2color("6A826C");
63  colormap[index++] = string2color("FF029D");
64  colormap[index++] = string2color("FE8900");
65  colormap[index++] = string2color("7A4782");
66  colormap[index++] = string2color("7E2DD2");
67  colormap[index++] = string2color("85A900");
68  colormap[index++] = string2color("FF0056");
69  colormap[index++] = string2color("A42400");
70  colormap[index++] = string2color("00AE7E");
71  colormap[index++] = string2color("683D3B");
72  colormap[index++] = string2color("BDC6FF");
73  colormap[index++] = string2color("263400");
74  colormap[index++] = string2color("BDD393");
75  colormap[index++] = string2color("00B917");
76  colormap[index++] = string2color("9E008E");
77  colormap[index++] = string2color("001544");
78  colormap[index++] = string2color("C28C9F");
79  colormap[index++] = string2color("FF74A3");
80  colormap[index++] = string2color("01D0FF");
81  colormap[index++] = string2color("004754");
82  colormap[index++] = string2color("E56FFE");
83  colormap[index++] = string2color("788231");
84  colormap[index++] = string2color("0E4CA1");
85  colormap[index++] = string2color("91D0CB");
86  colormap[index++] = string2color("BE9970");
87  colormap[index++] = string2color("968AE8");
88  colormap[index++] = string2color("BB8800");
89  colormap[index++] = string2color("43002C");
90  colormap[index++] = string2color("DEFF74");
91  colormap[index++] = string2color("00FFC6");
92  colormap[index++] = string2color("FFE502");
93  colormap[index++] = string2color("620E00");
94  colormap[index++] = string2color("008F9C");
95  colormap[index++] = string2color("98FF52");
96  colormap[index++] = string2color("7544B1");
97  colormap[index++] = string2color("B500FF");
98  colormap[index++] = string2color("00FF78");
99  colormap[index++] = string2color("FF6E41");
100  colormap[index++] = string2color("005F39");
101  colormap[index++] = string2color("6B6882");
102  colormap[index++] = string2color("5FAD4E");
103  colormap[index++] = string2color("A75740");
104  colormap[index++] = string2color("A5FFD2");
105  colormap[index++] = string2color("FFB167");
106  colormap[index++] = string2color("009BFF");
107  colormap[index++] = string2color("E85EBE");
108  max_colors=index;
109 
110  return true;
111 }
112 
114 {
115 }
116 
118 {
119  return false;
120 }
121 
123 {
124  return false;
125 }
126 
128 {
129  yarp::sig::Image* img = thing.cast_as<Image>();
130  if(img == nullptr)
131  {
132  yCError(SEGMENTATIONIMAGE, "SegmentationImageConverter: expected type FlexImage but got wrong data type!");
133  return false;
134  }
135 
137  {
138  return true;
139  }
140 
141  yCError(SEGMENTATIONIMAGE,
142  "SegmentationImageConverter: expected %s or %s, got %s, not doing any conversion!",
145  yarp::os::Vocab::decode(img->getPixelCode()).c_str() );
146  return false;
147 }
148 
150 {
151  yarp::sig::Image* img = thing.cast_as<Image>();
152  int pixcode = img->getPixelCode();
153 
154  outImg.setPixelCode(VOCAB_PIXEL_RGB);
155  outImg.setPixelSize(3);
156  outImg.resize(img->width(), img->height());
157  outImg.zero();
158  unsigned char* outPixels = outImg.getRawImage();
159 
160 
161 
162  for(size_t h=0; h<img->height(); h++)
163  {
164  for(size_t w=0; w<img->width(); w++)
165  {
166  int inVal = 0;
167  if (pixcode == VOCAB_PIXEL_MONO)
168  {
169  char* inPixels = reinterpret_cast<char*> (img->getRawImage());
170  inVal = (int) inPixels[w + (h * img->width())];
171  }
172  else if (pixcode == VOCAB_PIXEL_MONO16)
173  {
174  short int* inPixels = reinterpret_cast<short int*> (img->getRawImage());
175  inVal = inPixels[w + (h * img->width())];
176  }
177  int index = inVal % max_colors;
178  outPixels[w*3 + (h * (img->width()*3 )) + 0] = colormap[index].r;
179  outPixels[w*3 + (h * (img->width()*3 )) + 1] = colormap[index].g;
180  outPixels[w*3 + (h * (img->width()*3 )) + 2] = colormap[index].b;
181  }
182  }
183  th.setPortWriter(&outImg);
184  return th;
185 }
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.
static LogCallback printCallback()
Get current print callback.
Definition: Log.cpp:852
static LogType minimumPrintLevel()
Get current minimum print level.
Definition: Log.cpp:805
@ LogTypeReserved
Definition: Log.h:83
A class for storing options and configuration information.
Definition: Property.h:37
Base class for generic things.
Definition: Things.h:22
T * cast_as()
Definition: Things.h:57
Base class for storing images.
Definition: Image.h:85
size_t width() const
Gets width of image in pixels.
Definition: Image.h:153
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:534
size_t height() const
Gets height of image in pixels.
Definition: Image.h:159
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.cpp:454
#define yCError(component,...)
Definition: LogComponent.h:157
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
@ VOCAB_PIXEL_MONO16
Definition: Image.h:49
@ VOCAB_PIXEL_MONO
Definition: Image.h:48
@ VOCAB_PIXEL_RGB
Definition: Image.h:50
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:36
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:25