// DLMSimilarity.C # include "nsl_include.h" # include "DLMwin.h" # include "DLMSimilarity.h" # include "DLM.h" int maxGallerySize = -1; // actual maximal simulated gallery size ; // in order to avoid multiple mem allocation ; DLMSimilarity::DLMSimilarity(nsl_string str,NslModule* parent) : NslModule(str,parent), S21("S21",this), showS21("showS21",this), ObjectSelectionMode("ObjectSelectionMode",this), preferredObject("preferredObject",this), alpha_S("alpha_S",this) { initModule(); } int DLMSimilarity::memAlloc(int _gallerySizeMax,int _frame,int _i1max,int _j1max, int _i2max,int _j2max,int _i1Rmax,int _j1Rmax) { gallerySizeMax = _gallerySizeMax; frame = _frame; i1max = _i1max; j1max = _j1max; i2max = _i2max; j2max = _j2max; i1Rmax = _i1Rmax; j1Rmax = _j1Rmax; S21.memAlloc(gallerySizeMax,i2max*j2max,i1Rmax*j1Rmax); showS21.memAlloc(i2max*j2max,i1Rmax*j1Rmax); return 1; } int DLMSimilarity::initModule() { selectedObject=0; strcpy(objectsFileName,"DLMobjects"); strcpy(modelsFileName,"DLMmodels"); kLevels=5; kDirections=8; kMaximum=1.5708; kFactor=0.707107; sigma=6.28318; tablesCreated=0; kxArr = new float[kLevels*kDirections]; kyArr = new float[kLevels*kDirections]; confidenceBak = new float[kLevels*kDirections]; deltaPhaseBak = new float[kLevels*kDirections]; return 1; } // ---- function to create tables for the similarity function ; void DLMSimilarity::createTables() { float k0, k0Angle; int kLevel, kDirection, kIndex; for (kLevel=0; kLevel statement above. */ float DLMSimilarity::similarity(float *trafoData1, float *trafoData2) { int kLevel, kIndex, kIndex2; float k0x, k0y, deltaPhase; float dispX=0, dispY=0, confidence, meanError=0; float PhiX=0, PhiY=0, GammaXX=0, GammaXY=0, GammaYY=0; for (kLevel=kLevels-1; kLevel>=0; kLevel--) { for (kIndex=kLevel*kDirections; kIndex<(kLevel+1)*kDirections; kIndex++) { kIndex2 = kIndex*2; confidence = trafoData1[kIndex2]*trafoData2[kIndex2]; confidenceBak[kIndex] = confidence; if (confidence!=0) { k0x = kxArr[kIndex]; k0y = kyArr[kIndex]; deltaPhase = (trafoData1[kIndex2+1]-trafoData2[kIndex2+1]); while (deltaPhase< dispX*k0x+dispY*k0y-M_PI) { deltaPhase += 2*M_PI; } while (deltaPhase>=dispX*k0x+dispY*k0y+M_PI) { deltaPhase -= 2*M_PI; } deltaPhaseBak[kIndex] = deltaPhase; PhiX += confidence*k0x*deltaPhase; PhiY += confidence*k0y*deltaPhase; GammaXX += confidence*k0x*k0x; GammaXY += confidence*k0x*k0y; GammaYY += confidence*k0y*k0y; } } dispX = (+PhiX*GammaYY-PhiY*GammaXY)/(GammaXX*GammaYY-GammaXY*GammaXY); dispY = (-PhiX*GammaXY+PhiY*GammaXX)/(GammaXX*GammaYY-GammaXY*GammaXY); } for (kIndex=0; kIndex> DLMObjectPath >> objects; // ---- check whether the preferred object index is valid ; if ((preferredObject.elem()<=0)||(preferredObject.elem()>objects)) { cout <<"\nWARNING : preferredObject (" << preferredObject.elem() <<") out of range (1-" <objects)) { cerr << "\nERROR: selected object (" << selectedObject << ") out of range [1,...," << objects <<"] !\n"; exit(1); } // ---- read object name ; for (object=0; object> DLMObjectName >> Oi1offset >> Oj1offset; if (!DLMObjects.good()) { cerr <<"\nERROR : Could not read DLMObjectName, Oi1offset, and Oj1offset from DLMObjects !!\n"; exit(1); } DLMObjects.close(); cout << "\n Object :\n " << DLMObjectName; // ---- check whether the object name has changed since the last init ; if (strcmp(DLMObjectName,objectName)!=0) { for (model=0; model<=gallerySize.elem(); model++) strcpy(modelName[model],"nobody"); strcpy(objectName,DLMObjectName); }; // ---- open object graph file and read basic parameters ; sprintf(tmpStr,"%s%s",DLMObjectPath,objectName); DLMGraph.open(tmpStr,ios::in); if (!DLMGraph.good()) { cerr << "\nERROR : Cannot open file "<> floatDummy >> floatDummy>> Sj1max >> floatDummy >> floatDummy>> Si1max; // ---- determine offsets ; Si1offset = Oi1offset - (i1max-2*frame-10)/2; if (Si1offset<0) Si1offset = 0; if (Si1offset>Si1max-i1max+2*frame) Si1offset=Si1max-i1max+2*frame; if (Si1offset<0) { cerr <<"\nERROR : object graph has less columns ("<Sj1max-j1max+2*frame) Sj1offset=Sj1max-j1max+2*frame; if (Sj1offset<0) { cerr <<"\nERROR : object graph has less rows ("<> kLevelsTmp >> kDirectionsTmp >> kMaximumTmp >> kFactorTmp >> sigmaTmp; if (kLevels!=kLevelsTmp) cerr << "\nERROR: kLevels (" << kLevelsTmp << ") in object graph != " << kLevels <<" !!\n"; if (kDirections!=kDirectionsTmp) cerr << "\nERROR: kDirections (" << kDirectionsTmp << ") in object graph != " << kDirections <<" !!\n"; if (kMaximum!=kMaximumTmp) cerr << "\nERROR: kMaximum (" << kMaximumTmp << ") in object graph != " << kMaximum <<" !!\n"; if (kFactor!=kFactorTmp) cerr << "\nERROR: kFactor (" << kFactorTmp << ") in object graph != " << kFactor <<" !!\n"; if (sigma!=sigmaTmp) cerr << "\nERROR: sigma (" << sigmaTmp << ") in object graph != " << sigma <<" !!\n"; // ---- read in jets of object graph ; float *DLMGImage = new float[i1max*j1max*kLevels*kDirections*2]; float ImageNormFloat; // float ImageNormChar[i1max*j1max]; float* ImageNormChar = new float[i1max*j1max]; for (Sj1=0, j1=-Sj1offset+frame; Sj1=0; Si1--, i1--) { ij1 = ij1Index(i1,j1,j1max); // check whether the node belongs to the selected subgraph ; char tmpChar=' '; if ((i1=i1max-frame)||(j1=j1max-frame)) { DLMGraph >> Norm >> Norm; tmpChar=' '; while (tmpChar!='\n') DLMGraph.get(tmpChar); for (k=0; k> ImageNormFloat >> ImageNormChar[ij1]; normCharFactor = 1/ImageNormChar[ij1]; tmpChar=' '; while (tmpChar!='\n') DLMGraph.get(tmpChar); for (k=0; k=0; Si1--) ; DLMGraph.close(); // ---- read in models and compute similarity matrices ----------------- ; // ---- variables ; int modelInGal; float *DLMGModel = new float[i1max*j1max*kLevels*kDirections*2]; // float ModelNormFloat[i2max*j2max]; // float ModelNormChar[i2max*j2max]; float* ModelNormFloat = new float[i2max*j2max]; float* ModelNormChar = new float[i2max*j2max]; // ---- open file of model names and read basic parameters ; ifstream DLMModels; DLMModels.open(modelsFileName,ios::in); if (!DLMModels.good()) { cerr << "\nERROR: Cannot open file "<> DLMModelPath >> models; // ---- check whether enough models are in DLMModels ; if (models> DLMModelName; if (strncmp(DLMModelName,objectName,strlen(objectName)-1)==0) break; }; if (modelInGal>models) cerr <<"\nERROR : person "<< objectName <<" not in DLMModels !!\n"; // ---- model loop ; cout << "\n\n Gallery : "; for (model=1; model<=gallerySize.elem(); model++) { // ---- read name of next model ; if (model!=1) { modelInGal++; if (modelInGal>models) { DLMModels.close(); DLMModels.open(modelsFileName,ios::in); if (!DLMModels.good()) { cerr << "\nERROR: Cannot open file "<> dummyString >> dummyInt; modelInGal=1; }; DLMModels >> DLMModelName; }; cout <<"\n "<> floatDummy>> floatDummy >> Sj2max >> floatDummy>> floatDummy >> Si2max; // ---- determine offsets ; Si2offset = (Si2max-i2max)/2; if (Si2offset<0) cerr <<"\nERROR : model graph has less columns ("<> kLevelsTmp >> kDirectionsTmp >> kMaximumTmp >> kFactorTmp >> sigmaTmp; if (kLevels!=kLevelsTmp) cerr << "\nERROR: kLevels (" << kLevelsTmp << ") in model graph != " << kLevels <<" !!\n"; if (kDirections!=kDirectionsTmp) cerr << "\nERROR: kDirections (" << kDirectionsTmp << ") in model graph != " << kDirections <<" !!\n"; if (kMaximum!=kMaximumTmp) cerr << "\nERROR: kMaximum (" << kMaximumTmp << ") in model graph != " << kMaximum <<" !!\n"; if (kFactor!=kFactorTmp) cerr << "\nERROR: kFactor (" << kFactorTmp << ") in model graph != " << kFactor <<" !!\n"; if (sigma!=sigmaTmp) cerr << "\nERROR: sigma (" << sigmaTmp << ") in model graph != " << sigma <<" !!\n"; // ---- read jets of model graph ; for (Sj2=0, j2=-Sj2offset; Sj2=0; Si2--, i2--) { ij2 = ij2Index(i2,j2,j2max); // check whether the node belongs to the selected subgraph ; char tmpChar; if ((i2<0)||(i2>=i2max)||(j2<0)||(j2>=j2max)) { DLMGraph >> Norm >> Norm; tmpChar=' '; while (tmpChar!='\n') DLMGraph.get(tmpChar); for (k=0; k> ModelNormFloat[ij2] >> ModelNormChar[ij2]; normCharFactor = 1/ModelNormChar[ij2]; tmpChar=' '; while (tmpChar!='\n') DLMGraph.get(tmpChar); for (k=0; k=0; Si2--) ; DLMGraph.close(); // ---- compute similarity matrix ; for (i2=0, ij2=0; i2elem(ij2,ij1R) S21[model][ij2][ij1R] = maximum(alpha_S.elem(), similarity(&(DLMGImage[ij1*kLevels*kDirections*2]), &(DLMGModel[ij2*kLevels*kDirections*2]))); } } // for (model=1; model<=gallerySize.elem(); model++) ; if (gallerySize.elem()>0) showS21 = S21[int(showLayer.elem())]; // ---- free memory ; delete[] DLMGModel; delete[] DLMGImage; // ---- close file of model names ; DLMModels.close(); S21.send(); return 1; } int DLMSimilarity::initRunTemp(NslFloat0& gallerySize) { int model, k, ij1R, ij2; // some index variables ; if (gallerySize.elem()>0) { float S; if (gallerySize.elem()>0) for (ij2=0; ij2