/******************************************************************************************
 * 
 * Monterey Bay Aquarium Research Institute
 * Francois Boudet summer intern 2007    
 * 07/24/2007
 *  
 * 
 * 
 * ******************************************************************************************/

#include "Raster/Raster.H"
#include "SIFT/Keypoint.H"
#include "MbariVisualObject.H"
#include "MbariVisualObjectDB.H"
#include "Image/DrawOps.H"
#include <time.h>

/*! Load a database, enrich it with new VisualObject entities
  extracted from the given images, and save it back. */

int main(const int argc, const char **argv)
{
  MYLOGVERB = LOG_INFO;

  // check command-line args:
  if (argc < 3 )
    LFATAL("USAGE: avedclassifier "); //<dbname.vdb> <imageComp.png> <fused.png> <classe> <image1.png>"
           //"... <imageN.png>");

  // load the database:
  MbariVisualObjectDB vdb;
  LINFO("Load DB \n");  
  int save = 0;
  if (!vdb.loadFrom(argv[1]))  
  		vdb.setName(argv[1]);
    			

  /*********************************************************************************************/
  /*******************              app match sift database              ***********************/
  /*********************************************************************************************/

  // get input image:
  Image< PixRGB<byte> > colim = Raster::ReadRGB(argv[2]);

  // create visual object and extract keypoints:
  rutz::shared_ptr<MbariVisualObject> vo(new MbariVisualObject(argv[2], argv[2], colim));

  // get the matching objects:
  std::vector< rutz::shared_ptr<MbariVisualObjectMatch> > matches;
  const uint nmatches = vdb.getObjectMatches(vo, matches, VOMA_KDTREEBBF);

  // prepare the fused image:
  Image< PixRGB<byte> > mimg;
  std::vector<Point2D> tl, tr, br, bl;

  // if no match, forget it:
  if (nmatches == 0U)
    LINFO("### No matching object found.");
  else
    {
      // let the user know about the matches:
      for (uint i = 0; i < nmatches; i ++)
        {
          rutz::shared_ptr<MbariVisualObjectMatch> vom = matches[i];
          rutz::shared_ptr<MbariVisualObject> obj = vom->getVoTest();

          LINFO("### Object match with '%s' score=%f",
                obj->getName().c_str(), vom->getScore());      
        }     
    }

  return 0;
}

