@@ -43,15 +43,19 @@ SolARFiducialMarkerPoseEstimator::SolARFiducialMarkerPoseEstimator():Configurabl
4343 declareInjectable<api::features::ISBPatternReIndexer>(m_patternReIndexer);
4444 declareInjectable<api::geom::IImage2WorldMapper>(m_img2worldMapper);
4545 declareInjectable<api::solver::pose::I3DTransformFinderFrom2D3D>(m_pnp);
46+ declareInjectable<api::features::ICornerRefinement>(m_cornerRefinement);
47+ declareInjectable<api::geom::IProject>(m_projector);
4648 declareProperty (" nbThreshold" , m_nbThreshold);
4749 declareProperty (" minThreshold" , m_minThreshold);
4850 declareProperty (" maxThreshold" , m_maxThreshold);
51+ declareProperty (" maxReprojError" , m_maxReprojError);
4952}
5053
5154void SolARFiducialMarkerPoseEstimator::setCameraParameters (const CamCalibration & intrinsicParams, const CamDistortion & distortionParams) {
5255 m_camMatrix = intrinsicParams;
5356 m_camDistortion = distortionParams;
5457 m_pnp->setCameraParameters (m_camMatrix, m_camDistortion);
58+ m_projector->setCameraParameters (m_camMatrix, m_camDistortion);
5559 // components initialisation for marker detection
5660 m_binaryMarker->loadMarker ();
5761 m_patternDescriptorExtractor->extract (m_binaryMarker->getPattern (), m_markerPatternDescriptor);
@@ -136,10 +140,22 @@ FrameworkReturnCode SolARFiducialMarkerPoseEstimator::estimate(const SRef<Image>
136140 m_patternReIndexer->reindex (recognizedContours, patternMatches, pattern2DPoints, img2DPoints);
137141 // Compute the 3D position of each corner of the marker
138142 m_img2worldMapper->map (pattern2DPoints, pattern3DPoints);
143+ // Refine corner locations
144+ m_cornerRefinement->refine (greyImage, img2DPoints);
139145 // Compute the pose of the camera using a Perspective n Points algorithm using only the 4 corners of the marker
140146 if (m_pnp->estimate (img2DPoints, pattern3DPoints, pose) == FrameworkReturnCode::_SUCCESS)
141147 {
142- return FrameworkReturnCode::_SUCCESS;
148+ std::vector<Point2Df> projected2DPts;
149+ m_projector->project (pattern3DPoints, projected2DPts, pose);
150+ float errorReproj (0 .f );
151+ for (int j = 0 ; j < projected2DPts.size (); ++j)
152+ errorReproj += (projected2DPts[j] - img2DPoints[j]).norm ();
153+ errorReproj /= projected2DPts.size ();
154+ LOG_DEBUG (" Mean reprojection error: {}" , errorReproj);
155+ if (errorReproj < m_maxReprojError)
156+ return FrameworkReturnCode::_SUCCESS;
157+ pose = Transform3Df::Identity ();
158+ return FrameworkReturnCode::_ERROR_;
143159 }
144160 }
145161 }
0 commit comments