contrib/mul/ipts/tools/find_dog_peaks.cxx
Go to the documentation of this file.
00001 // Find all local peaks in DoG scale space of an image
00002 
00003 #include <vimt/vimt_dog_pyramid_builder_2d.h>
00004 #include <vimt/vimt_image_pyramid.h>
00005 #include <vil/vil_convert.h>
00006 #include <vul/vul_arg.h>
00007 #include <vil/vil_load.h>
00008 #include <vil/vil_save.h>
00009 #include <vgl/vgl_point_3d.h>
00010 #include <ipts/ipts_scale_space_peaks.h>
00011 #include <ipts/ipts_draw.h>
00012 
00013 void print_usage()
00014 {
00015   vcl_cout<<"find_dog_peaks -i input_image -o out_image -d dog_image"<<vcl_endl;
00016 }
00017 
00018 int main( int argc, char* argv[] )
00019 {
00020   vul_arg<vcl_string> in_path("-i","Input image path");
00021   vul_arg<vcl_string> out_path("-o","Output image file (peaks)");
00022   vul_arg<vcl_string> dog_path("-d","Output image file (DOG)","dog.pnm");
00023   vul_arg<vcl_string> smooth_path("-s","Output image file (Smooth )","smooth.pnm");
00024   vul_arg<float> threshold("-t","Threshold on DoG value",2.0f);
00025   vul_arg_parse(argc, argv);
00026 
00027   if (in_path() == "")
00028   {
00029     print_usage();
00030     vul_arg_display_usage_and_exit();
00031   }
00032 
00033   vil_image_view<vxl_byte> image = vil_load(in_path().c_str());
00034   if (image.ni()==0)
00035   {
00036     vcl_cout<<"Failed to load image."<<vcl_endl;
00037     return 1;
00038   }
00039 
00040   vimt_image_2d_of<float> image_f,flat_dog,flat_smooth;
00041   vil_convert_cast(image,image_f.image());
00042 
00043   // Invert image, so we pick out troughs
00044 //  vil_math_scale_values(image_f.image(),-1.0);
00045 
00046   vimt_image_pyramid dog_pyramid,smooth_pyramid;
00047   vimt_dog_pyramid_builder_2d<float> pyr_builder;
00048   pyr_builder.build_dog(dog_pyramid,smooth_pyramid,image_f,true);
00049 
00050   vcl_vector<vgl_point_3d<double> > peak_pts;
00051   ipts_scale_space_peaks_2d(peak_pts,dog_pyramid,threshold());
00052   vcl_cout<<"Found "<<peak_pts.size()<<" peaks."<<vcl_endl;
00053 
00054   for (unsigned i=0;i<peak_pts.size();++i)
00055   {
00056     if (peak_pts[i].z()>1.1)
00057     ipts_draw_cross(image,int(peak_pts[i].x()+0.5),
00058                      int(peak_pts[i].y()+0.5),
00059                      int(peak_pts[i].z()+0.5), vxl_byte(255) );
00060   }
00061 
00062   vimt_image_pyramid_flatten(flat_dog,dog_pyramid);
00063   vimt_image_pyramid_flatten(flat_smooth,smooth_pyramid);
00064 
00065   vil_save(image,out_path().c_str());
00066   vcl_cout<<"Image + pts saved to "<<out_path()<<vcl_endl;
00067 
00068   vil_image_view<vxl_byte> out_dog;
00069   vil_convert_stretch_range(flat_dog.image(),out_dog);
00070   vil_save(out_dog,dog_path().c_str());
00071   vcl_cout<<"DoG pyramid saved to "<<dog_path()<<vcl_endl;
00072 
00073   vil_image_view<vxl_byte> out_smooth;
00074   vil_convert_stretch_range(flat_smooth.image(),out_smooth);
00075   vil_save(out_smooth,smooth_path().c_str());
00076   vcl_cout<<"Smooth pyramid saved to "<<smooth_path()<<vcl_endl;
00077 
00078   return 0;
00079 }