contrib/mul/ipts/tools/show_orientation_entropy.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Experimental tool to load in an image and compute orientation entropy.
00004 // \author Tim Cootes
00005 
00006 #include <vcl_iostream.h>
00007 #include <vul/vul_arg.h>
00008 #include <vxl_config.h> // for vxl_byte
00009 #include <vil/vil_load.h>
00010 #include <vil/vil_save.h>
00011 #include <vil/vil_image_view.h>
00012 #include <vil/vil_convert.h>
00013 #include <vil/vil_math.h>
00014 #include <vil/algo/vil_orientations.h>
00015 #include <vgl/vgl_point_2d.h>
00016 #include <ipts/ipts_local_entropy.h>
00017 #include <vimt/algo/vimt_find_peaks.h>
00018 #include <ipts/ipts_draw.h>
00019 
00020 void print_usage()
00021 {
00022   vcl_cout<<"show_orientation_entropy -i in_image -e entropy_image -o out_image -h half_width -a angle_image\n"
00023           <<"Load in an image and generate entropy of orientatins in square regions.\n"
00024           <<"Show peaks in entropy as crosses on original image.\n";
00025 }
00026 
00027 int main( int argc, char* argv[] )
00028 {
00029   vul_arg<vcl_string> in_path("-i","Input image");
00030   vul_arg<vcl_string> entropy_path("-e","Entropy image","entropy.jpg");
00031   vul_arg<vcl_string> out_path("-o","Output image","output.jpg");
00032   vul_arg<vcl_string> angle_path("-a","Angle image","angle.pnm");
00033   vul_arg<int> half_width("-h","Half width of RIO",10);
00034   vul_arg<float> threshold("-t","Threshold on edge magnitude",5.0f);
00035   vul_arg_parse(argc, argv);
00036 
00037   if (in_path() == "")
00038   {
00039     print_usage();
00040     vul_arg_display_usage_and_exit();
00041   }
00042 
00043   vil_image_view<vxl_byte> src_im = vil_load(in_path().c_str());
00044   if (src_im.size()==0)
00045   {
00046     vcl_cout<<"Unable to load source image from "<<in_path()<<vcl_endl;
00047     return 1;
00048   }
00049 
00050   vil_image_view<vxl_byte> orient_im,grey_im;
00051   vil_image_view<float> grad_mag,grad_i,grad_j;
00052   vil_math_mean_over_planes(src_im,grey_im);
00053 
00054   unsigned n_orientations = 16;
00055 
00056   vil_sobel_3x3(grey_im,grad_i,grad_j);
00057   vil_orientations_at_edges(grad_i,grad_j,orient_im,grad_mag,threshold(),n_orientations);
00058   vil_image_view<float> entropy_im,entropy_max_im;
00059   ipts_local_entropy(orient_im,entropy_im,half_width(),0,n_orientations);
00060 
00061 //vil_suppress_non_max_3x3(entropy_im,entropy_max_im);
00062 
00063   vil_image_view<vxl_byte> dest_im;
00064   vil_convert_stretch_range(entropy_im,dest_im);
00065 
00066   if (!vil_save(dest_im, entropy_path().c_str()))
00067   {
00068     vcl_cerr<<"Unable to save entropy image to "<<entropy_path()<<vcl_endl;
00069     return 1;
00070   }
00071 
00072   vcl_cout<<"Saved image to "<<entropy_path()<<vcl_endl;
00073 
00074   vcl_vector<vgl_point_2d<unsigned> > peaks;
00075   vimt_find_image_peaks_3x3(peaks,entropy_im);
00076   for (unsigned i=0;i<peaks.size();++i)
00077     ipts_draw_cross(grey_im,peaks[i].x()+half_width(),peaks[i].y()+half_width(),half_width(),vxl_byte(255));
00078 
00079   if (!vil_save(grey_im, out_path().c_str()))
00080   {
00081     vcl_cerr<<"Unable to save result image to "<<out_path()<<vcl_endl;
00082     return 1;
00083   }
00084 
00085   vcl_cout<<"Saved image to "<<out_path()<<vcl_endl;
00086 
00087   if (!vil_save(orient_im, angle_path().c_str()))
00088   {
00089     vcl_cerr<<"Unable to save angle image to "<<out_path()<<vcl_endl;
00090     return 1;
00091   }
00092 
00093   vcl_cout<<"Saved orientation image to "<<angle_path()<<vcl_endl;
00094 
00095   return 0;
00096 }