contrib/tbl/vepl/vepl_threshold.cxx
Go to the documentation of this file.
00001 // This is tbl/vepl/vepl_threshold.cxx
00002 #include "vepl_threshold.h"
00003 #include <vcl_iostream.h>
00004 #include <vepl/accessors/vipl_accessors_vil_image_view_base.h>
00005 #include <vipl/vipl_threshold.h>
00006 #include <vil/vil_image_view.h>
00007 #include <vil/vil_pixel_format.h>
00008 #include <vil/vil_plane.h>
00009 #include <vil/vil_new.h>
00010 #include <vxl_config.h> // for vxl_byte
00011 
00012 vil_image_resource_sptr vepl_threshold(vil_image_resource_sptr image, double threshold, double below, double above)
00013 {
00014   vil_image_resource_sptr img_out = vil_new_image_resource(image->ni(), image->nj(), image->nplanes(), image->pixel_format());
00015 
00016   // multi-planar image
00017   // since vipl does not know the concept of planes, run filter on each plane
00018   if (image->nplanes() > 1) {
00019     if (image->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
00020       vil_image_view<vxl_byte> out = image->get_copy_view();
00021       vil_image_view<vxl_byte> in = image->get_view();
00022       vipl_threshold<vil_image_view_base,vil_image_view_base,vxl_byte,vxl_byte>
00023         op((vxl_byte)threshold, (vxl_byte)below, (vxl_byte)above);
00024       for (unsigned int p=0; p<image->nplanes(); ++p) {
00025         vil_image_view<vxl_byte> i = vil_plane(in,p), o = vil_plane(out,p);
00026         op.put_in_data_ptr(&i); op.put_out_data_ptr(&o); op.filter();
00027       }
00028       img_out->put_view(out);
00029     }
00030     else
00031       vcl_cerr << __FILE__ ": vepl_dilate_disk() not implemented for multi-planar " << image << '\n';
00032   }
00033 
00034   // byte greyscale
00035   else if (image->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
00036     vil_image_view<vxl_byte> in = image->get_view();
00037     vil_image_view<vxl_byte> out = image->get_copy_view();
00038     vipl_threshold<vil_image_view_base,vil_image_view_base,vxl_byte,vxl_byte>
00039       op((vxl_byte)threshold, (vxl_byte)below, (vxl_byte)above);
00040     op.put_in_data_ptr(&in);
00041     op.put_out_data_ptr(&out);
00042     op.filter();
00043     img_out->put_view(out);
00044   }
00045 
00046   // byte rgb
00047   else if (image->pixel_format() == VIL_PIXEL_FORMAT_RGB_BYTE) {
00048     vil_image_view<vxl_byte> in = image->get_view(); // in will have 3 planes but 1 component
00049     vil_image_view<vxl_byte> out = image->get_copy_view();
00050     vipl_threshold<vil_image_view_base,vil_image_view_base,vxl_byte,vxl_byte>
00051       op((vxl_byte)threshold, (vxl_byte)below, (vxl_byte)above);
00052     op.put_in_data_ptr(&in);
00053     op.put_out_data_ptr(&out);
00054     op.filter();
00055     img_out->put_view(out);
00056   }
00057 
00058   // 16-bit greyscale
00059   else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_16) {
00060     vil_image_view<vxl_uint_16> in = image->get_view();
00061     vil_image_view<vxl_uint_16> out = image->get_copy_view();
00062     vipl_threshold<vil_image_view_base,vil_image_view_base,vxl_uint_16,vxl_uint_16>
00063       op((vxl_uint_16)threshold, (vxl_uint_16)below, (vxl_uint_16)above);
00064     op.put_in_data_ptr(&in);
00065     op.put_out_data_ptr(&out);
00066     op.filter();
00067     img_out->put_view(out);
00068   }
00069 
00070   // float
00071   else if (image->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
00072     vil_image_view<float> in = image->get_view();
00073     vil_image_view<float> out = image->get_copy_view();
00074     vipl_threshold<vil_image_view_base,vil_image_view_base,float,float>
00075       op((float)threshold, (float)below, (float)above);
00076     op.put_in_data_ptr(&in);
00077     op.put_out_data_ptr(&out);
00078     op.filter();
00079     img_out->put_view(out);
00080   }
00081 
00082   // double
00083   else if (image->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
00084     vil_image_view<double> in = image->get_view();
00085     vil_image_view<double> out = image->get_copy_view();
00086     vipl_threshold<vil_image_view_base,vil_image_view_base,double,double>
00087       op(threshold, below, above);
00088     op.put_in_data_ptr(&in);
00089     op.put_out_data_ptr(&out);
00090     op.filter();
00091     img_out->put_view(out);
00092   }
00093 
00094   //
00095   else
00096     vcl_cerr << __FILE__ ": vepl_threshold() not implemented for " << image << '\n';
00097 
00098   return img_out;
00099 }
00100