32 #include <Image/ByteImage.h>
33 #include <Calibration/Calibration.h>
34 #include <Math/Math3d.h>
50 std::vector<CHypothesisPoint*> aForegroundPoints;
56 if (aForegroundPoints.size() > 0)
66 for (
int i = 0; i < pObjectHypotheses->GetSize(); i++)
74 ARMARX_VERBOSE_S <<
"ValidateInitialHypothesis: Wrong hypothesis type (not RGBD or SingleColored). Not checking it for motion.";
79 float fForegroundRatio;
80 int nForegroundPoints;
83 if (fForegroundRatio > 0.3f)
85 ARMARX_VERBOSE_S <<
"Estimating transformation of hypothesis " << (*pObjectHypotheses)[i]->nHypothesisNumber <<
" (" << 100 * fForegroundRatio <<
" % change, " << (*pObjectHypotheses)[i]->aNewPoints.size() <<
" points).";
88 bool bValidated =
ValidateHypothesis(pPointCloudRegistration, aNewDepthMapPoints, aForegroundPoints, pForegroundImage, pHSVImage, calibration, upwardsVector, (*pObjectHypotheses)[i], pConfirmedHypothesis,
OLP_EFFORT_POINTCLOUD_MATCHING - 1);
93 pConfirmedHypotheses->AddElement(pConfirmedHypothesis);
98 ARMARX_VERBOSE_S <<
"The image region containing hypothesis " << i <<
" did not change (" << 100 * fForegroundRatio <<
" %).";
103 delete pPointCloudRegistration;
118 const CByteImage* pHSVImage,
const CCalibration* calibration,
const Vec3d upwardsVector,
CObjectHypothesisArray* pObjectHypotheses)
121 std::vector<CHypothesisPoint*> aForegroundPoints;
127 if (aForegroundPoints.size() > 0)
134 for (
int i = 0; i < pObjectHypotheses->GetSize(); i++)
139 <<
" (" << (*pObjectHypotheses)[i]->aConfirmedPoints.size() <<
" + " << (*pObjectHypotheses)[i]->aNewPoints.size() <<
" points).";
141 bool bValidated =
ValidateHypothesis(pPointCloudRegistration, aNewDepthMapPoints, aForegroundPoints, pForegroundImage, pHSVImage, calibration, upwardsVector, pObjectHypothesis, pConfirmedHypothesis);
145 (*pObjectHypotheses)[i] = pConfirmedHypothesis;
146 delete pObjectHypothesis;
154 delete pPointCloudRegistration;
170 std::vector<CHypothesisPoint*> aForegroundPointsFromOldPoints;
172 ARMARX_VERBOSE_S << aForegroundPointsFromOldPoints.size() <<
" points for foreground hypotheses";
175 std::vector<std::vector<CHypothesisPoint*> > aForegroundPointsFromOldPointsClusters;
177 ARMARX_VERBOSE_S << aForegroundPointsFromOldPointsClusters.size() <<
" foreground hypotheses";
179 for (
size_t i = 0; i < aForegroundPointsFromOldPointsClusters.size(); i++)
186 Vec3d vCenter = {0, 0, 0};
188 for (
size_t j = 0; j < aForegroundPointsFromOldPointsClusters.at(i).size(); j++)
190 pForegroundHypo->
aNewPoints.push_back(aForegroundPointsFromOldPointsClusters.at(i).at(j)->GetCopy());
191 Math3d::AddToVec(vCenter, aForegroundPointsFromOldPointsClusters.at(i).at(j)->vPosition);
194 Math3d::MulVecScalar(vCenter, 1.0 / aForegroundPointsFromOldPointsClusters.at(i).size(), pForegroundHypo->
vCenter);
197 pObjectHypotheses->AddElement(pForegroundHypo);
208 const int nEffortLevel)
210 Mat3d mRot = Math3d::unit_mat;
211 Vec3d vTrans = Math3d::zero_vec;
212 float errorAtOriginalPosition = FLT_MAX;
217 ARMARX_VERBOSE_S <<
"CHypothesisValidationRGBD: Initial check at old object position: the transformation shows no motion.";
223 std::vector<Vec3d> aPossibleLocations;
227 std::vector<float> aPointMatchDistances;
228 std::vector<CColorICP::CPointXYZRGBI> aNearestNeighbors;
229 pPointCloudRegistration->
EstimateTransformation(pObjectHypothesis, mRot, vTrans, upwardsVector, nEffortLevel, &aPossibleLocations, &aNearestNeighbors, &aPointMatchDistances, 2 * errorAtOriginalPosition);
235 float fRotationAngle;
237 Math3d::GetAxisAndAngle(mRot, vTemp, fRotationAngle);
238 fRotationAngle *= 180.0f /
M_PI;
241 Math3d::MulMatVec(mRot, pObjectHypothesis->
vCenter, vTrans, vTemp);
242 float fDistance = Math3d::Distance(vTemp, pObjectHypothesis->
vCenter);
245 float fMotionMeasure = 2 * fRotationAngle + fDistance;
246 ARMARX_VERBOSE_S <<
"Motion measure: " << fMotionMeasure <<
" (" << fDistance <<
"mm, " << fRotationAngle <<
"deg)";
251 int nNoForeground = 0;
252 int nLargeDistance = 0;
258 pTransformedHypo->
eType = pObjectHypothesis->
eType;
265 for (
size_t j = 0; j < pObjectHypothesis->
aNewPoints.size(); j++)
267 pPoint = pObjectHypothesis->
aNewPoints.at(j)->GetCopy();
324 pTransformedHypo->
aNewPoints.push_back(pPoint);
374 ARMARX_VERBOSE_S <<
"Discarded features: no foreground " << nNoForeground <<
", bad match " << nLargeDistance;
384 std::vector<Vec2d> aPointPositions2D;
389 calibration->WorldToImageCoordinates(pTransformedHypo->
aConfirmedPoints.at(i)->vPosition, aPointPositions2D.at(i),
false);
394 const Vec2d vPos2D = aPointPositions2D.at(i);
406 aPointPositions2D.at(j) = aPointPositions2D.at(aPointPositions2D.size() - 1);
412 ARMARX_VERBOSE_S <<
"Discarded " << nTooClose <<
" points because they were too close to another one";
417 std::vector<CHypothesisPoint*> aNewCandidatePoints;
418 std::vector<Vec2d> aForegroundPoints2D, aConfirmedPoints2D, aNewAndDoubtablePoints2D;
419 std::vector<Vec3d> aForegroundPoints3D, aConfirmedPoints3D;
424 for (
size_t i = 0; i < aForegroundPoints.size(); i++)
426 Vec3d point3D = aForegroundPoints.at(i)->vPosition;
427 aForegroundPoints3D.push_back(point3D);
429 calibration->WorldToImageCoordinates(point3D, point2D,
false);
430 aForegroundPoints2D.push_back(point2D);
439 aConfirmedPoints3D.push_back(point3D);
441 calibration->WorldToImageCoordinates(point3D, point2D,
false);
442 aConfirmedPoints2D.push_back(point2D);
451 calibration->WorldToImageCoordinates(pTransformedHypo->
aDoubtablePoints.at(i)->vPosition, point2D,
false);
452 aNewAndDoubtablePoints2D.push_back(point2D);
455 for (
size_t i = 0; i < pTransformedHypo->
aNewPoints.size(); i++)
458 calibration->WorldToImageCoordinates(pTransformedHypo->
aNewPoints.at(i)->vPosition, point2D,
false);
459 aNewAndDoubtablePoints2D.push_back(point2D);
466 Math3d::Transpose(mRot, mRotInv);
470 #pragma omp parallel for
471 for (
size_t j = 0; j < aForegroundPoints.size(); j++)
474 Vec3d vOldPointPosition;
475 Math3d::SubtractVecVec(aForegroundPoints.at(j)->vPosition, vTrans, vTemp);
476 Math3d::MulMatVec(mRotInv, vTemp, vOldPointPosition);
481 Vec2d vPos2D = aForegroundPoints2D.at(j);
482 Vec3d vPos3D = aForegroundPoints3D.at(j);
483 float fDist2D, fDist3D;
484 float fMinDist2D = FLT_MAX;
485 float fMinDist3D = FLT_MAX;
487 for (
size_t k = 0; k < aConfirmedPoints2D.size(); k++)
489 fDist2D = Math2d::Distance(vPos2D, aConfirmedPoints2D.at(k));
491 if (fDist2D < fMinDist2D)
493 fMinDist2D = fDist2D;
496 fDist3D = Math3d::Distance(vPos3D, aConfirmedPoints3D.at(k));
498 if (fDist3D < fMinDist3D)
500 fMinDist3D = fDist3D;
506 for (
size_t k = 0; k < aNewAndDoubtablePoints2D.size(); k++)
508 fDist2D = Math2d::Distance(vPos2D, aNewAndDoubtablePoints2D.at(k));
510 if (fDist2D < fMinDist2D)
512 fMinDist2D = fDist2D;
516 if (fMinDist2D > fMinDistanceLimit2D)
520 aNewCandidatePoints.push_back(aForegroundPoints.at(j)->GetCopy());
527 for (
size_t i = 0; i < aNewCandidatePoints.size(); i++)
542 pTransformedHypo->
aNewPoints.push_back(aNewCandidatePoints.at(i));
559 #ifdef OLP_ADD_POINTS_FROM_SEGMENTED_REGION
560 CByteImage* pSegmentationMask = NULL;
564 for (
size_t i = pTransformedHypo->
aVisibleConfirmedPoints.size(); i < pTransformedHypo->aConfirmedPoints.size(); i++)
573 Vec3d vNewCenter = {0, 0, 0};
605 pConfirmedHypothesis = pTransformedHypo;
612 ARMARX_VERBOSE_S <<
"CHypothesisValidationRGBD: not enough points left after transformation";
613 delete pTransformedHypo;
620 ARMARX_VERBOSE_S <<
"CHypothesisValidationRGBD: the transformation shows no motion.";