27 #include <VirtualRobot/MathTools.h>
42 #include "Image/ByteImage.h"
43 #include "Image/ImageProcessor.h"
44 #include <Calibration/Calibration.h>
45 #include <Color/ColorParameterSet.h>
48 #include "Locators/VisualTargetLocator.h"
64 useVision = getProperty<bool>(
"UseVision").getValue();
65 objectColor = getProperty<ObjectColor>(
"BlobColor").getValue();
70 stereoMatcher =
new CStereoMatcher();
77 getProxy<armarx::RobotStateComponentInterfacePrx>(
"RobotStateComponent");
82 debugDrawerPrx = getTopic<armarx::DebugDrawerInterfacePrx>(
"DebugDrawerUpdates");
98 ARMARX_IMPORTANT <<
" xxxxxxxxxxx BigBowlLocalization initRecognizer!!!!!!!!!!";
100 std::string colorParameterFilename =
101 getProperty<std::string>(
"ColorParameterFile").getValue();
104 colorParameterFilename))
106 ARMARX_ERROR <<
"Could not find color parameter file in ArmarXDataPath: "
107 << colorParameterFilename;
111 ARMARX_VERBOSE <<
"Color parameter file name: " << colorParameterFilename;
113 visualTargetLocator =
new CVisualTargetLocator();
119 int nMinPixelsPerRegion = getProperty<int>(
"nMinPixelsPerRegion").getValue();
120 double dMaxEpipolarDiff = getProperty<double>(
"dMaxEpipolarDiff").getValue();
121 double dMinFilledRatio = getProperty<double>(
"dMinFilledRatio").getValue();
122 double dMaxSideRatio = getProperty<double>(
"dMaxSideRatio").getValue();
123 double dMinSize = getProperty<double>(
"dMinSize").getValue();
124 double dMaxSize = getProperty<double>(
"dMaxSize").getValue();
126 visualTargetLocator->SetParameters(nMinPixelsPerRegion,
133 colorParameterSet =
new CColorParameterSet();
134 colorParameterSet->LoadFromFile(colorParameterFilename.c_str());
141 showSegmentedResultImages = getProperty<bool>(
"ShowSegmentedResultImages").getValue();
143 if (!showSegmentedResultImages)
151 memoryx::ObjectLocalizationResultList
153 CByteImage** cameraImages,
154 armarx::MetaInfoSizeBasePtr imageMetaInfo,
155 CByteImage** resultImages)
159 << (IceUtil::Time::now() -
161 .toMilliSecondsDouble()
164 localRobot, robotStateComponent,
imageMetaInfo->timeProvided))
167 <<
"Failed to synchronize local robot - results are probably wrong";
174 const std::string agentName = localRobot->getName();
176 Eigen::Vector3f expectedPositionVec;
177 expectedPositionVec << 0, 600, 1000;
178 const std::string rootFrameName = localRobot->getRootNode()->getName();
183 memoryx::ObjectLocalizationResultList resultList;
188 Vec3d bigBowlPosition;
189 int numFoundBlobs = 0;
192 ARMARX_INFO <<
"Found " << numFoundBlobs <<
" blobs.";
194 if (numFoundBlobs == 0)
201 Eigen::Vector3f position;
202 position << bigBowlPosition.x, bigBowlPosition.y, bigBowlPosition.z;
203 float diffToExpectedPosition = (position - expectedPosition->toEigen()).
norm();
205 if (diffToExpectedPosition < 1200)
208 memoryx::ObjectLocalizationResult result;
209 result.recognitionCertainty = 0.96f;
210 result.objectClassName = objectClassNames.at(0);
214 positionPtr->changeFrame(localRobot, localRobot->getRootNode()->getName());
215 positionPtr->z = 1030;
216 result.position = positionPtr;
218 VirtualRobot::MathTools::rpy2eigen4f(-
M_PI / 2, 0, 0, orientation);
222 result.orientation = ori;
226 resultList.push_back(result);
229 ARMARX_INFO <<
"position >> x: " << positionPtr->x <<
" y: " << positionPtr->y
230 <<
" z: " << positionPtr->z <<
" frame: " << positionPtr->frame;
232 ARMARX_INFO <<
"globalposition >> x: " << globalPosPtr->x
233 <<
" y: " << globalPosPtr->y <<
" z: " << globalPosPtr->z
234 <<
" frame: " << globalPosPtr->frame;
239 currPose.setIdentity();
241 currPose(0, 3) = globalPosPtr->x;
242 currPose(1, 3) = globalPosPtr->y;
243 currPose(2, 3) = globalPosPtr->z;
247 Eigen::Vector3f
v(100, 100, 100);
252 "objLocalizationLayer", poseGlobal,
s, armarx::DrawColor{1, 0, 0, 1});
263 <<
"Object is too far away from expected position: "
264 << (int)diffToExpectedPosition;
270 ARMARX_INFO <<
"Vision is switched off, returning fake result";
272 memoryx::ObjectLocalizationResult result;
273 result.recognitionCertainty = 0.8f;
274 result.objectClassName = objectClassNames.at(0);
276 result.position = expectedPosition;
283 resultList.push_back(result);
292 BigBowlLocalization::drawCrossInImage(CByteImage* image, Eigen::Vector3f point,
bool leftCamera)
294 Vec3d positionIVT = {point(0), point(1), point(2)};
295 Vec2d projectedPoint;
309 ARMARX_INFO <<
"projectedPoint.x :" << projectedPoint.x
310 <<
" projectedPoint.y: " << projectedPoint.y;
312 if (size < projectedPoint.x && size < projectedPoint.y &&
313 projectedPoint.x < image->width - size - 1 &&
314 projectedPoint.y < image->height - size - 3)
316 for (
int i = -size; i <= size; i++)
318 int indexHorizontalLine =
319 3 * ((int)projectedPoint.y * image->width + (
int)projectedPoint.x + i);
320 int indexVerticalLine =
321 3 * (((int)projectedPoint.y + i) * image->width + (int)projectedPoint.x);
322 image->pixels[indexHorizontalLine] = 255;
323 image->pixels[indexHorizontalLine + 1] = 255;
324 image->pixels[indexHorizontalLine + 2] = 255;
325 image->pixels[indexVerticalLine] = 255;
326 image->pixels[indexVerticalLine + 1] = 255;
327 image->pixels[indexVerticalLine + 2] = 255;
333 BigBowlLocalization::locateBowl(CByteImage** cameraImages,
335 CByteImage** resultImages)
337 int numberOfSegments = 0;
338 CByteImage* imageHSVLeft =
340 CByteImage* imageHSVRight =
342 CByteImage* imageFiltered =
344 CByteImage* imgLeftGray =
346 CByteImage* imgRightGray =
349 ::ImageProcessor::CalculateHSVImage(
cameraImages[0], imageHSVLeft);
350 ::ImageProcessor::CalculateHSVImage(
cameraImages[1], imageHSVRight);
351 ::ImageProcessor::ConvertImage(
cameraImages[0], imgLeftGray);
352 ::ImageProcessor::ConvertImage(
cameraImages[1], imgRightGray);
353 ::ImageProcessor::Zero(imageFiltered);
355 int ThesholdHMinValue = 4;
356 int ThesholdHMaxValue = 20;
357 int ThesholdSMinValue = 220;
358 int ThesholdSMaxValue = 255;
360 int imagePosXLeft = 0;
361 int imagePosYLeft = 0;
362 int totalPixelLeft = 0;
363 int imagePosXRight = 0;
364 int imagePosYRight = 0;
365 int totalPixelRight = 0;
369 for (
int i = deltaROI; i < imageHSVLeft->width - deltaROI; i++)
371 for (
int j = deltaROI; j < imageHSVLeft->height - deltaROI; j++)
373 int pixelIndex = (i + j * imageHSVLeft->width) * imageHSVLeft->bytesPerPixel;
374 double hVal = (
double)imageHSVLeft->pixels[pixelIndex];
375 double sVal = (double)imageHSVLeft->pixels[pixelIndex + 1];
377 if ((hVal > ThesholdHMinValue && hVal < ThesholdHMaxValue) &&
378 (sVal > ThesholdSMinValue && sVal < ThesholdSMaxValue))
380 imageFiltered->pixels[pixelIndex] =
cameraImages[0]->pixels[pixelIndex];
381 imageFiltered->pixels[pixelIndex + 1] =
cameraImages[0]->pixels[pixelIndex + 1];
382 imageFiltered->pixels[pixelIndex + 2] =
cameraImages[0]->pixels[pixelIndex + 2];
389 ARMARX_INFO <<
"totalPixel >> left " << totalPixelLeft;
393 for (
int i = deltaROI; i < imageHSVRight->width - deltaROI; i++)
395 for (
int j = deltaROI; j < imageHSVRight->height - deltaROI; j++)
397 int pixelIndex = (i + j * imageHSVRight->width) * imageHSVRight->bytesPerPixel;
398 double hVal = (
double)imageHSVRight->pixels[pixelIndex];
399 double sVal = (double)imageHSVRight->pixels[pixelIndex + 1];
401 if ((hVal > ThesholdHMinValue && hVal < ThesholdHMaxValue) &&
402 (sVal > ThesholdSMinValue && sVal < ThesholdSMaxValue))
410 ARMARX_INFO <<
"totalPixel >> right " << totalPixelRight;
412 if (totalPixelLeft > 10000 && totalPixelRight > 10000)
414 numberOfSegments = 1;
420 ::ImageProcessor::Zero(imageFiltered);
422 numberOfSegments = 0;
423 return numberOfSegments;
426 Vec2d imageCoorLeft, imageCoorRight;
427 imageCoorLeft.x = (int)imagePosXLeft / totalPixelLeft;
428 imageCoorLeft.y = (int)imagePosYLeft / totalPixelLeft;
429 imageCoorRight.x = (int)imagePosXRight / totalPixelRight;
430 imageCoorRight.y = (int)imagePosYRight / totalPixelRight;
432 ARMARX_INFO <<
"imageCoorLeft x: " << imageCoorLeft.x <<
" y: " << imageCoorLeft.y;
433 ARMARX_INFO <<
"imageCoorRight x: " << imageCoorRight.x <<
" y: " << imageCoorRight.y;
435 if (imageCoorLeft.x < 250 || imageCoorLeft.x > 470 || imageCoorLeft.y < 150 ||
436 imageCoorLeft.y > 320)
440 ::ImageProcessor::Zero(imageFiltered);
442 numberOfSegments = 0;
443 return numberOfSegments;
446 Vec2d vCorrespondingPointRight;
447 const int nDispMin = stereoMatcher->GetDisparityEstimate(10000);
448 const int nDispMax = stereoMatcher->GetDisparityEstimate(500);
452 int nMatchingResult = stereoMatcher->Match(imgLeftGray,
454 (
int)imageCoorLeft.x,
455 (
int)imageCoorLeft.y,
459 vCorrespondingPointRight,
465 ARMARX_INFO <<
"nMatchingResult " << nMatchingResult;
469 if (nMatchingResult <= 0)
473 ::ImageProcessor::Zero(imageFiltered);
475 numberOfSegments = 0;
476 return numberOfSegments;
480 int deltaCenter = 10;
481 for (
int i = imageCoorLeft.x - deltaCenter; i < imageCoorLeft.x + deltaCenter; i++)
483 for (
int j = imageCoorLeft.y - deltaCenter; j < imageCoorLeft.y + deltaCenter; j++)
485 int pixelIndex = (i + j * imageHSVLeft->width) * imageHSVLeft->bytesPerPixel;
486 imageFiltered->pixels[pixelIndex] = 255;
487 imageFiltered->pixels[pixelIndex + 1] = 255;
488 imageFiltered->pixels[pixelIndex + 2] = 255;
506 return numberOfSegments;