Image.cpp

Go to the documentation of this file.
00001 
00004 // This file is part of the SaliencyToolbox - Copyright (C) 2006-2007
00005 // by Dirk B. Walther and the California Institute of Technology.
00006 // See the enclosed LICENSE.TXT document for the license agreement. 
00007 // More information about this project is available at: 
00008 // http://www.saliencytoolbox.net
00009 
00010 #include "Image.h"
00011 #include "mexLog.h"
00012 #include <cstring>
00013 
00014 // ######################################################################
00015 Image::Image()
00016   : itsInitialized(false)
00017 {}
00018 
00019 // ######################################################################
00020 Image::Image(const mxArray *arr)
00021 {
00022   initialize(arr);
00023 }
00024 
00025 // ######################################################################
00026 Image::Image(const Image& other)
00027 {
00028   initialize(other.getConstArray());
00029 }
00030 
00031 // ######################################################################
00032 Image::Image(const int w, const int h)
00033 {
00034   initialize(w,h);
00035 }
00036     
00037 // ######################################################################
00038 Image Image::operator=(const Image other)
00039 {
00040   itsInitialized = other.isInitialized();
00041   if (isInitialized())
00042     initialize(other.getConstArray());
00043   return *this;
00044 }
00045 
00046 // ######################################################################
00047 void Image::initialize(const mxArray *arr)
00048 {
00049   initialize(mxGetN(arr),mxGetM(arr));
00050   memcpy(beginw(),mxGetPr(arr),getSize()*sizeof(double));
00051 }
00052 
00053 // ######################################################################
00054 void Image::initialize(const int w, const int h)
00055 {
00056   itsInitialized = true;
00057   itsArray = mxCreateDoubleMatrix(h,w,mxREAL);
00058 }
00059 
00060 // ######################################################################
00061 int Image::getWidth() const
00062 {
00063   ASSERT(isInitialized());
00064   return mxGetN(getConstArray());
00065 }
00066 
00067 // ######################################################################
00068 int Image::getHeight() const
00069 {
00070   ASSERT(isInitialized());
00071   return mxGetM(getConstArray());
00072 }
00073 
00074 // ######################################################################
00075 int Image::getSize() const
00076 {
00077   ASSERT(isInitialized());
00078   return mxGetNumberOfElements(getConstArray());
00079 }
00080 
00081 // ######################################################################
00082 Image::const_iterator Image::begin() const
00083 {
00084   return mxGetPr(getConstArray());
00085 }
00086 
00087 // ######################################################################
00088 Image::const_iterator Image::end() const
00089 {
00090   return (begin() + getSize());
00091 }
00092 
00093 // ######################################################################
00094 Image::iterator Image::beginw()
00095 {
00096   return mxGetPr(getArray());
00097 }
00098 
00099 // ######################################################################
00100 Image::iterator Image::endw()
00101 {
00102   return (beginw() + getSize());
00103 }
00104 
00105 // ######################################################################
00106 bool Image::isInitialized() const
00107 {
00108   return itsInitialized;
00109 }
00110 
00111 // ######################################################################
00112 mxArray* Image::getArray()
00113 {
00114   return itsArray;
00115 }
00116 
00117 // ######################################################################
00118 const mxArray* Image::getConstArray() const
00119 {
00120   return itsArray;
00121 }
00122 
00123 
00124 // ######################################################################
00125 void Image::clamp(const double bottom, const double top)
00126 {
00127   ASSERT(isInitialized());
00128   for (iterator ptr = beginw(); ptr != endw(); ++ptr)
00129   {
00130     if (*ptr < bottom) *ptr = bottom;
00131     if (*ptr > top) *ptr = top;
00132   }
00133 }
00134     
00135 // ######################################################################
00136 void Image::clampBottom(const double bottom)
00137 {
00138   ASSERT(isInitialized());
00139   for (iterator ptr = beginw(); ptr != endw(); ++ptr)
00140     if (*ptr < bottom) *ptr = bottom;
00141 }
00142     
00143 // ######################################################################
00144 void Image::clampTop(const double top)
00145 {
00146   ASSERT(isInitialized());
00147   for (iterator ptr = beginw(); ptr != endw(); ++ptr)
00148     if (*ptr > top) *ptr = top;
00149 }
00150     
00151 // ######################################################################
00152 Image Image::operator*=(const double factor)
00153 {
00154   ASSERT(isInitialized());
00155   for (iterator ptr = beginw(); ptr != endw(); ++ptr)
00156     *ptr *= factor;
00157   return *this;
00158 }
00159   
00160 // ######################################################################
00161 double Image::getVal(int index) const
00162 {
00163   ASSERT(isInitialized());
00164   ASSERT(index < getSize());
00165   return *(begin()+index);
00166 }
00167 
00168 // ######################################################################
00169 double Image::getVal(int x, int y) const
00170 {
00171   ASSERT(coordsOK(x,y));
00172   const_iterator ptr = begin() + x * getHeight() + y;
00173   return *ptr;
00174 }
00175 
00176 // ######################################################################
00177 void Image::setVal(int index, double val)
00178 {
00179   ASSERT(isInitialized());
00180   ASSERT(index < getSize());
00181   *(beginw()+index) = val;
00182 }
00183   
00184 // ######################################################################
00185 void Image::setVal(int x, int y, double val)
00186 {
00187   ASSERT(coordsOK(x,y));
00188   iterator ptr = beginw() + x * getHeight() + y;
00189   *ptr = val;
00190 }
00191 
00192 // ######################################################################
00193 void Image::getLocalMaxima(const double thresh, int *lm_num, double *lm_sum)
00194 {
00195   ASSERT(isInitialized());
00196 
00197   const int w = getWidth();
00198   const int h = getHeight();
00199 
00200 
00201   // then get the mean value of the local maxima:
00202   *lm_sum = 0.0; *lm_num = 0;
00203   
00204   for (int j = 1; j < h - 1; j ++)
00205     for (int i = 1; i < w - 1; i ++)
00206       {
00207         double val = getVal(i,j);
00208         if (val >= thresh &&
00209             val >= getVal(i-1, j) &&
00210             val >= getVal(i+1, j) &&
00211             val >= getVal(i, j+1) &&
00212             val >= getVal(i, j-1))  // local max
00213           {
00214             *lm_sum += val;
00215             (*lm_num)++;
00216           }
00217       }
00218   return;
00219 }
00220   
00221 // ######################################################################
00222 bool Image::coordsOK(int x, int y) const
00223 {
00224   if (!isInitialized()) return false;
00225   
00226   return ((x < getWidth()) && (y < getHeight()));
00227 }

Generated on Fri Sep 7 13:09:49 2007 for SaliencyToolbox by  doxygen 1.5.2