contrib/mul/vil3d/tools/vil3d_slice_image.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Tool to load in a 3D image and produce 2D slices
00004 // \author Tim Cootes
00005 // Slices may be cropped, and range is stretched linearly
00006 
00007 #include <vul/vul_arg.h>
00008 #include <vcl_iostream.h>
00009 #include <vil3d/vil3d_property.h>
00010 #include <vil3d/vil3d_load.h>
00011 #include <vil3d/vil3d_convert.h>
00012 #include <vil3d/vil3d_slice.h>
00013 #include <vil/vil_convert.h>
00014 #include <vil/vil_save.h>
00015 #include <vil/vil_crop.h>
00016 #include <vil/vil_resample_bilin.h>
00017 #include <vnl/vnl_math.h>
00018 #include <vxl_config.h> // For vxl_byte
00019 
00020 void print_usage()
00021 {
00022   vcl_cout<<"Tool to load in a 3D image and produce 2D slices."<<vcl_endl;
00023   vul_arg_display_usage_and_exit();
00024 }
00025 
00026 void save_slice(const vil_image_view<float>& image,
00027                 float wi, float wj, double border,
00028                 vcl_string path)
00029 {
00030   // Resize so that image has square pixels
00031   float w=vcl_min(wi,wj);
00032   unsigned ni=vnl_math_rnd(image.ni()*wi/w);
00033   unsigned nj=vnl_math_rnd(image.nj()*wj/w);
00034 
00035   vil_image_view<float> image2;
00036   if (ni==nj)
00037     image2=image;
00038   else
00039     vil_resample_bilin(image,image2,ni,nj);
00040 
00041   unsigned ilo=vnl_math_rnd(ni*border);
00042   unsigned ihi=ni-1-ilo;
00043   unsigned jlo=vnl_math_rnd(nj*border);
00044   unsigned jhi=nj-1-jlo;
00045 
00046   vil_image_view<float> cropped_image
00047      = vil_crop(image2,ilo,1+ihi-ilo, jlo,1+jhi-jlo);
00048   // Ignore image size in first test
00049   vil_image_view<vxl_byte> byte_im;
00050   vil_convert_stretch_range(cropped_image,byte_im);
00051 
00052   if (vil_save(byte_im,path.c_str()))
00053     vcl_cerr<<"Saved image to "<<path<<'\n';
00054   else
00055     vcl_cerr<<"Failed to save image to "<<path<<'\n';
00056 }
00057 
00058 int main(int argc, char** argv)
00059 {
00060   vul_arg<vcl_string> image_path("-i","3D image filename");
00061   vul_arg<vcl_string> output_path("-o","Base path for output","slice");
00062   vul_arg<double> pi("-pi","Slice position as %age along i",50);
00063   vul_arg<double> pj("-pj","Slice position as %age along i",50);
00064   vul_arg<double> pk("-pk","Slice position as %age along i",50);
00065   vul_arg<double> b("-b","Border as %age",0);
00066 
00067   vul_arg_parse(argc,argv);
00068 
00069   if (image_path()=="")
00070   {
00071     print_usage();
00072     return 0;
00073   }
00074 
00075   // Attempt to load in the 3D image
00076   vil3d_image_resource_sptr im_res = vil3d_load_image_resource(image_path().c_str());
00077   if (im_res==0)
00078   {
00079     vcl_cerr<<"Failed to load in image from "<<image_path()<<'\n';
00080     return 1;
00081   }
00082 
00083   // Read in voxel size if available
00084   float width[3] = { 1.0f, 1.0f, 1.0f };
00085   im_res->get_property(vil3d_property_voxel_size, width);
00086   vcl_cout<<"Voxel sizes: "
00087           <<width[0]<<" x "<<width[1]<<" x "<<width[2]<<vcl_endl;
00088 
00089 //  vil3d_image_view_base_sptr im_ptr = im_res->get_view();
00090 
00091   // Load the image data and cast to float
00092   vil3d_image_view<float> image3d
00093     = vil3d_convert_cast(float(),im_res->get_view());
00094 
00095   if (image3d.size()==0)
00096 
00097   vcl_cout<<"Image3D: "<<image3d<<vcl_endl;
00098   {
00099     vcl_cerr<<"Failed to load in image from "<<image_path()<<'\n';
00100     return 1;
00101   }
00102 
00103   // Select i,j,k values
00104   unsigned i=vnl_math_rnd(0.01*pi()*(image3d.ni()-1));
00105   i=vcl_min(i,image3d.ni()-1);
00106   unsigned j=vnl_math_rnd(0.01*pj()*(image3d.nj()-1));
00107   j=vcl_min(j,image3d.nj()-1);
00108   unsigned k=vnl_math_rnd(0.01*pk()*(image3d.nk()-1));
00109   k=vcl_min(k,image3d.nk()-1);
00110 
00111   vil_image_view<float> slice_jk = vil3d_slice_jk(image3d,i);
00112   vil_image_view<float> slice_ik = vil3d_slice_ik(image3d,j);
00113   vil_image_view<float> slice_ij = vil3d_slice_ij(image3d,k);
00114 
00115   save_slice(slice_jk,width[1],width[2],0.01*b(),output_path()+"_jk.jpg");
00116   save_slice(slice_ik,width[0],width[2],0.01*b(),output_path()+"_ik.jpg");
00117   save_slice(slice_ij,width[0],width[1],0.01*b(),output_path()+"_ij.jpg");
00118 
00119   return 0;
00120 }