contrib/brl/bseg/sdet/sdet_region_proc.cxx
Go to the documentation of this file.
00001 // This is brl/bseg/sdet/sdet_region_proc.cxx
00002 #include "sdet_region_proc.h"
00003 //:
00004 // \file
00005 #include <vnl/vnl_numeric_traits.h>
00006 #include <vil1/vil1_memory_image_of.h>
00007 #include <vil1/vil1_vil.h>
00008 #include <brip/brip_roi.h>
00009 #include <brip/brip_vil1_float_ops.h>
00010 #include <sdet/sdet_edgel_regions.h>
00011 #include <vsol/vsol_box_2d.h>
00012 #include <vtol/vtol_list_functions.h>
00013 #include <vtol/vtol_intensity_face.h>
00014 #include <sdet/sdet_detector.h>
00015 
00016 //---------------------------------------------------------------
00017 // Constructors
00018 //
00019 //----------------------------------------------------------------
00020 
00021 //: constructor from a parameter block (the only way)
00022 //
00023 sdet_region_proc::sdet_region_proc(sdet_region_proc_params& rpp)
00024   : sdet_region_proc_params(rpp)
00025 {
00026   image_ = 0;
00027   vimage_ = 0;
00028   clip_ = 0;
00029   vclip_ = 0;
00030   use_vil_image_ = true; //who knows, but need to chose one
00031 }
00032 
00033 //:Default Destructor
00034 sdet_region_proc::~sdet_region_proc()
00035 {
00036 }
00037 
00038 //-------------------------------------------------------------------------
00039 //: Set the image to be processed (vil1 type image)
00040 //
00041 void sdet_region_proc::set_image(vil1_image const& image,
00042                                  vsol_box_2d_sptr const& box)
00043 {
00044   if (!image)
00045   {
00046     vcl_cout <<"In sdet_region_proc::set_image(.) - null input\n";
00047     return;
00048   }
00049   
00050   if (box)
00051   {
00052     clip_ = vil1_image();
00053     roi_ = new brip_roi(image.width(), image.height());
00054     roi_->add_region(box);
00055     if (!brip_vil1_float_ops::chip(image, roi_, clip_))
00056       vcl_cout << "In sdet_region_proc::set_image() - chip failed\n";
00057   }
00058   regions_valid_ = false;
00059   image_ = image;
00060   use_vil_image_ = false;
00061 }
00062 
00063 //-------------------------------------------------------------------------
00064 //: Set the image to be processed (vil type image)
00065 //
00066 void sdet_region_proc::set_image_resource(vil_image_resource_sptr const& image,
00067                                           vsol_box_2d_sptr const& box)
00068 {
00069   if (!image)
00070   {
00071     vcl_cout <<"In sdet_region_proc::set_image(.) - null input\n";
00072     return;
00073   }
00074 vil_image_resource_sptr vil_crop(const vil_image_resource_sptr &src, unsigned i0,
00075                                  unsigned n_i, unsigned j0, unsigned n_j);
00076   if (box)
00077   {
00078     vclip_ = 0;
00079     
00080     unsigned i0 = (unsigned)box->get_min_x(), j0 = (unsigned)box->get_min_y();
00081     unsigned n_i = ((unsigned)box->get_max_x())-i0;
00082     unsigned n_j = ((unsigned)box->get_max_y())-j0;
00083     vclip_ = vil_crop(image, i0, n_i, j0, n_j);
00084     if (!vclip_)
00085       vcl_cout << "In sdet_region_proc::set_image_resource() - chip failed\n";
00086   }
00087   regions_valid_ = false;
00088   vimage_ = image;
00089   use_vil_image_ = true;
00090 }
00091 bool sdet_region_proc::
00092 set_detector_image_vil1(sdet_detector& d)
00093 {
00094   // Check the image 
00095   if (!image_ && !clip_)
00096     {
00097       vcl_cout << "In sdet_region_proc::extract_regions() -"
00098                << " no vil1_image nor clip\n";
00099       return false;
00100     }
00101   vil1_image temp = image_;
00102   if (clip_)
00103     temp = clip_;
00104   vcl_cout << "sdet_region_proc::extract_regions(): width = "
00105            << temp.width() << " height = " << temp.height() << vcl_endl;
00106 
00107   d.SetImage(temp);
00108   return true;
00109 }
00110 
00111 bool sdet_region_proc::
00112 set_detector_image_vil(sdet_detector& d)
00113 {
00114   // Check the image resources 
00115   if (!vimage_ && !vclip_)
00116     {
00117       vcl_cout << "In sdet_region_proc::extract_regions() -"
00118                << " no vil_image nor clip\n";
00119       return false;
00120     }
00121   vil_image_resource_sptr temp = vimage_;
00122   if (clip_)
00123     temp = vclip_;
00124   vcl_cout << "sdet_region_proc::extract_regions(): width = "
00125            << temp->ni() << " height = " << temp->nj() << vcl_endl;
00126 
00127   d.SetImage(temp);
00128   return true;
00129 }
00130 
00131 //--------------------------------------------------------------------------
00132 //: extract a set of vtol_intensity_face(s)
00133 void sdet_region_proc::extract_regions()
00134 {
00135   if (regions_valid_)
00136     return;
00137   vcl_vector<vtol_edge_2d_sptr> * edges;
00138   sdet_detector detector(dp_);
00139   if(!use_vil_image_&&!this->set_detector_image_vil1(detector))
00140     {
00141       vcl_cout << "In sdet_region_proc::extract_regions() [vil1] -"
00142                << " image set error\n";
00143       return;
00144     }
00145   else if(use_vil_image_&&!this->set_detector_image_vil(detector))
00146     {
00147       vcl_cout << "In sdet_region_proc::extract_regions() [vil]-"
00148                << " image set error\n";
00149       return;
00150     }
00151 
00152   detector.DoContour();
00153   edges = detector.GetEdges();
00154   if (!edges||!edges->size())
00155   {
00156     vcl_cout << "In sdet_region_proc::extract_regions() -"
00157              << " No Edgels were computed\n";
00158     return;
00159   }
00160   //Process the image to extract regions
00161   regions_.clear();
00162   sdet_edgel_regions er(array_scale_, verbose_, debug_);
00163   if(use_vil_image_)
00164     if(clip_)
00165       er.compute_edgel_regions(vclip_, *edges, regions_);
00166     else
00167       er.compute_edgel_regions(vimage_, *edges, regions_);
00168   else
00169     if(clip_)
00170       er.compute_edgel_regions(clip_, *edges, regions_);
00171     else 
00172       er.compute_edgel_regions(image_, *edges, regions_);
00173   if (debug_)
00174     edge_image_ = er.GetEdgeImage(*edges);
00175   regions_valid_ = true;
00176 }
00177 
00178 //----------------------------------------------------------
00179 //: Clear internal storage
00180 //
00181 void sdet_region_proc::clear()
00182 {
00183   regions_.clear();
00184 }
00185 
00186 //--------------------------------------------------------------------------
00187 //: Use a linear approximation to intensity to predict region data.
00188 //  Return the residual error
00189 vil1_image sdet_region_proc::get_residual_image()
00190 {
00191   if (!image_||!regions_valid_)
00192   {
00193     vcl_cout << "In sdet_region_proc::get_residual_image() - no regions\n";
00194     return 0;
00195   }
00196   int xsize = image_.width(), ysize = image_.height();
00197   vil1_memory_image_of<unsigned char> res_image(xsize, ysize);
00198   res_image.fill(0);
00199   float min_res = (float)vnl_numeric_traits<unsigned short>::maxval;
00200   for (vcl_vector<vtol_intensity_face_sptr>::iterator fit = regions_.begin();
00201        fit != regions_.end(); fit++)
00202     for ((*fit)->reset(); (*fit)->next();)
00203     {
00204       float res = (*fit)->Ir();
00205       if (res<min_res)
00206         min_res = res;
00207     }
00208 
00209   for (vcl_vector<vtol_intensity_face_sptr>::iterator fit = regions_.begin();
00210        fit != regions_.end(); fit++)
00211     for ((*fit)->reset(); (*fit)->next();)
00212     {
00213       int x = int((*fit)->X()), y = int((*fit)->Y());
00214       float res = (*fit)->Ir();
00215       float is = res-min_res;//to ensure non-negative
00216       if (is>255)
00217         is = 255;//to ensure within char
00218       unsigned char pix = (unsigned char)is;
00219       res_image(x, y)=pix;
00220     }
00221   return res_image;
00222 }
00223 //--------------------------------------------------------------------------
00224 //: Use a linear approximation to intensity to predict region data.
00225 //  Return the residual error
00226 vil_image_view<float> sdet_region_proc::get_residual_image_view()
00227 {
00228   vil_image_view<float> null;
00229   if (!vimage_||!regions_valid_)
00230   {
00231     vcl_cout << "In sdet_region_proc::get_residual_image() - no regions\n";
00232     return null;
00233   }
00234 
00235   int xsize = vimage_->ni(), ysize = vimage_->nj();
00236   null.set_size(xsize, ysize);
00237   null.fill(0.0);
00238   for (vcl_vector<vtol_intensity_face_sptr>::iterator fit = regions_.begin();
00239        fit != regions_.end(); fit++)
00240     for ((*fit)->reset(); (*fit)->next();)
00241     {
00242       int x = int((*fit)->X()), y = int((*fit)->Y());
00243       null(x, y)= (*fit)->Ir();
00244     }
00245   return null;
00246 }
00247 
00248 //: Returns a vil_image_resource_sptr of the edge image
00249 vil_image_resource_sptr
00250 sdet_region_proc::get_edge_image_resource()
00251 {
00252   vil_image_resource_sptr virs = vil1_to_vil_image_resource(edge_image_);
00253   return virs;
00254 }
00255 
00256 #if 0
00257 //: If a clip has been used we have to transform the regions back into the image coordinates.
00258 bool sdet_region_proc::transform_regions()
00259 {
00260   if (!roi_)
00261     return false;
00262   //the origin of the roi
00263   int cmin = roi->cmin(0), rmin = roi->rmin(0);
00264   //we need to get all the edges and remove duplicates
00265   //then get the vertices and remove duplicates
00266   //finally transform the digital regions
00267   vcl_vector<vtol_edge_sptr> edges;
00268   for (vcl_vector<vtol_intensity_face_sptr>::iterator fit = regions_.begin();
00269        fit != regions_.end(); fit++)
00270   {
00271     vcl_vector<vtol_edge_sptr> face_edges;
00272     (*fit)->edges(face_edges);
00273     for (vcl_vector<vtol_edge_sptr>::iterator eit = face_edges.begin();
00274          eit != face_edges.end(); eit++)
00275       edges.push_back(*eit);
00276   }
00277   //remove duplicates
00278   vcl_vector<vtol_edge_sptr>* edg_adr = &edges;
00279   edg_adr = tagged_union(edg_adr);
00280 
00281   for (vcl_vector<vtol_edge_sptr>::iterator eit = edges.begin();
00282        eit != edges.end(); eit++)
00283     ...
00284 }
00285 #endif