34 #include <VisionX/interface/components/VisualContactDetection.h>
35 #include <VisionX/interface/components/HandLocalization.h>
44 #include <MemoryX/interface/observers/ObjectMemoryObserverInterface.h>
47 #include <Math/Math3d.h>
48 #include <Math/Math2d.h>
49 #include <Image/IplImageAdaptor.h>
53 #include <opencv2/highgui/highgui.hpp>
56 class CStereoCalibration;
62 class CHandLocalisation;
63 class CHandModelVisualizer;
73 defineOptionalProperty<std::string>(
"ImageProviderAdapterName",
"Armar3ImageProvider",
"Ice Adapter name of the image provider");
74 defineOptionalProperty<std::string>(
"RobotStateProxyName",
"RobotStateComponent",
"Ice Adapter name of the robot state proxy");
75 defineOptionalProperty<std::string>(
"HandFrameName",
"TCP R",
"Name of the robot state frame of the hand that will be localized");
76 defineOptionalProperty<std::string>(
"CameraFrameName",
"EyeLeftCamera",
"Name of the robot state frame of the primary camera");
77 defineOptionalProperty<int>(
"NumberOfParticles", 1500,
"Number of particles for the particle filter for hand localization");
78 defineOptionalProperty<std::string>(
"ObjectMemoryObserverName",
"ObjectMemoryObserver",
"Name of the object memory observer proxy");
79 defineOptionalProperty<std::string>(
"HandNameInMemoryX",
"handright3b",
"Name of the hand in the object memory");
80 defineOptionalProperty<int>(
"MinNumPixelsInClusterForCollisionDetection", 3000,
"Minimal number of pixels that a motion cluster must contain to be considered in collision detection");
81 defineOptionalProperty<bool>(
"UseHandLocalization",
true,
"Switch on or off whether the hand should be localized with the particle filter. If it is switched off, the hand pose from MemoryX is used (if available, otherwise just from the kinematic model).");
82 defineOptionalProperty<int>(
"MinWaitingTimeBetweenTwoFrames", 300,
"Minimal time to wait between two frames so that enough motion happens (in ms)");
102 virtual public visionx::VisualContactDetectionInterface
111 return "VisualContactDetection";
118 armarx::FramedPoseBasePtr
getHandPose(
const Ice::Current&
c = Ice::emptyCurrent)
override;
123 visionx::FramedPositionBaseList
getFingertipPositions(
const Ice::Current&
c = Ice::emptyCurrent)
override;
125 void activate(
const Ice::Current&
c = Ice::emptyCurrent)
override;
126 void deactivate(
const Ice::Current&
c = Ice::emptyCurrent)
override;
150 std::string providerName;
151 ImageProviderInterfacePrx imageProviderPrx;
152 std::string robotStateProxyName;
154 std::string handFrameName, cameraFrameName;
155 memoryx::ObjectMemoryObserverInterfacePrx objectMemoryObserver;
156 std::string handNameInMemoryX;
158 bool useHandLocalization;
160 std::mutex activationStateMutex;
163 CStereoCalibration* stereoCalibration;
164 CByteImage** cameraImages;
165 CByteImage** resultImages;
169 CHandModelVisualizer* handModelVisualizer, *handModelVisualizer1, *handModelVisualizer2, *handModelVisualizer3;
170 static const bool drawComplexHandModelInResultImage =
false;
172 void calculateOpticalFlowClusters();
173 IplImage* pCamLeftIpl, *pCamLeftOldIpl;
174 CByteImage* camImgLeftGrey, *camImgLeftGreySmall, *camImgLeftGreyOld, *camImgLeftGreyOldSmall, *tempImageRGB, *pSegmentedImage, *pOpticalFlowClusterImage;
176 Vec3d oldHandPosSensor;
177 std::vector<std::vector<std::vector<float> > > opticalFlowClusters;
178 float* pVisOptFlowRaw;
180 static const int imageResizeFactorForOpticalFlowCalculation = 4;
181 static const int maxNumOptFlowClusters = 5;
182 int minNumPixelsInClusterForCollisionDetection;
183 static const int clusteringSampleStep = 1;
184 static constexpr
float pushDetectionBoxForwardOffsetToTCP = 150.0f;
185 float oldCollisionProbability;
187 bool detectCollision();
188 Vec2d handPos2D, pushTarget2D, pushTargetBoxLeftUpperCorner, pushTargetBoxRightLowerCorner;
190 void drawVisualization(
bool collisionDetected);
191 CByteImage* tempImageRGB1, *tempImageRGB2, *tempImageRGB3, *tempImageRGB4;
193 static const bool recordImages =
true;
194 int visualizationImageNumber;
195 std::vector<CByteImage*> cameraImagesForSaving, localizationResultImages, opticalFlowImages, opticalFlowClusterImages;
196 std::vector<long> timesOfImageCapture;
198 static void extractAnglesFromRotationMatrix(
const Mat3d& mat,
Vec3d& angles);
199 static IplImage* convertToIplImage(CByteImage* pByteImageRGB);
200 static void clusterXMeans(
const std::vector<std::vector<float> >& aPoints,
const int nMinNumClusters,
const int nMaxNumClusters,
const float fBICFactor,
201 std::vector<std::vector<std::vector<float> > >& aaPointClusters, std::vector<std::vector<int> >& aaOldIndices);
203 void SetNumberInFileName(std::string& sFileName,
int nNumber,
int nNumDigits = 4);
205 VisualContactDetectionListenerPrx listener;
207 enum EResultImageIndices
217 eNumberOfResultImages