contrib/mul/mipa/tools/mipa_orient_histo.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Tool to compute an orientation histogram image from an input image
00004 // \author Tim Cootes
00005 
00006 #include <vcl_iostream.h>
00007 #include <vxl_config.h> // for vxl_byte
00008 #include <vil/vil_load.h>
00009 #include <vil/vil_save.h>
00010 #include <vil/vil_image_view.h>
00011 #include <mipa/mipa_orientation_histogram.h>
00012 #include <vil/vil_convert.h>
00013 #include <vul/vul_arg.h>
00014 
00015 void print_usage()
00016 {
00017   vcl_cout<<"mipa_orient_histo  -i src_image -o out_image -c 4\n"
00018           <<"Tool to compute an orientation histogram image from an input image.\n"
00019           <<"Uses 4 orientation bins, outputs 3-plane image with first three.\n"
00020           <<"Each histogram is computed from pixels in square of size cell_size"<<vcl_endl;
00021 }
00022 
00023 int main(int argc, char** argv)
00024 {
00025   vul_arg<vcl_string> in_path("-i","Input image");
00026   vul_arg<vcl_string> out_path("-o","Output path","output.jpg");
00027   vul_arg<unsigned> cell_size("-c","Cell size",4);
00028   vul_arg_parse(argc, argv);
00029 
00030   if (in_path() == "")
00031   {
00032     print_usage();
00033     vul_arg_display_usage_and_exit();
00034   }
00035 
00036   vil_image_view<vxl_byte> src_im = vil_load(in_path().c_str());
00037   if (src_im.size()==0)
00038   {
00039     vcl_cout<<"Unable to load source image from "<<in_path()<<vcl_endl;
00040     return 1;
00041   }
00042 
00043   vil_image_view<float> hog_image;
00044 
00045   if (src_im.nplanes()==1)
00046     mipa_orientation_histogram(src_im,hog_image,4,cell_size(),false);
00047   else
00048   {
00049     vil_image_view<float> grey_im;
00050     vil_convert_planes_to_grey(src_im,grey_im);
00051     mipa_orientation_histogram(grey_im,hog_image,4,cell_size(),false);
00052   }
00053 
00054   // Save first 3 planes, so can view as colour image.
00055   vil_image_view<vxl_byte> dest_im;
00056   vil_convert_stretch_range(vil_planes(hog_image,0,1,3),dest_im);
00057   if (!vil_save(dest_im, out_path().c_str()))
00058   {
00059     vcl_cerr<<"Unable to save result image to "<<out_path()<<vcl_endl;
00060     return 1;
00061   }
00062 
00063   return 0;
00064 }