pose_validation.cpp
Go to the documentation of this file.
1 
21 #include "pose_validation.h"
22 
23 #include <rviz/mesh_loader.h>
24 
25 #include <OgreMesh.h>
26 #include <OGRE/OgreLight.h>
27 
28 #include <ros/package.h>
29 #include <cmath>
30 #include <ros/console.h>
31 
33 
34 PoseValidation::PoseValidation() : is_initialized_(false)
35 {
36 
37 }
38 
39 PoseValidation::PoseValidation(double image_width, double image_height, double far_plane, double near_plane, double cx, double cy, double fx, double fy, int render_width, int render_height) :
40  image_width_(image_width),
41  image_height_(image_height),
42  is_initialized_(true)
43 {
44 
45  ROS_DEBUG_STREAM("Initialize render system");
46 
48  root_ = render_sys_->root();
49  if (!root_->isInitialised()) {
50  root_->initialise(false);
51  }
52 
53  ROS_DEBUG_STREAM("Render system initialized");
54 
55  scene_manager_ = root_->createSceneManager(Ogre::ST_GENERIC);
56 
57  scene_manager_->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
58 
59  Ogre::Light* light2 = scene_manager_->createLight("MainLight");
60  light2->setType( Ogre::Light::LT_DIRECTIONAL );
61  light2->setDirection( Ogre::Vector3( 0 , 0, -1 ) );
62  light2->setDiffuseColour( Ogre::ColourValue( 1.0f, 1.0f, 1.0f ) );
63 
64 
65  camera_ = scene_manager_->createCamera("mainCamera");
66 
67  Ogre::Matrix4 proj_matrix;
68  proj_matrix = Ogre::Matrix4::ZERO;
69 
70  proj_matrix[0][0] = 2.0 * fx/image_width;
71  proj_matrix[1][1] = 2.0 * fy/image_height;
72  proj_matrix[0][2] = 2.0 * (0.5 - cx/image_width);
73  proj_matrix[1][2] = 2.0 * (cy/image_height - 0.5);
74  proj_matrix[2][2] = -(far_plane + near_plane) / (far_plane - near_plane);
75  proj_matrix[2][3] = -2.0 * far_plane * near_plane / (far_plane - near_plane);
76  proj_matrix[3][2] = -1;
77  camera_->setCustomProjectionMatrix(true, proj_matrix);
78 
79  camera_node_ = scene_manager_->getRootSceneNode()->createChildSceneNode();
80  camera_node_->attachObject(camera_);
81 
82 
83  rtt_texture_ =
84  Ogre::TextureManager::getSingleton().createManual(
85  "RttTex",
86  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
87  Ogre::TEX_TYPE_2D,
88  image_width, image_height,
89  0,
90  Ogre::PF_R8G8B8,
91  Ogre::TU_RENDERTARGET);
92 
93 
94  render_texture_ = rtt_texture_->getBuffer()->getRenderTarget();
95 
96  render_texture_->addViewport(camera_);
97  render_texture_->getViewport(0)->setClearEveryFrame(true);
98  render_texture_->getViewport(0)->setBackgroundColour(Ogre::ColourValue::Blue);
99  render_texture_->getViewport(0)->setOverlaysEnabled(false);
100 
101  render_texture_->update();
102 
103  ROS_DEBUG_STREAM("Create render window");
104 
105  window_ = root_->createRenderWindow("Pose validation render window", render_width, render_height, false);
106  //window_->setHidden(true);
107  Ogre::WindowEventUtilities::messagePump();
108  window_->addViewport(camera_);
109 }
110 
112 {
113  return is_initialized_;
114 }
115 
116 HalconCpp::HImage PoseValidation::renderObjectImage(RecognitionResult *recognition_result, Ogre::MeshPtr mesh)
117 {
118 
119  HalconCpp::HPose pose;
120  pose.SetFromTuple(recognition_result->getPose());
121  HalconCpp::HQuaternion quaternion;
122  quaternion.PoseToQuat(pose);
123  quaternion = quaternion.QuatCompose(recognition_result->getAdjustedRotation());
124 
125  scene_manager_->destroyAllEntities();
126 
127  Ogre::Entity* meshEntity = scene_manager_->createEntity(mesh->getName());
128  Ogre::SceneNode* node = scene_manager_->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(pose.ConvertToTuple()[0], -1 * pose.ConvertToTuple()[1], -1 * pose.ConvertToTuple()[2]));
129  node->scale(0.001, 0.001, 0.001);
130  node->attachObject(meshEntity);
131  Ogre::Quaternion orient(quaternion.ConvertToTuple()[0], quaternion.ConvertToTuple()[1], quaternion.ConvertToTuple()[2], quaternion.ConvertToTuple()[3]);
132  orient = Ogre::Quaternion(Ogre::Degree(180), Ogre::Vector3::UNIT_X) * orient;
133  node->setOrientation(orient);
134 
135  root_->renderOneFrame();
136  render_texture_->update();
137 
138  rtt_texture_->getBuffer()->lock(Ogre::HardwareBuffer::HBL_NORMAL);
139 
140  const Ogre::PixelBox& pixelBox = rtt_texture_->getBuffer()->getCurrentLock();
141 
142  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
143  long* pixeldata = (long*)const_cast<unsigned char*>(&pDest[0]);
144  rtt_texture_->getBuffer()->unlock();
145 
146  HalconCpp::HImage img;
147  img.GenImageInterleaved(pixeldata, "bgrx", image_width_, image_height_, 0, "byte", image_width_, image_height_, 0, 0, -1, 0);
148 
149  return img;
150 
151 }
152 
153 
154 HalconCpp::HTuple PoseValidation::validateObject(ObjectDescriptor *object, RecognitionResult *recognition_result, Ogre::MeshPtr mesh)
155 {
156  bool use_color = object->getModelViewDescriptors().at(recognition_result->getViewIndex()).getUseColor();
157  HalconCpp::HImage rendered_image = renderObjectImage(recognition_result, mesh);
158  if (!use_color) {
159  rendered_image = rendered_image.Rgb1ToGray();
160  }
161 
162  HalconCpp::HTuple score;
163  rendered_image.FindUncalibDescriptorModel(object->getModelViewDescriptors().at(recognition_result->getViewIndex()).getDescModel(), HalconCpp::HTuple(), HalconCpp::HTuple(), HalconCpp::HTuple(), HalconCpp::HTuple(), object->getModelViewDescriptors().at(recognition_result->getViewIndex()).getScore2D(), 1, "inlier_ratio", &score);
164 
165  if (score.Length() > 0) {
166  HalconCpp::HTuple matrix_tuple = object->getModelViewDescriptors().at(recognition_result->getViewIndex()).getDescModel().GetDescriptorModelResults(0, "homography");
167  return matrix_tuple;
168  }
169  return HalconCpp::HTuple();
170 }
171 
172 
173 bool PoseValidation::compareHomographyMatrices(HalconCpp::HHomMat2D original, HalconCpp::HHomMat2D rendered, int pose_validation_dist_err)
174 {
175  double x_1, y_1, w_1, x_2, y_2, w_2, x_3, y_3, w_3;
176  x_1 = original.ProjectiveTransPoint2d(0, 0, 1, &y_1, &w_1);
177  x_2 = original.ProjectiveTransPoint2d(0, 10, 1, &y_2, &w_2);
178  x_3 = original.ProjectiveTransPoint2d(10, 0, 1, &y_3, &w_3);
179 
180  double x_rend_1, y_rend_1, w_rend_1, x_rend_2, y_rend_2, w_rend_2, x_rend_3, y_rend_3, w_rend_3;
181  x_rend_1 = rendered.ProjectiveTransPoint2d(0, 0, 1, &y_rend_1, &w_rend_1);
182  x_rend_2 = rendered.ProjectiveTransPoint2d(0, 10, 1, &y_rend_2, &w_rend_2);
183  x_rend_3 = rendered.ProjectiveTransPoint2d(10, 0, 1, &y_rend_3, &w_rend_3);
184 
185  x_1 = x_1 / w_1;
186  y_1 = y_1 / w_1;
187  x_2 = x_2 / w_2;
188  y_2 = y_2 / w_2;
189  x_3 = x_3 / w_3;
190  y_3 = y_3 / w_3;
191 
192  x_rend_1 = x_rend_1 / w_rend_1;
193  y_rend_1 = y_rend_1 / w_rend_1;
194  x_rend_2 = x_rend_2 / w_rend_2;
195  y_rend_2 = y_rend_2 / w_rend_2;
196  x_rend_3 = x_rend_3 / w_rend_3;
197  y_rend_3 = y_rend_3 / w_rend_3;
198 
199  double dist_1, dist_2, dist_3;
200  dist_1 = sqrt(pow(x_1 - x_rend_1, 2) + pow(y_1 - y_rend_1, 2));
201  dist_2 = sqrt(pow(x_2 - x_rend_2, 2) + pow(y_2 - y_rend_2, 2));
202  dist_3 = sqrt(pow(x_3 - x_rend_3, 2) + pow(y_3 - y_rend_3, 2));
203 
204  ROS_DEBUG_STREAM("Pose validation: Max. error = " << pose_validation_dist_err << ", dist1: " << dist_1 << ", dist2: " << dist_2 << ", dist3: " << dist_3);
205  return dist_1 < pose_validation_dist_err && dist_2 < pose_validation_dist_err && dist_3 < pose_validation_dist_err;
206 }
207 
208 }
bool compareHomographyMatrices(HalconCpp::HHomMat2D original, HalconCpp::HHomMat2D rendered, int pose_validation_dist_err)
Compares the given matrices and returns whether they describe the same projection.
f
static RenderSystem * get()
Ogre::Root * root()
PoseValidation()
The empty constructor of this class.
bool isInitialized() const
Returns whether this object has been initialized.
std::vector< ObjectViewDescriptor > getModelViewDescriptors() const
#define ROS_DEBUG_STREAM(args)
HalconCpp::HImage renderObjectImage(RecognitionResult *recognition_result, Ogre::MeshPtr mesh)
Renders the given mesh.
HalconCpp::HTuple validateObject(ObjectDescriptor *object, RecognitionResult *recognition_result, Ogre::MeshPtr mesh)
Searches the rendered image for the given object.


asr_descriptor_surface_based_recognition
Author(s): Allgeyer Tobias, Hutmacher Robin, Meißner Pascal
autogenerated on Mon Dec 16 2019 03:31:15