contrib/gel/gevd/gevd_region_proc.cxx
Go to the documentation of this file.
00001 // This is gel/gevd/gevd_region_proc.cxx
00002 #include "gevd_region_proc.h"
00003 //:
00004 // \file
00005 // See gevd_region_proc.h for documentation
00006 #include <gevd/gevd_float_operators.h>
00007 #include <gevd/gevd_detector.h>
00008 #include <gevd/gevd_clean_edgels.h>
00009 #include <gevd/gevd_edgel_regions.h>
00010 #include <vtol/vtol_intensity_face.h>
00011 
00012 //---------------------------------------------------------------
00013 // Constructors
00014 //
00015 //----------------------------------------------------------------
00016 
00017 //: constructor from a parameter block (the only way)
00018 //
00019 gevd_region_proc::gevd_region_proc()
00020 {
00021   debug_=false;
00022   //debug_data_ = 0;
00023   //roi_proc_ = 0;
00024   buf_ = 0;
00025 }
00026 
00027 //:Default Destructor
00028 gevd_region_proc::~gevd_region_proc()
00029 {
00030   delete buf_;
00031 }
00032 
00033 //---------------------------------------------------------
00034 //: Extract the region of interest from image_ as a BufferXY.
00035 //
00036 gevd_bufferxy* gevd_region_proc::get_image_buffer(vil1_image& image)
00037 {
00038   if (!image)
00039     return NULL;
00040   if (image.planes() != 1)
00041     return NULL;
00042 
00043   int wd = image.width();
00044   int ht = image.height();
00045   int sz = image.components() * image.bits_per_component(); // bits per pixel
00046   gevd_bufferxy* buf = new gevd_bufferxy(wd, ht, sz);
00047   image.get_section(buf->GetBuffer(), 0, 0, wd, ht);
00048   return buf;
00049 }
00050 
00051 //---------------------------------------------------------
00052 //: Convert buf to a floating point buffer
00053 //
00054 gevd_bufferxy*  gevd_region_proc::get_float_buffer(gevd_bufferxy* buf)
00055 {
00056   if (!buf)
00057     return NULL;
00058   gevd_bufferxy* fbuf =  new gevd_bufferxy(buf->GetSizeX(),
00059                                            buf->GetSizeY(),
00060                                            8*sizeof(float));
00061   gevd_float_operators::BufferToFloat(*buf, *fbuf);
00062   return fbuf;
00063 }
00064 
00065 
00066 //---------------------------------------------------------
00067 // convert a float buffer back to 16 bits/pixel
00068 //
00069 gevd_bufferxy* gevd_region_proc::put_float_buffer(gevd_bufferxy* fbuf)
00070 {
00071   if (!fbuf)
00072     return NULL;
00073 
00074   gevd_bufferxy* pbuf = new gevd_bufferxy(fbuf->GetSizeX(), fbuf->GetSizeY(), 16);
00075   gevd_float_operators::FloatToBuffer(*fbuf, *pbuf);
00076   return pbuf;
00077 }
00078 //-------------------------------------------------------------------------
00079 //: Set the image to be processed
00080 //
00081 void gevd_region_proc::set_image(vil1_image& image)
00082 {
00083   if (!image)
00084     {
00085       vcl_cout <<"In gevd_region_proc::set_image(.) - null input\n";
00086       return;
00087     }
00088   regions_valid_ = false;
00089   image_ = image;
00090   if (buf_)
00091     {
00092       delete buf_;
00093       buf_ = NULL;
00094     }
00095   //Expand by expand_scale_
00096   if (expand_scale_==1.0f)
00097     {
00098       buf_ = this->get_image_buffer(image_);
00099       return;
00100     }
00101   else if (expand_scale_==2.0f)
00102     {
00103       gevd_bufferxy* temp = this->get_image_buffer(image_);
00104       if (!temp)
00105         {
00106           vcl_cout <<"In gevd_region_proc::set_image(.) "
00107                    <<"- couldn't get gevd_bufferxy from the image\n";
00108           return;
00109         }
00110       gevd_bufferxy* fbuf = this->get_float_buffer(temp);
00111       gevd_bufferxy* expand = NULL;
00112       gevd_float_operators::ExpandBy2(*fbuf, expand, burt_adelson_factor_);
00113       gevd_float_operators::TruncateToPositive(*expand);
00114       buf_ = this->put_float_buffer(expand);
00115       delete temp;
00116       delete fbuf;
00117       delete expand;
00118       return;
00119     }
00120   else if (expand_scale_==0.5f)
00121     {
00122       gevd_bufferxy* temp = this->get_image_buffer(image_);
00123       if (!temp)
00124         {
00125           vcl_cout <<"In gevd_region_proc::set_image(.) "
00126                    <<"- couldn't get gevd_bufferxy from the image\n";
00127           return;
00128         }
00129       gevd_bufferxy* fbuf = this->get_float_buffer(temp);
00130       gevd_bufferxy* shrink = NULL;
00131       gevd_float_operators::ShrinkBy2(*fbuf, shrink, burt_adelson_factor_);
00132       gevd_float_operators::TruncateToPositive(*shrink);
00133       buf_ = this->put_float_buffer(shrink);
00134       delete temp;
00135       delete fbuf;
00136       delete shrink;
00137       return;
00138     }
00139   vcl_cout <<"In gevd_region_proc::set_image(.) - invalid expand scale factor\n";
00140 }
00141 
00142 //--------------------------------------------------------------------------
00143 //: extract a set of vdgl_poly_intensity_face(s)
00144 void gevd_region_proc::extract_regions()
00145 {
00146   if (regions_valid_)
00147     return;
00148 
00149   // Check the image
00150   if (!buf_)
00151     {
00152       vcl_cout << "In gevd_region_proc::extract_regions() - no image\n";
00153       return;
00154     }
00155 
00156   vcl_cout << "gevd_region_proc::extract_regions(): sizeX = "
00157            << buf_->GetSizeX() << " sizeY = " << buf_->GetSizeY() << vcl_endl;
00158 
00159   //Process the image to extract regions
00160   regions_.clear();
00161 
00162   // -tpk- need to pass along the scaled image rather than the orignal
00163   gevd_detector detector ( image_);
00164 
00165   //could move this all back into detector_params
00166   detector.junctionFactor = 1.0;
00167   detector.contourFactor = 2.0;
00168   detector.minLength = 4;
00169   detector.junctionp = 1;
00170   detector.smooth = 2.0;
00171   detector.borderp = 1;
00172 
00173   detector.DoContour();
00174 
00175   vcl_vector<vtol_edge_2d_sptr> * edgels = detector.GetEdges();
00176 
00177   if (!edgels->size())
00178     {
00179       vcl_cout << "In gevd_region_proc::extract_regions()- No Edgels were computed\n";
00180       return;
00181     }
00182 #if 0 // commented out
00183   vcl_vector<vtol_edge_2d_sptr>::iterator eit; = edgels.begin();
00184   for (eit = edgels->begin(); eit != edgels->end(); eit++)
00185     {
00186       vcl_cout << "Edgel output from DoContour:";
00187       (*eit)->describe(vcl_cout,2);
00188     }
00189 
00190   gevd_detector det(dp_);
00191   vcl_vector<vtol_edge_2d_sptr> broken_edgels;
00192   det.DoBreakCorners(edgels, broken_edgels);
00193 #endif
00194 
00195   gevd_clean_edgels cl;
00196   vcl_vector<vtol_edge_2d_sptr> clean_edgels;
00197   cl.DoCleanEdgelChains(*edgels, clean_edgels);
00198   if (!clean_edgels.size())
00199     {
00200       vcl_cout << "In gevd_region_proc::extract_regions()- All edges removed by clean\n";
00201       return;
00202     }
00203   gevd_edgel_regions er(debug_);
00204   er.set_magnification(expand_scale_);
00205   //if (verbose_)
00206   //  er.SetVerbose();
00207 
00208   vcl_vector<vtol_intensity_face_sptr> faces;
00209   //float xo = roi_proc_->get_xo(), yo = roi_proc_->get_yo();
00210   er.compute_edgel_regions(buf_, clean_edgels, faces);
00211 #if 0 // commented out
00212   // Transform the extracted region boundaries if necessary
00213   if (expand_scale_!=0.0f)
00214     {
00215       float si = 1.0f/expand_scale_;
00216       //We use a TwoChain to provide a superior to the set of Faces
00217       //so that the standard ::TaggedTransform can be used to
00218       //transform IntensityFace(expand_scale_) segmented at an expanded scale.
00219       TwoChain_ref tc = new TwoChain(faces.length());
00220       for (vcl_vector<IntensityFace*>::iterator fit = faces.begin();
00221           fit != faces.end(); fit++)
00222         tc->add_face((Face*)(*fit), '1');
00223       //Coordinates are defined at the center of a pixel
00224       CoolTransform minus=CoolTransform::translation(-0.5, -0.5, 0.0);
00225       CoolTransform plus=CoolTransform::translation(0.5+xo, 0.5+yo, 0.0);
00226       CoolTransform scale = CoolTransform::stretching(si, si, 1.0);
00227       CoolTransform t = CoolTransform::identity();
00228       //Concatenate the transforms
00229       t *= minus;
00230       t *= scale;
00231       t *= plus;
00232       tc->TaggedTransform(t);
00233     }
00234 #endif
00235   //Copy the faces to a vector
00236   vcl_vector<vtol_intensity_face_sptr>::iterator fit;
00237   for ( fit = faces.begin(); fit != faces.end(); fit++)
00238     {
00239       //joe mod
00240       //vtol_intensity_face_ref ifr = (*fit);
00241       //      if (!roi_proc_->inside_roi_mask(ifr))
00242       //if (!roi_proc_->inside_process_rois(ifr))
00243         continue; // hence this for loop is void! - PVr
00244       //vtol_intensity_face_sptr intf = (*fit);
00245       //vcl_vector<OneChain*>* chains = intf->OneChains();
00246       //vdgl_digital_region * dr = intf->cast_to_digital_region();
00247       //vdgl_poly_intensity_face_ref rf = new vdgl_poly_intensity_face(chains, *dr);
00248       //regions_.push_back(dr);
00249       //delete chains;
00250       //end joe mod
00251     }
00252   regions_valid_ = true;
00253 }
00254 //----------------------------------------------------------
00255 //: Clear internal storage
00256 //
00257 void gevd_region_proc::clear()
00258 {
00259   regions_.clear();
00260 }