MaskRobotInImage.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2018, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX
19  * @author Julian Zimmer ( urdbu at student dot kit dot edu )
20  * @date 2018
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "MaskRobotInImage.h"
26 
28 #include <inttypes.h>
29 
31 
32 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
33 
34 
35 
36 using namespace armarx;
37 
38 MaskRobotInImage::MaskRobotInImage(RobotStateComponentInterfacePrx robotStateComponent, std::string cameraFrameName, visionx::ImageFormatInfo imageFormat, float fov, SbColor backgroundColor, bool flipImages, bool useFullModel, float collisionModelInflationMargin)
39 {
40  VirtualRobot::init("MaskRobotInImage");
41  // needed for SoSelection
42  SoInteraction::init();
43 
44  this->cameraFrameName = cameraFrameName;
45  this->fov = fov;
46  this->imageWidth = imageFormat.dimension.width;
47  this->imageHeight = imageFormat.dimension.height;
48  this->backgroundColor = backgroundColor;
49  this->flipImages = flipImages;
50  this->useFullModel = useFullModel;
51  this->collisionModelInflationMargin = collisionModelInflationMargin;
52 
53 
54  renderedImage = new CByteImage(imageWidth, imageHeight, CByteImage::eRGB24);
55  maskImage = new CByteImage(imageWidth, imageHeight, CByteImage::eGrayScale);
56 
57  //get the local robot
58  ARMARX_CHECK_EXPRESSION(robotStateComponent);
59 
60  this->robotStateComponent = robotStateComponent;
61 
63 
64 
65  //create the offscreen renderer
66  renderer.reset(VirtualRobot::CoinVisualizationFactory::createOffscreenRenderer(imageWidth, imageHeight));
67  renderBuffer.resize(imageWidth * imageHeight * 3);
68 
69 
70  renderer->setBackgroundColor(backgroundColor);
71 
72 
73 
74 
75  if (!useFullModel)
76  {
77  if (collisionModelInflationMargin > 0.f)
78  {
79  //inflate the collision model
80  for (auto& model : localRobot->getCollisionModels())
81  {
82  model->setUpdateVisualization(true);
83  model->inflateModel(collisionModelInflationMargin);
84  ARMARX_INFO << "MARGIN AFTER INITIALIZATION: " << model->getMargin();
85  }
86  }
87  }
88  else
89  {
90  if (collisionModelInflationMargin > 0.f)
91  {
92  ARMARX_IMPORTANT << "Inflating collisionModel but not rendering collisionModel!";
93  }
94  }
95 
96 
97 
98  //ARMARX_INFO << "HASTIMESERVER: " << TimeUtil::HasTimeServer();
99 
100 
101 
102  ARMARX_INFO << "RENDERER BACKGROUND COLOR IS: " << renderer->getBackgroundColor().getValue()[0] << ";" << renderer->getBackgroundColor().getValue()[1] << ";" << renderer->getBackgroundColor().getValue()[2] << "]";
103 }
104 
106 {
107 
108 }
109 
111 {
112  ARMARX_CHECK_EXPRESSION(robotStateComponent);
113  //synchronize the localRobot
114  RemoteRobot::synchronizeLocalCloneToTimestamp(localRobot, robotStateComponent, timestamp);
115  ARMARX_CHECK_EXPRESSION(localRobot);
116  //get the cameraNode
117  VirtualRobot::RobotNodePtr cameraNode = localRobot->getRobotNode(cameraFrameName);
118  ARMARX_CHECK_EXPRESSION(cameraNode);
119 
120 
121  //get the visualization
122  VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Collision;
123 
124  if (useFullModel)
125  {
126  visuType = VirtualRobot::SceneObject::Full;
127  }
128 
129 
130  VirtualRobot::CoinVisualizationPtr visualization = localRobot->getVisualization<VirtualRobot::CoinVisualization>(visuType);
131  ARMARX_CHECK_EXPRESSION(visualization);
132 
133 
134 
135  SoNode* visualisationNode = NULL;
136  ARMARX_CHECK_EXPRESSION(visualization);
137 
138  visualisationNode = visualization->getCoinVisualization();
139 
140  ARMARX_CHECK_EXPRESSION(renderer);
141 
142 
143  //feed it into the renderer
144  bool renderOK = false;
145 
146  auto bufferPtr = renderBuffer.data();
147 
148 
149 
150 
151 
152  //increasing the zNear drawing distance, because the DepthCameraSim camera node is inside the depth camera model
153  renderOK = VirtualRobot::CoinVisualizationFactory::renderOffscreen(renderer.get(), cameraNode, visualisationNode, &bufferPtr, 20.0f, 100000.0f, fov);
154 
155  //Depending on the pose of the camera node, the image should be flipped upside down
156  //If the nodes like EyeLeftCameraSim or DepthCameraSim are used, this has to be performed,
157  //since the orientation of these nodes is flipped in comparison to the orientation expected by the offscreen renderer
158 
159  //copy rendering into image and create the image mask
160  unsigned char* pRenderedImage = renderer->getBuffer();
161  if (renderOK)
162  {
163  std::uint8_t* pixelsRow = renderedImage->pixels;
164  std::uint8_t* maskPixelsRow = maskImage->pixels;
165  for (int y = 0; y < imageHeight; y++)
166  {
167  for (int x = 0; x < imageWidth; x++)
168  {
169  int adjustedX = x;
170  int adjustedY = y;
171  if (flipImages)
172  {
173  adjustedX = imageWidth - 1 - x;
174  adjustedY = imageHeight - 1 - y;
175  }
176 
177 
178  pixelsRow[x * 3 + 0] = pRenderedImage[3 * (imageWidth * (imageHeight - adjustedY) + adjustedX) + 0];
179  pixelsRow[x * 3 + 1] = pRenderedImage[3 * (imageWidth * (imageHeight - adjustedY) + adjustedX) + 1];
180  pixelsRow[x * 3 + 2] = pRenderedImage[3 * (imageWidth * (imageHeight - adjustedY) + adjustedX) + 2];
181 
182  //set the masking pixel to 255 if the background color is in the image pixel, else set it to 0
183  SbColor pixelColor(((float) pixelsRow[x * 3 + 0]) / 255.f, ((float) pixelsRow[x * 3 + 1]) / 255.f, ((float) pixelsRow[x * 3 + 2]) / 255.f);
184  if (backgroundColor.equals(pixelColor, 0.004f))
185  {
186  maskPixelsRow[x] = 0;
187  }
188  else
189  {
190  maskPixelsRow[x] = 255;
191  }
192  }
193  pixelsRow += imageWidth * 3;
194  maskPixelsRow += imageWidth;
195  }
196  }
197 
198 
199  return maskImage;
200 }
201 
202 void MaskRobotInImage::setBackgroundColor(SbColor newBackgroundColor)
203 {
204  backgroundColor = newBackgroundColor;
205  renderer->setBackgroundColor(newBackgroundColor);
206 }
RemoteRobot.h
armarx::eFull
@ eFull
Definition: MorphingItem.h:35
MaskRobotInImage.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::RemoteRobot::createLocalCloneFromFile
static VirtualRobot::RobotPtr createLocalCloneFromFile(RobotStateComponentInterfacePrx robotStatePrx, VirtualRobot::RobotIO::RobotDescription loadMode=VirtualRobot::RobotIO::eFull)
This is a convenience function for createLocalClone, which automatically gets the filename from the R...
Definition: RemoteRobot.cpp:441
armarx::MaskRobotInImage::~MaskRobotInImage
~MaskRobotInImage()
MaskRobotInImage Destructor.
Definition: MaskRobotInImage.cpp:105
CoinVisualizationPtr
boost::shared_ptr< VirtualRobot::CoinVisualization > CoinVisualizationPtr
Definition: ManipulatorVisualization.h:52
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
CMakePackageFinder.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::MaskRobotInImage::MaskRobotInImage
MaskRobotInImage(RobotStateComponentInterfacePrx robotStateComponent, std::string cameraFrameName, visionx::ImageFormatInfo imageFormat, float fov, SbColor backgroundColor=SbColor(0.f,(177.f/255.f),(64.f/255.f)), bool flipImages=true, bool useFullModel=true, float collisionModelInflationMargin=0.f)
MaskRobotInImage Constructor.
Definition: MaskRobotInImage.cpp:38
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::RemoteRobot::synchronizeLocalCloneToTimestamp
static bool synchronizeLocalCloneToTimestamp(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx, Ice::Long timestamp)
Synchronizes a local robot to a robot state at timestamp.
Definition: RemoteRobot.cpp:487
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
armarx::MaskRobotInImage::setBackgroundColor
void setBackgroundColor(SbColor newBackgroundColor)
Definition: MaskRobotInImage.cpp:202
armarx::MaskRobotInImage::getMaskImage
CByteImage * getMaskImage(Ice::Long timestamp)
Definition: MaskRobotInImage.cpp:110
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28