/****************************************************************************/
/* Copyright 2012 MBARI.                                                    */
/* Monterey Bay Aquarium Research Institute Proprietary Information.        */
/* All rights reserved.                                                     */
/****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <windows.h>

/* OpenCV 2.4.2 includes: */
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/nonfree/nonfree.hpp>

int main(int argc, char** argv)
{
   char file_name[128];

   /* openCV vars */
   cv::SIFT sift;
   cv::Mat in_img;
   cv::Mat out_img_yaml;
   cv::Mat out_img_sift;
   
   cv::Mat descriptors;
   cv::vector<cv::KeyPoint> keypoints_sift;
   cv::vector<cv::KeyPoint> keypoints_yaml;
   
   cv::FileStorage fs;

/****************************************************************************/
/*                        startup and usage banners                         */
/****************************************************************************/
    
   printf("sifter built on %s at %s\n\n", __DATE__, __TIME__);

   if (argc != 2) 
   { 
       printf("Usage: sifter.exe [image number]\n");
       return -1;
   }

/****************************************************************************/
/*                        load the image and sift it                        */
/****************************************************************************/
    
   /* create the image file name */
   sprintf(file_name, "image%04d.jpg", atoi(argv[1]));

   /* load the image */
   in_img = cv::imread(file_name);

   /* peform the SIFT algorithm */
   sift(in_img, cv::Mat(), keypoints_sift, descriptors);

   /* open a display window named "image_sift" */
   cvNamedWindow("image_sift", CV_WINDOW_AUTOSIZE);
   
   /* draw the keypoints on an output image */
   drawKeypoints(in_img, keypoints_sift, out_img_sift);
   cv::imshow("image_sift", out_img_sift);

/****************************************************************************/
/*              load the yaml keypoints and draw the keypoionts             */
/****************************************************************************/
   
   /* note: a small pause seems to make the windows 
   appear side by side instead of overlapped */
   cv::waitKey(33);
   
   /* read keypoints from the ayml file */
   sprintf(file_name, "keypoints%04d.yml", atoi(argv[1]));
   fs.open(file_name, cv::FileStorage::READ);
   read(fs["key_points"], keypoints_yaml);
   fs.release();
   
   /* open a display window named "image_yaml" */
   cvNamedWindow("image_yaml", CV_WINDOW_AUTOSIZE);
   
   /* draw the keypoints on an output image */
   drawKeypoints(in_img, keypoints_yaml, out_img_yaml);
   cv::imshow("image_yaml", out_img_yaml);

   /* wait for user keypress before exiting */
   cv::waitKey(0);

   return 0;
}
