00001
00018 #include "vetMotionLame.h"
00019
00020 #define RED_COEF .2989961801
00021 #define GREEN_COEF .5869947588
00022 #define BLUE_COEF .1139912943
00023
00024
00025 #include "../filters/vetDigitalFilter.h"
00026
00027 vetDFMatrix* lowpass = vetDFMatrix::createKernel_3x3(VETDF_3x3_gaussian);
00028
00029
00030 vetMotionLame::vetMotionLame(vetMotionLameParameters* initParams) : vetVision()
00031 {
00032 INFO("vetMotionLame::vetMotionLame(..* initParams) : vetMotion() [CONTRUCTOR]")
00033
00034 buffer = NULL;
00035 diff = NULL;
00036
00037 setParameters(initParams);
00038 reset();
00039 }
00040
00041 vetMotionLame::~vetMotionLame()
00042 {
00043 INFO("vetMotionLame::~vetMotionLame() [DESTRUCTOR]")
00044
00045 if (buffer != NULL)
00046 delete buffer;
00047
00048 if (diff != NULL)
00049 delete diff;
00050
00051
00052 delete myParams;
00053
00054 }
00055
00056 VETRESULT vetMotionLame::reset()
00057 {
00058 INFO("int vetMotionLame::reset() [SET DEFAULT PARAMETERS]")
00059
00060 if (buffer != NULL)
00061 delete buffer;
00062 buffer = NULL;
00063
00064 if (diff != NULL)
00065 delete diff;
00066 diff = NULL;
00067
00068 alertCall = NULL;
00069 alertCallArgument = NULL;
00070
00071 lastDiffValue = 0;
00072
00073 setName("Lame Motion Detection");
00074 setDescription("detect difference between 2 frames.");
00075 setVersion(1.0);
00076
00077 return VETRET_OK;
00078 }
00079
00080
00081 VETRESULT vetMotionLame::setParameters(vetMotionLameParameters* initParams)
00082 {
00083 if ( initParams == NULL )
00084 myParams = new vetMotionLameParameters();
00085 else
00086 myParams = initParams;
00087
00088 return VETRET_OK;
00089 }
00090
00091
00092
00093
00094 void vetMotionLame::initFrame(vetFrameRGB24& img)
00095 {
00096 if (buffer != NULL)
00097 delete buffer;
00098
00099 buffer = new vetFrameRGB24(img);
00100
00101 if (diff != NULL)
00102 delete diff;
00103
00104 diff = new vetFrameRGB24(img.width, img.height);
00105
00106
00107 lastDiffValue = 0;
00108 }
00109
00110
00111
00112
00113
00114
00115 VETRESULT vetMotionLame::importFrom(vetFrameRGB24& img)
00116 {
00117 INFO("int vetMotionLame::importFrom(vetFrameRGB24& img) [reading data]")
00118
00119 if (buffer == NULL)
00120 {
00121 initFrame(img);
00122 return VETRET_OK;
00123 }
00124
00125 getDifference(*diff, *buffer, img);
00126
00127 if ( myParams->doEval && evalDifference(*diff) && myParams->doAlert )
00128 doAlert();
00129
00130 *buffer = img;
00131
00132 return VETRET_OK;
00133 }
00134
00135
00136
00137
00138 VETRESULT vetMotionLame::importFrom(vetFrameYUV420& img)
00139 {
00140 INFO("int vetMotionLame::importFrom(vetFrameYUV420& img) [reading data]")
00141
00142 return VETRET_NOT_IMPLEMENTED;
00143
00144 }
00145
00146
00147
00148
00149 VETRESULT vetMotionLame::importFrom(vetFrameT<unsigned char>& img)
00150 {
00151 INFO("int vetMotionLame::importFrom(vetFrameT<unsigned char>& img) [reading data]")
00152
00153 return VETRET_NOT_IMPLEMENTED;
00154 }
00155
00156
00157 int vetMotionLame::evalDifference(vetFrameRGB24& diff_img)
00158 {
00159 INFO("")
00160
00161 vetDigitalFilter::doProcessing(diff_img, *buffer, *lowpass);
00162
00163 lastDiffValue = getDifferenceBrightnessValue(*buffer);
00164
00165 if ( myParams->threshold > lastDiffValue )
00166 return 0;
00167
00168 return lastDiffValue;
00169 }
00170
00171
00172
00173 long vetMotionLame::getDifferenceBrightnessValue(vetFrameRGB24& diff_img)
00174 {
00175 INFO("")
00176
00177 long result = 0;
00178
00179 for (unsigned int i=0; i<diff_img.height*diff_img.width; i++)
00180 {
00181 result += (long)(diff_img.data[i][0] * RED_COEF + diff_img.data[i][0] * GREEN_COEF + diff_img.data[i][0] * BLUE_COEF);
00182 }
00183
00184 return result;
00185 }
00186
00187
00188
00189
00191
00192
00193
00194
00195 vetMotionLameParameters::vetMotionLameParameters(long threshold, bool doAlert, bool doEval)
00196 {
00197 reset();
00198
00199 setDoEval(doEval);
00200 setDoAlert(doAlert);
00201 setThresholdValue(threshold);
00202 }
00203
00204 void vetMotionLameParameters::reset()
00205 {
00206 doAlert = false;
00207 doEval = true;
00208 threshold = 1;
00209 }
00210
00211
00212 void vetMotionLameParameters::setThresholdValue(long value)
00213 {
00214 threshold = value;
00215 }
00216
00217
00218
00219 VETRESULT vetMotionLameParameters::saveToStreamXML(FILE *fp)
00220 {
00221
00222 if ( fp == NULL )
00223 return VETRET_PARAM_ERR;
00224
00225 if( fprintf(fp, "<vetMotionLameParameters>\n") == EOF )
00226 return VETRET_INTERNAL_ERR;
00227
00228 if ( fprintf(fp, " <threshold=\"%ld\" />\n", threshold) == EOF)
00229 return VETRET_INTERNAL_ERR;
00230
00231 if ( fprintf(fp, " <doEval=\"%u\" />\n", (int)doEval) == EOF)
00232 return VETRET_INTERNAL_ERR;
00233
00234 if ( fprintf(fp, " <doAlert=\"%u\" />\n", (int)doAlert) == EOF)
00235 return VETRET_INTERNAL_ERR;
00236
00237 if( fprintf(fp, "</vetMotionLameParameters>\n") == EOF )
00238 return VETRET_INTERNAL_ERR;
00239
00240 return VETRET_OK;
00241 }
00242
00243
00244 VETRESULT vetMotionLameParameters::loadFromStreamXML(FILE *fp)
00245 {
00246 if ( fscanf(fp, "<vetMotionLameParameters>\n") == EOF )
00247 throw "error in XML file, unable to import data.";
00248
00249 if ( fscanf(fp, " <threshold=\"%ld\" />\n", &threshold) == EOF )
00250 throw "error in XML file, unable to import data.";
00251
00252 int boolTmp = 0;
00253 if ( fscanf(fp, " <doEval=\"%u\" />\n", &boolTmp) == EOF )
00254 throw "error in XML file, unable to import data.";
00255
00256 if (boolTmp == 0)
00257 doEval = false;
00258 else
00259 doEval = true;
00260
00261 boolTmp = 0;
00262 if ( fscanf(fp, " <doAlert=\"%u\" />\n", &boolTmp) == EOF )
00263 throw "error in XML file, unable to import data.";
00264
00265 if (boolTmp == 0)
00266 doAlert = false;
00267 else
00268 doAlert = true;
00269
00270 return VETRET_OK;
00271 }
00272
00273
00274
00275