contrib/tbl/vepl1/vepl1_median.cxx
Go to the documentation of this file.
00001 // This is tbl/vepl1/vepl1_median.cxx
00002 #include "vepl1_median.h"
00003 #include <vcl_iostream.h>
00004 #include <vipl/accessors/vipl_accessors_vil1_image.h>
00005 #include <vipl/vipl_median.h>
00006 #include <vil1/vil1_memory_image_of.h>
00007 #include <vil1/vil1_rgb.h>
00008 #include <vxl_config.h> // for vxl_byte
00009 
00010 vil1_image vepl1_median(vil1_image const& image, float radius)
00011 {
00012   // byte greyscale
00013   if (vil1_pixel_format(image) == VIL1_BYTE) {
00014     vil1_memory_image_of<vxl_byte> mem(image); // load in memory to pass to filter
00015     vil1_memory_image_of<vxl_byte> out(image);
00016     vipl_median<vil1_image,vil1_image,vxl_byte,vxl_byte> op(radius);
00017     op.put_in_data_ptr(&mem);
00018     op.put_out_data_ptr(&out);
00019     op.filter();
00020     return out;
00021   }
00022 
00023   // byte rgb: process colour bands independently as ubyte images
00024   else if (vil1_pixel_format(image) == VIL1_RGB_BYTE)
00025   {
00026 #define r_g_b vil1_rgb<vxl_byte> // cannot use typedef since that may cause ambiguous overload problems
00027     vil1_memory_image_of<r_g_b > in(image); // load in memory to pass to filter
00028     vil1_memory_image_of<r_g_b > out(image);
00029     vil1_memory_image_of<vxl_byte> mem((vxl_byte*)(in.get_buffer()),3*in.width(),in.height()); // reinterpret as vxl_byte
00030     vil1_memory_image_of<vxl_byte> mout((vxl_byte*)(out.get_buffer()),3*in.width(),in.height());
00031     vipl_median<vil1_image,vil1_image,vxl_byte,vxl_byte> op(radius);
00032     op.put_in_data_ptr(&mem);
00033     op.put_out_data_ptr(&mout);
00034     op.filter();
00035     return out;
00036   }
00037 
00038   // 16-bit greyscale
00039   else if (vil1_pixel_format(image) == VIL1_UINT16) {
00040     vil1_memory_image_of<vxl_uint_16> mem(image); // load in memory to pass to filter
00041     vil1_memory_image_of<vxl_uint_16> out(image);
00042     vipl_median<vil1_image,vil1_image,vxl_uint_16,vxl_uint_16> op(radius);
00043     op.put_in_data_ptr(&mem);
00044     op.put_out_data_ptr(&out);
00045     op.filter();
00046     return out;
00047   }
00048 
00049   // float
00050   else if (vil1_pixel_format(image) == VIL1_FLOAT) {
00051     vil1_memory_image_of<float> mem(image); // load in memory to pass to filter
00052     vil1_memory_image_of<float> out(image);
00053     vipl_median<vil1_image,vil1_image,float,float> op(radius);
00054     op.put_in_data_ptr(&mem);
00055     op.put_out_data_ptr(&out);
00056     op.filter();
00057     return out;
00058   }
00059 
00060   // double
00061   else if (vil1_pixel_format(image) == VIL1_DOUBLE) {
00062     vil1_memory_image_of<double> mem(image); // load in memory to pass to filter
00063     vil1_memory_image_of<double> out(image);
00064     vipl_median<vil1_image,vil1_image,double,double> op(radius);
00065     op.put_in_data_ptr(&mem);
00066     op.put_out_data_ptr(&out);
00067     op.filter();
00068     return out;
00069   }
00070 
00071   //
00072   else {
00073     vcl_cerr << __FILE__ ": vepl1_median() not implemented for " << image << vcl_endl;
00074     return 0;
00075   }
00076 }
00077