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

mac/build/mpisearch.framework/Versions/Current/Headers/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       //cout << "RImage<T>: allocated RImage with width = " << width << " height = " << height << endl;
00057     }
00058         inline void deleteArray(){
00059                 delete [] array;
00060         }
00061     virtual ~RImage ( );
00062     virtual T getPixel (const int x, const int y) const;
00063     virtual T getPixel (const int index) const;
00064     void setPixel (const int x, const int y, const T &value);
00065     void setPixel (const int index, const T &value);
00066     
00067     void print ( ) const;
00068     void print ( const int m ) const;
00069     T *array;
00070     
00071     int width;
00072     int height;
00073     int numpixels;
00074     int my_memory;
00075 };
00076 
00077 template <class T>
00078 inline RImage<T>::RImage (const T *array_, const int width_, const int height_ )
00079 {
00080   width = width_;
00081   height = height_;
00082   numpixels = width * height;
00083   array = const_cast<T *>(array_);
00084   my_memory = 0;
00085 }
00086   
00087 template <class T>
00088 inline RImage<T>::RImage ( const int width_, const int height_ )
00089 {
00090         setSize(width_,height_);
00091 }
00092 
00093 template <class T>
00094 inline RImage<T>::RImage ( const int width_, const int height_ , const T val)
00095 {
00096     setSize(width_,height_);
00097     T *ptr = array;
00098     for(int y=0; y <height_; y++)
00099       for(int x=0; x <width_; x++)      
00100            *(ptr++) = val; 
00101 }
00102 
00103 template <class T>
00104 inline RImage<T>::~RImage ()
00105 {
00106   if(my_memory){
00107     //cout << "RImage::~RImage(): deleting" << endl;
00108     delete [] array;
00109   }
00110 }
00111 
00112 template <class T>
00113 inline T RImage<T>::getPixel ( const int x, const int y ) const
00114 {
00115   if(x >= 0 && y >= 0 && x < width && y < height)
00116     return array[width * y + x]; 
00117   else 
00118 #ifdef WIN32
00119         return array[0];
00120 #else
00121     return 0;
00122 #endif
00123 }
00124 
00125 template <class T>
00126 inline T RImage<T>::getPixel ( const int index ) const
00127 {
00128   if((index >= 0) && (index < numpixels ))
00129     return array[index];
00130   else
00131 #ifdef WIN32
00132         return array[0];
00133 #else
00134     return 0;
00135 #endif
00136 }
00137 
00138 template <class T>
00139 inline void RImage<T>::setPixel ( const int x, const int y, const T &value )
00140 {
00141   if(x >= 0 && y >= 0 && x < width && y < height)
00142     array[width * y + x] = value;
00143 }
00144 
00145 template <class T>
00146 inline void RImage<T>::setPixel ( const int index, const T &value )
00147 {
00148   if((index >= 0) && (index < numpixels ))
00149     array[index] = value;
00150 }
00151 
00152 template <class T>
00153 void RImage<T>::print ( ) const 
00154 {
00155   for(int y = 0; y < height; y++) {
00156     for(int x = 0; x < width; x++) {
00157       cout << getPixel(x,y) << " ";
00158     }
00159     cout << endl;
00160   }
00161 }
00162 
00163 template <class T>
00164 void RImage<T>::print ( const int m ) const 
00165 {
00166   for(int y = 0; y < min(m,height); y++) {
00167     for(int x = 0; x < min(m,width); x++) {
00168       cout << getPixel(x,y) << " ";
00169     }
00170     cout << endl;
00171   }
00172 }
00173 
00174 
00175 //  RImage<T> returns the wrong values if we scan too far
00176 //  so we override getPixel in derived class and provide constructor.
00177 
00178 
00179 template <class T>
00180 class RIntegral : public RImage<T>
00181 {
00182 public:
00183   RIntegral(const int width, const int height){
00184       setSize(width+1,height+1);
00185   }
00186 
00187   RIntegral(const RImage<T> &image) {
00188       setSize(image.width+1,image.height+1);
00189     integrate(image);
00190   }
00191 
00192   RIntegral(const RImage<T> &image, ROI &roi){
00193       setSize(image.width+1,image.height+1);
00194     integrate(image, roi);
00195   }
00196   
00197   void integrate(const RImage<T> &image){
00198       T * p = array;
00199       for(int x = 0; x < width; ++x) // create top row of zeros
00200         *p++ = 0;
00201       for(int y = 0; y < height-1; ++y){
00202         *p++ = 0;                    // create left column of zeros
00203         for(int x = 0; x < width-1; ++x)
00204           *p++ =
00205             image.getPixel(x,y) + getPixel(x, y+1) +
00206             getPixel(x+1, y) - getPixel(x, y);
00207       }
00208   }
00209   void integrate(const RImage<T> &image, ROI roi){
00210                 //cout << "RImage<T>::integrate: " << roi << endl;
00211     //roi.fitImage(*this);
00212     //cout << "RImage<T>::integrate: " << roi << endl;
00213 
00214     //cout << "roi: (" << roi.m_min_x << ", " << roi.m_min_y << ", "<< roi.m_max_x << ", "<< roi.m_max_y << ")" << endl;
00215       T *p = array + (width*roi.m_min_y + roi.m_min_x);
00216       T *pxy, *px1y, *pxy1, *q;
00217       for(int x = roi.m_min_x; x < roi.m_max_x+1; ++x) // create top row of zeros
00218         *p++ = 0;
00219       for(int y = roi.m_min_y; y < roi.m_max_y; ++y){
00220         //cout << "integrating line " << y << " of " << image.height << endl;
00221         p = array + (width*(y+1) + roi.m_min_x);
00222         pxy = array + (width * y +  roi.m_min_x);
00223         px1y = array + (width * y +  roi.m_min_x + 1);
00224         pxy1 = array + (width * (y+1) +  roi.m_min_x);
00225         q = image.array + (image.width * y +  roi.m_min_x);
00226         *p++ = 0;                    // create left column of zeros
00227         for(int x = roi.m_min_x; x < roi.m_max_x; ++x)
00228           *p++ =
00229             //image.getPixel(x,y) + getPixel(x, y+1) +
00230             //getPixel(x+1, y) - getPixel(x, y);
00231             *q++ + *pxy1++ + *px1y++ - *pxy++;
00232       }
00233   }
00234   
00235   T getPixel (const int x, const int y) const{
00236     int lx = x, ly = y;
00237     if(lx < 0 || ly < 0) {
00238       return 0;
00239     }
00240     if(lx >= width) {
00241       ASSERT(false);
00242       lx = width - 1;
00243     }
00244     if(ly >= height) {
00245       ASSERT(false);
00246       ly = height - 1;
00247     }
00248     return array[width * ly + lx];
00249   };
00250 
00251   T getPixel (const int index) const{
00252     if(index < 0)
00253       return 0;
00254     if(index >= numpixels)
00255       return array[numpixels-1];
00256     else
00257       return array[index];
00258   };
00259 
00260         inline void setPixel (const int x, const int y, T val)
00261         {
00262                 if(x >= 0 && y >= 0 && x < width && y < height)
00263                         array[width * y + x] = value;
00264         };
00265 };
00266 
00267 
00268 #endif
00269 
00270 
00271 /*
00272  * 
00273  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00274  * 
00275  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00276  *    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.
00277  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00278  * 
00279  * 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.
00280  * 
00281  */
00282 
00283 
00284 
00285 
00286 
00287 
00288 

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