TarzaNN
TarzaNN neural network simulator
|
00001 /**************************************************************************** 00002 ** 00003 ** Copyright C 2002-2012 Laboratory for Active and Attentive Vision (LAAV), Department of Computer Science and Engineering, York University, Toronto, ON, Canada. 00004 ** All rights reserved. 00005 ** 00006 ** This file is part of the TarzaNN Neural Network Simulator. 00007 ** 00008 ** This file may be distributed and/or modified under the terms of the 00009 ** GNU General Public License version 2 as published by the Free Software 00010 ** Foundation and appearing in the file LICENSE.GPL included in the 00011 ** packaging of this file. 00012 ** 00013 ** See http://www.tarzann.org/gpl/ for GPL licensing information. 00014 ** 00015 ** Contact info@tarzann.org if any conditions of this licensing are 00016 ** not clear to you. 00017 ** 00018 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00019 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00020 ** 00021 ****************************************************************************/ 00022 00023 #ifndef MATRIX_H_INCLUDED 00024 #define MATRIX_H_INCLUDED 00025 #include <stdio.h> 00026 #include <assert.h> 00027 #include <vector> 00028 using namespace std; 00029 #include "common.h" 00030 00031 #include "Array2D.h" 00032 00040 class Matrix : public Array2D<float>{ 00041 typedef int (Matrix::*ChangeOp)(int x, int y); 00042 protected: 00044 void serialize(char* buf); 00046 Matrix( char* buf); 00048 Matrix( QString fileName, int fileType); 00050 int getSendSize(); 00052 void createKernel(float* kernel); 00053 00054 bool created; 00055 Matrix( ); 00056 00057 public: 00058 Matrix( const int nXSize, const int nYSize); 00059 Matrix( const Matrix& matrix); 00060 00061 Matrix& operator = ( const Matrix& matrix); 00062 Matrix& copyData ( const Matrix& matrix); 00063 00065 void* getImage(); 00066 00073 void* getImageCopy(uint32_t* x, uint32_t* y, uint32_t* size, void* p); 00074 00075 bool isZero(); 00077 QPoint* getNonZero(); 00078 inline void setValue(float value){ 00079 uint32_t i,j; 00080 00081 for (j= 0; j<height; j++) 00082 for (i= 0; i<width; i++) 00083 (*this)(i,j)= value; 00084 } 00085 inline void setZero(){ 00086 memset( data, 0, memorySize); 00087 } 00088 inline void setOne(){ 00089 memset( data, 1, memorySize); 00090 } 00091 void multiplyScalar(float item); 00092 void addScalar(float item); 00093 void addMatrix(Matrix* input); 00094 bool equals(Matrix* input); 00095 bool equals(Matrix* input, int tol); 00096 void subtractMatrix(Matrix* input); 00098 void dotSquare(); 00099 void punch_out(int window); 00100 void circularMask(); 00101 00102 void clamp(float the_value); 00103 void applyGatingControl(Matrix* theGatingControl); 00104 void applyIOR(Matrix* theIORMap); 00105 00107 void applyThreshold(float the_value); 00109 void applyThresholdBinarize(float the_value); 00111 void applyThresholdAndMark(float value, Matrix* gc); 00112 void setPixels(unsigned char* data); 00113 void setPixels(int* data); 00115 void downsample(Matrix* input); 00117 void downsample_skip(Matrix* input); 00119 void upsample(Matrix* input); 00121 void upsample_skip(Matrix* input, float fill=0); 00122 void resample(Matrix* input, bool skip=false, float fill=0); 00123 float average(); 00124 void setZeroMean(); 00125 void setOneSum(); 00126 void normalize(float scale); 00127 void printContent(); //debug function 00128 void printContentMap(); //debug function 00129 void save(QDataStream* outStream); 00130 void read(QDataStream* inStream); 00131 void saveActivations(char* filename); 00133 QImage* toSTMQImage(int max=255); 00134 00136 void scaleValues(float minScaleValue, float maxScaleValue); //rescales the values within those boundaries 00137 00141 void dotProduct(Matrix *otherMatrix); 00144 void rectangleCopy(uint32_t r_w, uint32_t r_h, uint32_t x1,uint32_t y1, Matrix* otherMatrix, uint32_t o_x1, uint32_t o_y1); 00147 void shift(int dx, int dy, int fillOption=0); 00149 void rectangleFill(uint32_t x, uint32_t y, uint32_t w, uint32_t h, float theValue); 00150 00152 inline float sumAll(){ 00153 float sum = 0; 00154 uint32_t i,j; 00155 00156 for (j= 0; j<height; j++) 00157 for (i= 0; i<width; i++) 00158 sum += (*this)(i,j); 00159 return sum; 00160 } 00161 00163 inline float sumAllPositive(){ 00164 float sum = 0; 00165 uint32_t i,j; 00166 00167 for (j= 0; j<height; j++) 00168 for (i= 0; i<width; i++) 00169 if((*this)(i,j)>0) 00170 sum += (*this)(i,j); 00171 return sum; 00172 } 00173 00175 inline float getMaxValue(){ 00176 float maxValue = (float)((float*)(data))[0]; 00177 uint32_t i,j; 00178 00179 for (j= 0; j<height; j++) 00180 for (i= 0; i<width; i++) 00181 if ((*this)(i,j)>maxValue) 00182 maxValue = (*this)(i,j); 00183 return maxValue; 00184 } 00185 00186 00188 float getMinValue(); 00189 float sumAllAbsolute(); 00190 void addNoise(); 00191 00193 QPoint* getMaxCoords(); 00195 QPoint* getMaxCoordsSpiral(); 00196 00197 00198 /*UTILITIES */ 00199 inline int changeBrightnessOp( int value, int brightness ); 00200 inline int changeContrastOp( int value, int contrast ); 00201 inline int changeGammaOp( int value, int gamma ); 00202 inline int changeUsingTable( int value, const int table[] ); 00203 QImage changeImage( ChangeOp changeOp, const QImage& image, int value ); 00204 QImage changeBrightness( const QImage& image, int brightness ); 00205 QImage changeContrast( const QImage& image, int contrast ); 00206 QImage changeGamma( const QImage& image, int gamma ); 00207 00208 }; 00209 typedef vector<Matrix*> MatrixVector; 00210 #endif /*MATRIX_H_DEFINED*/