The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

src/rimage.h

Go to the documentation of this file.
00001 /*
00002  *  rimage.h
00003  *
00004  *  Created by Rhan Dahl on Thu Apr 18 2002.
00005  *  Fixes: removed bounds checking,
00006  *         improved corner caching - Ian Fasel, July 24, 2002
00007  *         fixed bounds checking and properly templatized, 
00008  *         added RIntegral class   - John Hershey, Sept 5, 2002
00009  * 
00010  *  Copyright (c) 2002 Machine Perception Laboratory 
00011  *  University of California San Diego.
00012  * 
00013  * Please read the disclaimer and notes about redistribution 
00014  * at the end of this file.
00015  *
00016  * Benchmark on June 2002 on my 700 MHz iBook:
00017  *    Jan 10 2003 -- spock.gif = 1.25, dude = 0.3, people = 7.86
00018  *    Apr 19 2003 -- spock.gif = 0.82, dude = 0.3, people = 5.24 -- about 33% improvement!
00019  *    Jul 29 2003 -- spock.gif = 0.35, dude = 0.055, people = 3.1 -- 40-60% or more improvement!
00020  *
00021  */
00022 #ifndef _RIMAGE_H_
00023 #define _RIMAGE_H_
00024 
00025 #include "roi.h"
00026 //#include <iostream>
00027 #ifndef ASSERT
00028 #ifdef _DEBUG
00029 #include <assert.h>
00030 #define ASSERT(x) assert(x)
00031 #else
00032 #define ASSERT(x) 
00033 #endif
00034 #endif
00035 
00036 template <class T>
00037 class RImage 
00038 {
00039   public:
00040     RImage (const T *array, const int, const int);
00041     RImage (const int, const int);
00042     RImage (const int, const int, T);
00043     RImage (const RImage &other){
00044       width = other.width;
00045       height = other.height;
00046       numpixels = other.numpixels;
00047       array = other.array;
00048     }
00049     RImage (){};
00050     inline void setSize(const int width_, const int height_){
00051       width = width_;
00052       height = height_;
00053       numpixels = width * height;
00054       array = new T[numpixels];
00055       my_memory = 1;
00056       // keeps track of use of the image. The destructor will only
00057       //erase the image if my_memory == 0
00058       
00059       //  cout << "RImage<T>:
00060       //allocated RImage with width = " << width << " height = " <<
00061       //height << endl;
00062     }
00063     inline void deleteArray(){
00064       delete [] array;
00065     }
00066     virtual ~RImage ( );
00067     virtual T getPixel (const int x, const int y) const;
00068     virtual T getPixel (const int index) const;
00069     void setPixel (const int x, const int y, const T &value);
00070     void setPixel (const int index, const T &value);
00071     
00072     void print ( ) const;
00073     void print ( const int m ) const;
00074     T *array;
00075     
00076     int width;
00077     int height;
00078     int numpixels;
00079 
00082     int my_memory;
00083       
00084 };
00085 
00086 template <class T>
00087 inline RImage<T>::RImage (const T *array_, const int width_, const int height_ )
00088 {
00089   width = width_;
00090   height = height_;
00091   numpixels = width * height;
00092   array = const_cast<T *>(array_);
00093   my_memory = 0;
00094 }
00095   
00096 template <class T>
00097 inline RImage<T>::RImage ( const int width_, const int height_ )
00098 {
00099         setSize(width_,height_);
00100 }
00101 
00102 template <class T>
00103 inline RImage<T>::RImage ( const int width_, const int height_ , const T val)
00104 {
00105     setSize(width_,height_);
00106     T *ptr = array;
00107     for(int y=0; y <height_; y++)
00108       for(int x=0; x <width_; x++)      
00109            *(ptr++) = val; 
00110 }
00111 
00112 template <class T>
00113 inline RImage<T>::~RImage ()
00114 {
00115   if(my_memory){
00116     //cout << "RImage::~RImage(): deleting" << endl;
00117     delete [] array;
00118   }
00119 }
00120 
00121 template <class T>
00122 inline T RImage<T>::getPixel ( const int x, const int y ) const
00123 {
00124   if(x >= 0 && y >= 0 && x < width && y < height)
00125     return array[width * y + x]; 
00126   else 
00127 #ifdef WIN32
00128         return array[0];
00129 #else
00130     return 0;
00131 #endif
00132 }
00133 
00134 template <class T>
00135 inline T RImage<T>::getPixel ( const int index ) const
00136 {
00137   if((index >= 0) && (index < numpixels ))
00138     return array[index];
00139   else
00140 #ifdef WIN32
00141         return array[0];
00142 #else
00143     return 0;
00144 #endif
00145 }
00146 
00147 template <class T>
00148 inline void RImage<T>::setPixel ( const int x, const int y, const T &value )
00149 {
00150   if(x >= 0 && y >= 0 && x < width && y < height)
00151     array[width * y + x] = value;
00152 }
00153 
00154 template <class T>
00155 inline void RImage<T>::setPixel ( const int index, const T &value )
00156 {
00157   if((index >= 0) && (index < numpixels ))
00158     array[index] = value;
00159 }
00160 
00161 template <class T>
00162 void RImage<T>::print ( ) const 
00163 {
00164   for(int y = 0; y < height; y++) {
00165     for(int x = 0; x < width; x++) {
00166       cout << getPixel(x,y) << " ";
00167     }
00168     cout << endl;
00169   }
00170 }
00171 
00172 template <class T>
00173 void RImage<T>::print ( const int m ) const 
00174 {
00175   for(int y = 0; y < min(m,height); y++) {
00176     for(int x = 0; x < min(m,width); x++) {
00177       cout << getPixel(x,y) << " ";
00178     }
00179     cout << endl;
00180   }
00181 }
00182 
00183 
00184 //  RImage<T> returns the wrong values if we scan too far
00185 //  so we override getPixel in derived class and provide constructor.
00186 
00187 
00188 template <class T>
00189 class RIntegral : public RImage<T>
00190 {
00191 public:
00192   RIntegral(const int width, const int height){
00193       setSize(width+1,height+1);
00194   }
00195 
00196   RIntegral(const RImage<T> &image) {
00197       setSize(image.width+1,image.height+1);
00198     integrate(image);
00199   }
00200 
00201   RIntegral(const RImage<T> &image, ROI &roi){
00202       setSize(image.width+1,image.height+1);
00203     integrate(image, roi);
00204   }
00205   
00206   void integrate(const RImage<T> &image){
00207       T * p = array;
00208       for(int x = 0; x < width; ++x) // create top row of zeros
00209         *p++ = 0;
00210       for(int y = 0; y < height-1; ++y){
00211         *p++ = 0;                    // create left column of zeros
00212         for(int x = 0; x < width-1; ++x)
00213           *p++ =
00214             image.getPixel(x,y) + getPixel(x, y+1) +
00215             getPixel(x+1, y) - getPixel(x, y);
00216       }
00217   }
00218   void integrate(const RImage<T> &image, ROI roi){
00219                 //cout << "RImage<T>::integrate: " << roi << endl;
00220     //roi.fitImage(*this);
00221     //cout << "RImage<T>::integrate: " << roi << endl;
00222 
00223     //cout << "roi: (" << roi.m_min_x << ", " << roi.m_min_y << ", "<< roi.m_max_x << ", "<< roi.m_max_y << ")" << endl;
00224       T *p = array + (width*roi.m_min_y + roi.m_min_x);
00225       T *pxy, *px1y, *pxy1, *q;
00226       for(int x = roi.m_min_x; x < roi.m_max_x+1; ++x) // create top row of zeros
00227         *p++ = 0;
00228       for(int y = roi.m_min_y; y < roi.m_max_y; ++y){
00229         //cout << "integrating line " << y << " of " << image.height << endl;
00230         p = array + (width*(y+1) + roi.m_min_x);
00231         pxy = array + (width * y +  roi.m_min_x);
00232         px1y = array + (width * y +  roi.m_min_x + 1);
00233         pxy1 = array + (width * (y+1) +  roi.m_min_x);
00234         q = image.array + (image.width * y +  roi.m_min_x);
00235         *p++ = 0;                    // create left column of zeros
00236         for(int x = roi.m_min_x; x < roi.m_max_x; ++x)
00237           *p++ =
00238             //image.getPixel(x,y) + getPixel(x, y+1) +
00239             //getPixel(x+1, y) - getPixel(x, y);
00240             *q++ + *pxy1++ + *px1y++ - *pxy++;
00241       }
00242   }
00243   
00244   T getPixel (const int x, const int y) const{
00245     int lx = x, ly = y;
00246     if(lx < 0 || ly < 0) {
00247       return 0;
00248     }
00249     if(lx >= width) {
00250       ASSERT(false);
00251       lx = width - 1;
00252     }
00253     if(ly >= height) {
00254       ASSERT(false);
00255       ly = height - 1;
00256     }
00257     return array[width * ly + lx];
00258   };
00259 
00260   T getPixel (const int index) const{
00261     if(index < 0)
00262       return 0;
00263     if(index >= numpixels)
00264       return array[numpixels-1];
00265     else
00266       return array[index];
00267   };
00268 
00269         inline void setPixel (const int x, const int y, T val)
00270         {
00271                 if(x >= 0 && y >= 0 && x < width && y < height)
00272                         array[width * y + x] = value;
00273         };
00274 };
00275 
00276 
00277 #endif
00278 
00279 
00280 /*
00281  * 
00282  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00283  * 
00284  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00285  *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
00286  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00287  * 
00288  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00289  * 
00290  */
00291 
00292 
00293 
00294 
00295 
00296 
00297 

Generated on Mon Nov 8 17:07:51 2004 for MPT by  doxygen 1.3.9.1