26 #include <OGRE/OgreLight.h> 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) :
49 if (!
root_->isInitialised()) {
50 root_->initialise(
false);
60 light2->setType( Ogre::Light::LT_DIRECTIONAL );
61 light2->setDirection( Ogre::Vector3( 0 , 0, -1 ) );
62 light2->setDiffuseColour( Ogre::ColourValue( 1.0
f, 1.0
f, 1.0
f ) );
67 Ogre::Matrix4 proj_matrix;
68 proj_matrix = Ogre::Matrix4::ZERO;
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);
84 Ogre::TextureManager::getSingleton().createManual(
86 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
88 image_width, image_height,
91 Ogre::TU_RENDERTARGET);
98 render_texture_->getViewport(0)->setBackgroundColour(Ogre::ColourValue::Blue);
105 window_ =
root_->createRenderWindow(
"Pose validation render window", render_width, render_height,
false);
107 Ogre::WindowEventUtilities::messagePump();
119 HalconCpp::HPose pose;
120 pose.SetFromTuple(recognition_result->
getPose());
121 HalconCpp::HQuaternion quaternion;
122 quaternion.PoseToQuat(pose);
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);
135 root_->renderOneFrame();
138 rtt_texture_->getBuffer()->lock(Ogre::HardwareBuffer::HBL_NORMAL);
140 const Ogre::PixelBox& pixelBox =
rtt_texture_->getBuffer()->getCurrentLock();
142 Ogre::uint8* pDest =
static_cast<Ogre::uint8*
>(pixelBox.data);
143 long* pixeldata = (
long*)const_cast<unsigned char*>(&pDest[0]);
146 HalconCpp::HImage img;
147 img.GenImageInterleaved(pixeldata,
"bgrx",
image_width_,
image_height_, 0,
"byte",
image_width_,
image_height_, 0, 0, -1, 0);
156 bool use_color =
object->getModelViewDescriptors().at(recognition_result->
getViewIndex()).getUseColor();
159 rendered_image = rendered_image.Rgb1ToGray();
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);
165 if (score.Length() > 0) {
166 HalconCpp::HTuple matrix_tuple =
object->getModelViewDescriptors().at(recognition_result->
getViewIndex()).getDescModel().GetDescriptorModelResults(0,
"homography");
169 return HalconCpp::HTuple();
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);
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);
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;
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));
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;
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.
HalconCpp::HTuple getPose() const
static RenderSystem * get()
Ogre::RenderWindow * window_
Ogre::SceneManager * scene_manager_
PoseValidation()
The empty constructor of this class.
HalconCpp::HQuaternion getAdjustedRotation() const
bool isInitialized() const
Returns whether this object has been initialized.
Ogre::SceneNode * camera_node_
Ogre::TexturePtr rtt_texture_
Ogre::RenderTexture * render_texture_
std::vector< ObjectViewDescriptor > getModelViewDescriptors() const
#define ROS_DEBUG_STREAM(args)
rviz::RenderSystem * render_sys_
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.