PLearn 0.1
RGBImage.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037  
00038 
00039 /* *******************************************************      
00040    * $Id: RGBImage.h 8230 2007-11-07 20:48:13Z nouiz $
00041    * AUTHORS: Pascal Vincent & Yoshua Bengio
00042    * This file is part of the PLearn library.
00043    ******************************************************* */
00044 
00045 
00048 #ifndef RGBIMAGE_INC
00049 #define RGBIMAGE_INC
00050 
00051 #include <plearn/base/Array.h>
00052 //#include "general.h"
00053 #include <plearn/base/Object.h>
00054 #include <plearn/base/Storage.h>
00055 #include <plearn/math/TMat.h>
00056 //#include "VMat.h"
00057 
00058 namespace PLearn {
00059 using namespace std;
00060 
00061 
00062 class HSV;
00063 
00064 class RGB
00065 {
00066 public:
00067   unsigned char r;
00068   unsigned char g;
00069   unsigned char b;
00070   
00071   RGB() {}
00072   RGB(unsigned char the_r, unsigned char the_g, unsigned char the_b)
00073     : r(the_r), g(the_g), b(the_b) {}
00074 
00076 
00077 public:
00078   static const RGB BLACK;
00079   static const RGB WHITE;
00080   static const RGB RED;
00081   static const RGB GREEN;
00082   static const RGB BLUE;
00083   static const RGB DISCOCOLORS[];
00084 };
00085 
00086 
00087 class HSV
00088 {
00089 public:
00090   unsigned char h;
00091   unsigned char s;
00092   unsigned char v;
00093 
00094   HSV() {}
00095   HSV(unsigned char the_h, unsigned char the_s, unsigned char the_v)
00096     : h(the_h), s(the_s), v(the_v) {}
00097 
00099 };
00100 
00101 
00103 class RGBImage
00104 {
00105 protected:
00106   int height_;
00107   int width_;
00108 
00109   PP< Storage<RGB> > storage;
00110   char tmpfilename[100]; 
00111 
00112 public:
00113   RGBImage(int the_height=0, int the_width=0);
00114   RGBImage(Mat r, Mat g, Mat b); 
00115   ~RGBImage();
00116 
00117   int width() const { return width_; }
00118   int height() const { return height_; }
00119   void resize(int newheight, int newwidth);
00120 
00121   inline RGB getPixel(int i, int j) const; 
00122   inline void setPixel(int i, int j, RGB value); 
00123   void fill(RGB color);
00124   void clear() { fill(RGB(255,255,255)); }
00125 
00126   void loadPPM(const char* filename);
00127   void savePPM(const char* filename) const;
00128   void loadJPEG(const char* filename, int scale=1);
00129   void display() const;
00130   void displayAndWait() const;
00131 
00133   void removeBorders(int top_border,int bottom_border,
00134                      int left_border,int right_border);
00135   
00137   void removeBorders(int border) { removeBorders(border,border,border,border); }
00138 
00141   void shrinkToIntersection(int& i, int& j, int& h, int& w) const;
00142 
00144   void blit(int desti, int destj, RGBImage srcim);
00145   RGB computeAverage() const;
00146 
00149   Vec computeHistogram(int r_bins=16, int g_bins=16, int b_bins=16, bool do_normalize=false);
00150 };
00151 
00152 inline RGB
00153 RGBImage::getPixel(int i, int j) const
00154 {
00155 #ifdef BOUNDCHECK
00156   if(!storage)
00157     PLERROR("In RGBImage::getPixeel(int i, int j) EMPTY IMAGE!!!");
00158   if(i<0 || j<0 || i>=height_ || j>=width_)
00159     PLERROR("In RGBImage::getPixel(int i, int j) OUT OF IMAGE BOUNDS");
00160 #endif
00161   return storage->data[i*width_+j];
00162 }
00163 
00164 inline void
00165 RGBImage::setPixel(int i, int j, RGB value)
00166 {
00167 #ifdef BOUNDCHECK
00168   if(!storage)
00169     PLERROR("In RGBImage::getPixeel(int i, int j) EMPTY IMAGE!!!");
00170   if(i<0 || j<0 || i>=height_ || j>=width_)
00171     PLERROR("In RGBImage::getPixel(int i, int j) OUT OF IMAGE BOUNDS");
00172 #endif
00173   storage->data[i*width_+j] = value;
00174 }
00175 
00176 class RGBImageDB
00177 {
00178 protected:
00179   Array<char*> filenames;
00180   Array<RGBImage*> images;
00181   int subsample_factor;
00182   int remove_border;
00183   int max_n_images_in_memory;
00184   int n_images_in_memory;
00185   
00186   int append(char* filename);
00187   void load(char* dbfile);
00188 
00189   RGBImageDB(int the_subsample_factor=1,int remove_border=0, int the_max_n_images_in_memory=10); 
00190 
00191 public:
00194   Mat imageIdAndClass;
00195 
00196   RGBImageDB(char* dbfilename, int the_subsample_factor=1,int remove_border=0, int the_max_n_images_in_memory=10);
00197 
00198   RGBImage getImage(int imageid);
00199 
00202   Mat computeHistogramRepresentation(int r_bins=16, int g_bins=16, int b_bins=16, bool do_normalize=false);
00203 
00204   ~RGBImageDB();
00205 };
00206 
00207 
00208 class RGBImagesVMatrix;
00209 
00222 class RGBImageVMatrix: public Object
00223 {
00224   friend class RGBImagesVMatrix;
00225 
00226 protected:
00227   RGBImage image;
00228   Vec delta_row; 
00229   Vec delta_column; 
00230   real scale; 
00231   real offset_; 
00232   int width_; 
00233   int max_delta_row, max_delta_column;
00234   int first_row; 
00235   int first_column; 
00236   int bottom_border_row; 
00237   int right_border_col; 
00238   int n_cols; 
00239   int current_i, current_j; 
00240 
00241 public:
00249   RGBImageVMatrix(RGBImage image, 
00250                        const Vec& delta_row, const Vec& delta_col,
00251                        real scale=1.0, 
00252                        real offset_=0.0);
00253   virtual int width();
00254   virtual int length();
00255   virtual void reset();
00256   virtual void sample(Vec& samplevec);
00257   virtual void seek(int position);
00258   virtual int position();
00259 
00261   virtual bool first() 
00262     { return (current_i==first_row) && (current_j==first_column); }
00263 
00265   void setImage(RGBImage new_image);
00266 };
00267 
00268 
00274 class RGBImagesVMatrix: public Object
00275 {
00276 protected:
00277   RGBImageVMatrix image_distr;
00278   RGBImageDB& images_db;
00279   int current_image;
00280   bool append_class;
00281   int length_;
00282   Vec pixelsAndClass;
00283   Vec pixels;
00284   Vec image_start; 
00285   int width_;
00286 
00287 public:
00288   RGBImagesVMatrix(RGBImageDB& images_db,
00289                         const Vec& delta_row,
00290                         const Vec& delta_col, 
00291                         bool append_class,
00292                         real scale=1.0,
00293                         real offset_=0.0);
00294 
00295   virtual int width();
00296   virtual int length();
00297   virtual void reset();
00298   virtual void sample(Vec& samplevec);
00299   virtual void seek(int position);
00300   virtual int position();
00301   virtual bool firstSampleOfObject();
00302   virtual int nSamplesOfObject();
00303   virtual int nObjects();
00304 };
00305 
00338 } // end of namespace PLearn
00339 
00340 #endif
00341 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines