contrib/mul/msm/tools/msm_draw_points_on_image.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Creates an EPS file showing points and curves on images.
00004 // \author Tim Cootes
00005 
00006 #include <mbl/mbl_eps_writer.h>
00007 #include <msm/msm_points.h>
00008 #include <msm/utils/msm_draw_shape_to_eps.h>
00009 #include <vil/vil_resample_bilin.h>
00010 #include <vil/algo/vil_gauss_filter.h>
00011 #include <msm/msm_curve.h>
00012 #include <vgl/vgl_point_2d.h>
00013 #include <vul/vul_arg.h>
00014 #include <vil/vil_load.h>
00015 
00016 // Note: Currently doesn't quite deal with images correctly - need a
00017 // half pixel offset so (0,0) is the centre of the pixel.
00018 
00019 void print_usage()
00020 {
00021   vcl_cout << "msm_draw_points_on_image  -p points.pts -c curves.crvs -i image.jpg -o image+pts.eps\n"
00022            << "Load in points and curves.\n"
00023            << "Writes out eps file displaying the curves.\n"
00024            << "If image supplied, then includes that too."<<vcl_endl;
00025   vul_arg_display_usage_and_exit();
00026 }
00027 
00028 
00029 int main( int argc, char* argv[] )
00030 {
00031   vul_arg<vcl_string> curves_path("-c","File containing curves");
00032   vul_arg<vcl_string> pts_path("-p","File containing points");
00033   vul_arg<vcl_string> image_path("-i","Image");
00034   vul_arg<vcl_string> out_path("-o","Output path","image+pts.eps");
00035   vul_arg<vcl_string> line_colour("-lc","Line colour","yellow");
00036   vul_arg<vcl_string> pt_colour("-pc","Point colour","none");
00037   vul_arg<vcl_string> pt_edge_colour("-pbc","Point border colour","none");
00038   vul_arg<double> pt_radius("-pr","Point radius",2.0);
00039   vul_arg<double> scale("-s","Scaling to apply",1.0);
00040 
00041   vul_arg_parse(argc,argv);
00042 
00043   if (pts_path()=="")
00044   {
00045     print_usage();
00046     return 0;
00047   }
00048 
00049   msm_curves curves;
00050   if (curves_path()!="" && !curves.read_text_file(curves_path()))
00051     vcl_cerr<<"Failed to read in curves from "<<curves_path()<<'\n';
00052 
00053   msm_points points;
00054   if (!points.read_text_file(pts_path()))
00055   {
00056     vcl_cerr<<"Failed to read points from "<<pts_path()<<'\n';
00057     return 2;
00058   }
00059   vcl_vector< vgl_point_2d<double> > pts;
00060   points.get_points(pts);
00061 
00062   //================ Attempt to load image ========
00063   vil_image_view<vxl_byte> image;
00064   if (image_path()!="")
00065   {
00066     image = vil_load(image_path().c_str());
00067     if (image.size()==0)
00068     {
00069       vcl_cout<<"Failed to load image from "<<image_path()<<vcl_endl;
00070       return 1;
00071     }
00072     vcl_cout<<"Image is "<<image<<vcl_endl;
00073   }
00074 
00075   if (scale() > 1.001 || scale() < 0.999)
00076   {
00077     // Scale image and points
00078     vil_image_view<vxl_byte> image2;
00079     image2.deep_copy(image);
00080     if (scale()<0.51)
00081       vil_gauss_filter_2d(image,image2,1.0,3);
00082     vil_resample_bilin(image2,image,
00083                        int(0.5+scale()*image.ni()),
00084                        int(0.5+scale()*image.nj()));
00085 
00086     points.scale_by(scale());
00087   }
00088 
00089 
00090   // Compute bounding box of points
00091   vgl_box_2d<double> bbox = points.bounds();
00092   bbox.scale_about_centroid(1.05);  // Add a border
00093 
00094   // If an image is supplied, use that to define bounding box
00095   if (image.size()>0)
00096   {
00097     bbox = vgl_box_2d<double>(0,image.ni(), 0,image.nj());
00098   }
00099 
00100   vgl_point_2d<double> blo=bbox.min_point();
00101 
00102   // Translate all points to allow for shift of origin
00103   points.translate_by(-blo.x(),-blo.y());
00104 
00105   mbl_eps_writer writer(out_path().c_str(),bbox.width(),bbox.height());
00106 
00107   if (image.size()>0)
00108     writer.draw_image(image,0,0, 1,1);
00109 
00110   if (pt_colour()!="none")
00111   {
00112     // Draw all the points
00113     writer.set_colour(pt_colour());
00114     msm_draw_points_to_eps(writer,points,pt_radius());
00115   }
00116 
00117   if (pt_edge_colour()!="none")
00118   {
00119     // Draw disks around all the points
00120     writer.set_colour(pt_edge_colour());
00121     msm_draw_points_to_eps(writer,points,pt_radius(),false);
00122   }
00123 
00124   if (curves.size()>0 && line_colour()!="none")
00125   {
00126     // Draw all the lines
00127     writer.set_colour(line_colour());
00128     msm_draw_shape_to_eps(writer,points,curves);
00129   }
00130   writer.close();
00131 
00132   vcl_cout<<"Graphics saved to "<<out_path()<<vcl_endl;
00133 
00134   return 0;
00135 }