Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

BiColorMarkerData.cc

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #include <vector>
00003 
00004 #include "Shared/ProjectInterface.h"
00005 
00006 #include "DualCoding/ShapeMarker.h"
00007 #include "DualCoding/Sketch.h"
00008 
00009 #include "DualCoding/ShapeFuns.h"
00010 #include "DualCoding/BlobData.h"
00011 #include "DualCoding/ShapeBlob.h"
00012 #include "DualCoding/VRmixin.h"  // for camShS and mapBuilder
00013 #include "Crew/MapBuilder.h"
00014 
00015 #include "BiColorMarkerData.h"
00016 
00017 using namespace std;
00018 
00019 namespace DualCoding {
00020 
00021   const MarkerType_t BiColorMarkerData::biColorMarkerType =
00022     MarkerData::registerMarkerType("biColorMarkerType",BiColorMarkerData::extractMarkers);
00023 
00024   BiColorMarkerData::BiColorMarkerData(ShapeSpace& _space, const Point& _center, rgb _top, rgb _bottom) :
00025     MarkerData(_space, _center, _top), topColor(_top), bottomColor(_bottom)
00026   { typeOfMarker = biColorMarkerType; }
00027 
00028   DATASTUFF_CC(BiColorMarkerData);
00029 
00030   void BiColorMarkerData::printParams() const {
00031     cout << "Type = " << getTypeName() << "(BiColor)" << "  ID=" << getId() << "  ParentID=" << getParentId() << endl;
00032     printf("  top color = %d %d %d",topColor.red,topColor.green,topColor.blue);
00033     printf("  bottom color = %d %d %d\n",bottomColor.red,bottomColor.green,bottomColor.blue);
00034     cout << "  center =" << center.getCoords() << endl
00035    << endl;
00036   }
00037 
00038   bool BiColorMarkerData::isMatchingMarker(const Shape<MarkerData>& other) const {
00039     // make sure they are the same type
00040     if (!(typeOfMarker == other->typeOfMarker))
00041       return false;
00042   
00043     // make sure colors are the same
00044     const BiColorMarkerData& other_marker = (const BiColorMarkerData&)(other.getData());
00045     if (!(topColor == other_marker.topColor && bottomColor == other_marker.bottomColor))
00046       return false;  
00047 
00048     return true;
00049   }
00050 
00051   string BiColorMarkerData::getMarkerDescription() const {
00052     stringstream ss;
00053     ss << ProjectInterface::getColorName(topColor) << "/" << ProjectInterface::getColorName(bottomColor);
00054     ss << " coords=" << getCentroid();
00055     return ss.str();
00056   }
00057 
00058   vector<Shape<MarkerData> > 
00059   BiColorMarkerData::extractMarkers(const Sketch<uchar> &sketch, const MapBuilderRequest &req) {
00060 
00061     vector<Shape<MarkerData> > markers;
00062 
00063     // Figure out the designated height of the marker center above the ground plane
00064     std::map<MarkerType_t, coordinate_t>::const_iterator it = req.markerHeights.find(biColorMarkerType);
00065     coordinate_t markerHeight = (it != req.markerHeights.end()) ? it->second : MapBuilderRequest::defaultMarkerHeight;
00066 
00067     // Find the colors we're using for markers, so we can check each blob color
00068     std::map<ShapeType_t, std::set<color_index> >::const_iterator it_c = req.objectColors.find(markerDataType);
00069     if ( it_c == req.objectColors.end() )
00070       return markers;
00071     const std::set<color_index> &colors = it_c->second;
00072 
00073     NEW_SHAPEVEC(blobs, BlobData, select_type<BlobData>(VRmixin::camShS));
00074     if ( blobs.empty() ) {
00075       VRmixin::mapBuilder->getCamBlobs(colors, 50);
00076       blobs = select_type<BlobData>(VRmixin::camShS);
00077     }
00078   
00079     SHAPEVEC_ITERATE(blobs, BlobData, b1) {
00080       if ( colors.find(ProjectInterface::getColorIndex(b1->getColor())) == colors.end() )
00081   continue;
00082       Point c1 = b1->getCentroid();
00083     
00084       SHAPENEXT_ITERATE(blobs, BlobData, b1, b2) {
00085   // only look for markers with two different allowed colors
00086   if ( b1->getColor() == b2->getColor()  ||
00087        colors.find(ProjectInterface::getColorIndex(b2->getColor())) == colors.end() )
00088     continue;
00089       
00090   // widths and areas should be similar
00091   const float widthRatio = 
00092     (b1->topRight.coordX() - b1->topLeft.coordX()) /
00093     (b2->topRight.coordX() - b2->topLeft.coordX());
00094   if ( widthRatio < 0.5 || widthRatio > 2.0 )
00095     continue;
00096   const float areaRatio = b1->getArea()/b2->getArea();
00097   if ( areaRatio < 0.5 || areaRatio > 2.0 )
00098     continue;
00099       
00100   Point c2 = b2->getCentroid();
00101   float dy = abs(c1.coordY() - c2.coordY());
00102   float dx = abs(c1.coordX() - c2.coordX());
00103       
00104   float width  = max(b1->topRight.coordX() - b1->topLeft.coordX(),
00105          b2->topRight.coordX() - b2->topLeft.coordX());
00106   float height = max(b1->bottomLeft.coordY() - b1->topLeft.coordY(),
00107          b2->bottomLeft.coordY() - b2->topLeft.coordY());
00108       
00109   // should be roughly aligned on top of each other, but
00110   // relax the width in case the marker is only partly in the camera frame
00111   float const minWidthForComparison = 10;
00112   if ( (dx > max(width/2, minWidthForComparison)) || (dy > 1.75 * height) || (dy < 0.5 * height) )  // change 0.75*height to 0.5*height to accomodate distorted views of planar markers
00113     continue;
00114       
00115   // figure out which one is on top
00116   Shape<BlobData> &topBlob    = (c1.coordY() < c2.coordY()) ? b1 : b2;
00117   Shape<BlobData> &bottomBlob = (c1.coordY() < c2.coordY()) ? b2 : b1;
00118   
00119   Point center((c1.coordX()+c2.coordX())/2.0,
00120          (topBlob->getBoundingBox().max[1] + bottomBlob->getBoundingBox().min[1])/2.0,
00121          0,
00122          camcentric);
00123   calculateCameraDistance(center, markerHeight);
00124       
00125   BiColorMarkerData *marker = 
00126     new BiColorMarkerData(sketch->getSpace().getDualSpace(), center, 
00127         topBlob->getColor(), bottomBlob->getColor());
00128   markers.push_back(Shape<MarkerData>(marker));
00129       } END_ITERATE;
00130     } END_ITERATE;
00131   
00132     return markers;
00133   }
00134 
00135 }; // namespace

DualCoding 5.1CVS
Generated Mon May 9 04:56:25 2016 by Doxygen 1.6.3