libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/pascalclear_dataset.hpp
00001 /***************************************************************************
00002  *   Copyright (C) 2009 by Pierre Sermanet   *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef PASCALCLEAR_DATASET_HPP_
00034 #define PASCALCLEAR_DATASET_HPP_
00035 
00036 #include <algorithm>
00037 
00038 #include "xml_utils.h"
00039 
00040 #ifdef __BOOST__
00041 #define BOOST_FILESYSTEM_VERSION 2
00042 #include "boost/filesystem.hpp"
00043 #include "boost/regex.hpp"
00044 using namespace boost::filesystem;
00045 using namespace boost;
00046 #endif
00047 
00048 using namespace std;
00049 
00050 namespace ebl {
00051 
00053   // constructors & initializations
00054 
00055   template <class Tdata>
00056   pascalclear_dataset<Tdata>::pascalclear_dataset(const char *name_,
00057                                                   const char *inroot_,
00058                                                   const char *outdir_,
00059                                                   const char *annotations)
00060     : pascalbg_dataset<Tdata>(name_, inroot_, outdir_, 0, false, false, false,
00061                               annotations) {
00062   }
00063 
00064   template <class Tdata>
00065   pascalclear_dataset<Tdata>::~pascalclear_dataset() {
00066   }
00067 
00069   // data extraction
00070 
00071   template <class Tdata>
00072   bool pascalclear_dataset<Tdata>::extract() {
00073 #ifdef __BOOST__    
00074 #ifdef __XML__    
00075     cout << "Clearing objects from original pascal images..." << endl;
00076     mkdir_full(outdir);
00077     // adding data to dataset using all xml files in annroot
00078     regex eExt(XML_PATTERN);
00079     cmatch what;
00080     path p(annroot);
00081     if (!exists(p)) {
00082       cerr << "path " << annroot << " does not exist." << endl;
00083       return false;
00084     }    
00085     directory_iterator end_itr; // default construction yields past-the-end
00086     for (directory_iterator itr(p); itr != end_itr; ++itr) {
00087       if (!is_directory(itr->status()) &&
00088           regex_match(itr->leaf().c_str(), what, eExt)) {
00089         this->process_xml(itr->path().string());
00090         if (this->full()) //max_data_set && (data_cnt >= max_data))
00091           break ;
00092       }
00093     }
00094     cout << "Cleared and saved " << data_cnt;
00095     cout << " images." << endl;
00096 #endif /* __XML__ */
00097 #endif /* __BOOSt__ */
00098     return true;
00099   }
00100 
00101 #ifdef __BOOST__ // disable some derived methods if BOOST not available
00102 #ifdef __XML__ // disable some derived methods if XML not available
00103   
00105   // process object's image
00106 
00107   template <class Tdata>
00108   void pascalclear_dataset<Tdata>::
00109   process_image(idx<ubyte> &img, vector<rect<int> >& bboxes,
00110                 const string &image_filename) {
00111     vector<rect<int> >::iterator ibb;
00112     ostringstream fname;
00113 
00114 #ifdef __GUI__
00115     uint h = 63, w = 0;
00116     if (display_extraction) {
00117         disable_window_updates();
00118         clear_window();
00119         // draw original image
00120         draw_matrix(img, h, w, 1.0, 1.0, (ubyte) 0, (ubyte) 255);
00121         h += img.dim(0);
00122     }
00123 #endif
00124     // clear each bbox
00125     for (ibb = bboxes.begin(); ibb != bboxes.end(); ++ibb) {
00126       idx<ubyte> bb = img.narrow(0, ibb->height, ibb->h0);
00127       bb = bb.narrow(1, ibb->width, ibb->w0);
00128       idx_clear(bb);
00129     }
00130 #ifdef __GUI__
00131     if (display_extraction) {
00132       draw_matrix(img, h, w, 1.0, 1.0, (ubyte) 0, (ubyte) 255);
00133       enable_window_updates();
00134       if (sleep_display)
00135         millisleep((long) sleep_delay);
00136     }
00137 #endif
00138     fname.str("");
00139     fname << outdir << "/" << image_filename << "_" << data_cnt << ".mat";
00140     save_matrix(img, fname.str());
00141     cout << data_cnt++ << ": saved " << fname.str() << endl;
00142   }
00143   
00144 #endif /* __XML__ */
00145 #endif /* __BOOST__ */
00146 
00147 } // end namespace ebl
00148 
00149 #endif /* PASCALCLEAR_DATASET_HPP_ */