contrib/mul/ipts/tools/find_scale_entropy_peaks.cxx
Go to the documentation of this file.
00001 // Find all local peaks in entropy of scale space of an image
00002 
00003 #include <ipts/ipts_entropy_pyramid.h>
00004 #include <vil/vil_convert.h>
00005 #include <vul/vul_arg.h>
00006 #include <vil/vil_load.h>
00007 #include <vil/vil_save.h>
00008 #include <vgl/vgl_point_3d.h>
00009 #include <ipts/ipts_scale_space_peaks.h>
00010 #include <ipts/ipts_draw.h>
00011 
00012 void print_usage()
00013 {
00014   vcl_cout<<"find_scale_entropy_peaks -i input_image -s scale_step -h half_width -t threshold -o out_image -e entropy_pyramid\n"
00015           <<"Generates scale pyramid\n"
00016           <<"computes local entropy in squares with given half width\n"
00017           <<"looks for local peaks in the entropy pyramid"<<vcl_endl;
00018 }
00019 
00020 int main( int argc, char* argv[] )
00021 {
00022   vul_arg<vcl_string> in_path("-i","Input image path");
00023   vul_arg<vcl_string> out_path("-o","Output image file (peaks)","output.pnm");
00024   vul_arg<int> half_width("-h","Half-width of square for entropy measurements",5);
00025   vul_arg<float> threshold("-t","Threshold for peak detection",-999);
00026   vul_arg<vcl_string> entropy_path("-e","Output image file for entropy pyramid)");
00027   vul_arg<double> scale("-s","Scale step",1.41);
00028 
00029   vul_arg_parse(argc, argv);
00030 
00031   if (in_path() == "")
00032   {
00033     print_usage();
00034     vul_arg_display_usage_and_exit();
00035   }
00036 
00037   vil_image_view<vxl_byte> image = vil_load(in_path().c_str());
00038   if (image.ni()==0)
00039   {
00040     vcl_cout<<"Failed to load image.\n";
00041     return 1;
00042   }
00043 
00044   vimt_image_2d_of<vxl_byte> image0,flat_smooth;
00045   vimt_image_2d_of<float> flat_entropy;
00046 
00047   image0.image() = image;  // Wrap vil_image_view in a vimt_image
00048 
00049   vimt_image_pyramid smooth_pyramid,entropy_pyramid;
00050 
00051   ipts_entropy_pyramid(image0,entropy_pyramid,smooth_pyramid,scale(),half_width());
00052 
00053 
00054   vcl_vector<vgl_point_3d<double> > peak_pts;
00055   ipts_scale_space_peaks_2d(peak_pts,entropy_pyramid,threshold());
00056   vcl_cout<<"Found "<<peak_pts.size()<<" peaks.\n";
00057 
00058   for (unsigned i=0;i<peak_pts.size();++i)
00059   {
00060     if (peak_pts[i].z()>1.1)
00061     ipts_draw_cross(image,int(peak_pts[i].x()+0.5),
00062                      int(peak_pts[i].y()+0.5),
00063                      int(peak_pts[i].z()+0.5), vxl_byte(255) );
00064   }
00065 
00066   vimt_image_pyramid_flatten(flat_entropy,entropy_pyramid);
00067 //  vimt_image_pyramid_flatten(flat_smooth,smooth_pyramid);
00068 
00069   vil_save(image,out_path().c_str());
00070   vcl_cout<<"Image + pts saved to "<<out_path()<<vcl_endl;
00071 
00072 
00073   if (entropy_path()!="")
00074   {
00075     vil_image_view<vxl_byte> out_entropy;
00076     vil_convert_stretch_range(flat_entropy.image(),out_entropy);
00077     vil_save(out_entropy,entropy_path().c_str());
00078     vcl_cout<<"entropy pyramid saved to "<<entropy_path()<<vcl_endl;
00079   }
00080 
00081   return 0;
00082 }