34 #include <VisionX/interface/components/HandLocalization.h>
35 #include <VisionX/interface/components/VisualContactDetection.h>
45 #include <MemoryX/interface/observers/ObjectMemoryObserverInterface.h>
48 #include <Image/IplImageAdaptor.h>
49 #include <Math/Math2d.h>
50 #include <Math/Math3d.h>
54 #include <opencv2/highgui/highgui.hpp>
57 class CStereoCalibration;
63 class CHandLocalisation;
64 class CHandModelVisualizer;
73 defineOptionalProperty<std::string>(
"ImageProviderAdapterName",
74 "Armar3ImageProvider",
75 "Ice Adapter name of the image provider");
76 defineOptionalProperty<std::string>(
"RobotStateProxyName",
77 "RobotStateComponent",
78 "Ice Adapter name of the robot state proxy");
79 defineOptionalProperty<std::string>(
82 "Name of the robot state frame of the hand that will be localized");
83 defineOptionalProperty<std::string>(
86 "Name of the robot state frame of the primary camera");
87 defineOptionalProperty<int>(
90 "Number of particles for the particle filter for hand localization");
91 defineOptionalProperty<std::string>(
"ObjectMemoryObserverName",
92 "ObjectMemoryObserver",
93 "Name of the object memory observer proxy");
94 defineOptionalProperty<std::string>(
95 "HandNameInMemoryX",
"handright3b",
"Name of the hand in the object memory");
96 defineOptionalProperty<int>(
"MinNumPixelsInClusterForCollisionDetection",
98 "Minimal number of pixels that a motion cluster must "
99 "contain to be considered in collision detection");
100 defineOptionalProperty<bool>(
101 "UseHandLocalization",
103 "Switch on or off whether the hand should be localized with the particle filter. "
104 "If it is switched off, the hand pose from MemoryX is used (if available, "
105 "otherwise just from the kinematic model).");
106 defineOptionalProperty<int>(
107 "MinWaitingTimeBetweenTwoFrames",
109 "Minimal time to wait between two frames so that enough motion happens (in ms)");
129 virtual public visionx::VisualContactDetectionInterface
138 return "VisualContactDetection";
144 armarx::FramedPoseBasePtr
getHandPose(
const Ice::Current&
c = Ice::emptyCurrent)
override;
149 visionx::FramedPositionBaseList
153 void activate(
const Ice::Current&
c = Ice::emptyCurrent)
override;
154 void deactivate(
const Ice::Current&
c = Ice::emptyCurrent)
override;
176 std::string providerName;
177 ImageProviderInterfacePrx imageProviderPrx;
178 std::string robotStateProxyName;
180 std::string handFrameName, cameraFrameName;
181 memoryx::ObjectMemoryObserverInterfacePrx objectMemoryObserver;
182 std::string handNameInMemoryX;
184 bool useHandLocalization;
186 std::mutex activationStateMutex;
189 CStereoCalibration* stereoCalibration;
190 CByteImage** cameraImages;
191 CByteImage** resultImages;
196 *handModelVisualizer3;
197 static const bool drawComplexHandModelInResultImage =
false;
199 void calculateOpticalFlowClusters();
200 IplImage *pCamLeftIpl, *pCamLeftOldIpl;
201 CByteImage *camImgLeftGrey, *camImgLeftGreySmall, *camImgLeftGreyOld,
202 *camImgLeftGreyOldSmall, *tempImageRGB, *pSegmentedImage, *pOpticalFlowClusterImage;
204 Vec3d oldHandPosSensor;
205 std::vector<std::vector<std::vector<float>>> opticalFlowClusters;
206 float* pVisOptFlowRaw;
208 static const int imageResizeFactorForOpticalFlowCalculation = 4;
209 static const int maxNumOptFlowClusters = 5;
210 int minNumPixelsInClusterForCollisionDetection;
211 static const int clusteringSampleStep = 1;
212 static constexpr
float pushDetectionBoxForwardOffsetToTCP = 150.0f;
213 float oldCollisionProbability;
215 bool detectCollision();
216 Vec2d handPos2D, pushTarget2D, pushTargetBoxLeftUpperCorner, pushTargetBoxRightLowerCorner;
218 void drawVisualization(
bool collisionDetected);
219 CByteImage *tempImageRGB1, *tempImageRGB2, *tempImageRGB3, *tempImageRGB4;
221 static const bool recordImages =
true;
222 int visualizationImageNumber;
223 std::vector<CByteImage*> cameraImagesForSaving, localizationResultImages, opticalFlowImages,
224 opticalFlowClusterImages;
225 std::vector<long> timesOfImageCapture;
227 static void extractAnglesFromRotationMatrix(
const Mat3d& mat,
Vec3d& angles);
228 static IplImage* convertToIplImage(CByteImage* pByteImageRGB);
229 static void clusterXMeans(
const std::vector<std::vector<float>>& aPoints,
230 const int nMinNumClusters,
231 const int nMaxNumClusters,
232 const float fBICFactor,
233 std::vector<std::vector<std::vector<float>>>& aaPointClusters,
234 std::vector<std::vector<int>>& aaOldIndices);
236 void SetNumberInFileName(std::string& sFileName,
int nNumber,
int nNumDigits = 4);
238 VisualContactDetectionListenerPrx listener;
240 enum EResultImageIndices
250 eNumberOfResultImages