// DlmSimilarity.C # include "nsl_include.h" #include "DLMwin.h" #include "DLMdisplay.h" # include "DlmSimilarity.h" # include "Dlm.h" DlmSimilarity::DlmSimilarity(nsl_string str,NslModule* parent) : DlmCommon(str,parent), DlmObjects("DlmObjects",this), DlmGraph("DlmGraphs",this), DlmModels("DlmModel",this), DlmGImage("DlmGImage",this), ImageNormChar("ImageNormChar",this), DlmGModel("DlmGModel",this), ModelNormFloat("ModelNormFloat",this), ModelNormChar("ModelNormChar",this), kxArr("kxArr",this), kyArr("kyArr",this), confidenceBak("confidenceBak",this), deltaPhaseBak("deltaPhaseBak",this), objectsFileName("objectsFileName",this), modelsFileName("modelsFileName",this), objectName("objectName",this), // name of the image graph file ; gallerySize("gallerySize",this), // actual size of the simmulated gallery ; ObjectSelectionMode("ObjectSelectionMode",this), preferredObject("preferredObject",this), alpha_S("alpha_S",this) { nsl_string _str; for (int i=0; i statement above. */ float DlmSimilarity::similarity(NslFloat1& trafoData1,int i1, int j1, NslFloat1& trafoData2,int i2, int j2) { 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[(i1*j1max+j1)*kLevels*kDirections*2+kIndex2]* trafoData2[(i2*j2max+j2)*kLevels*kDirections*2+kIndex2]; confidenceBak[kIndex] = confidence; if (confidence!=0) { k0x = kxArr[kIndex]; k0y = kyArr[kIndex]; deltaPhase = (trafoData1[(i1*j1max+j1)*kLevels*kDirections*2+kIndex2+1]- trafoData2[(i2*j2max+j2)*kLevels*kDirections*2+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; kIndexobjects)) { NSLoutput("\nWARNING : preferredObject (",preferredObject.elem()); NSLoutput(") out of range (1-",objects); NSLoutput("), set preferredObject to 1 !!\n"); preferredObject=1; } // ---- select object ; switch (int(ObjectSelectionMode.elem())) { case 1: // PREFERRED : selectedObject = int(preferredObject.elem()); break; case 2: // NEXT : selectedObject++; break; case 3: // RANDOM : selectedObject = rand()%objects+1; break; default: NSLoutput("\nERROR: ObjectSelectionMode ",ObjectSelectionMode.elem()); NSLoutput(" not valid !\n"); break; } if ((selectedObject<=0)||(selectedObject>objects)) { NSLoutput("\nERROR: selected object (",selectedObject); NSLoutput(") out of range [1,...,",objects); NSLoutput("] !\n"); exit(1); } // ---- read object name for (object=0; objectSi1max-i1max+2*frame) Si1offset=Si1max-i1max+2*frame; if (Si1offset<0) { NSLoutput("\nERROR : object graph has less columns (",Si1max); NSLoutput(") than required (",i1max-2*frame); NSLoutput(") !!\n"); exit(1); } Sj1offset = Oj1offset - (j1max-2*frame-10)/2; if (Sj1offset<0) Sj1offset = 0; if (Sj1offset>Sj1max-j1max+2*frame) Sj1offset=Sj1max-j1max+2*frame; if (Sj1offset<0) { NSLoutput("\nERROR : object graph has less rows (",Sj1max); NSLoutput(") than required (",j1max-2*frame); NSLoutput(") !!\n"); exit(1); } NSLoutput(" \t(i = ",Si1offset); NSLoutput(",...,",Si1offset+i1max-2*frame-1); NSLoutput("; j = ",Sj1offset); NSLoutput(",...,",Sj1offset+j1max-2*frame-1); NSLoutput(")\n"); // ---- read in trafo parameter of object graph and check them ; DlmGraph.read(kLevelsTmp); DlmGraph.read(kDirectionsTmp); DlmGraph.read(kMaximumTmp); DlmGraph.read(kFactorTmp); DlmGraph.read(sigmaTmp); if (kLevels!=kLevelsTmp) { NSLoutput("\nERROR: kLevels (",kLevelsTmp); NSLoutput(") in object graph != ",kLevels); NSLoutput(" !!\n"); } if (kDirections!=kDirectionsTmp) { NSLoutput("\nERROR: kDirections (",kDirectionsTmp); NSLoutput(") in object graph != ",kDirections); NSLoutput(" !!\n"); } if (kMaximum!=kMaximumTmp) { NSLoutput("\nERROR: kMaximum (",kMaximumTmp); NSLoutput(") in object graph != ",kMaximum); NSLoutput(" !!\n"); } if (kFactor!=kFactorTmp) { NSLoutput("\nERROR: kFactor (",kFactorTmp); NSLoutput(") in object graph != ",kFactor); NSLoutput(" !!\n"); } if (sigma!=sigmaTmp){ NSLoutput("\nERROR: sigma (",sigmaTmp); NSLoutput(") in object graph != ",sigma); NSLoutput(" !!\n"); } // ---- read in jets of object graph ; float ImageNormFloat; for (Sj1=0, j1=-Sj1offset+frame; Sj1=0; Si1--, i1--) { // check whether the node belongs to the selected subgraph ; char tmpChar=' '; if ((i1=i1max-frame)||(j1=j1max-frame)) { DlmGraph.read(Norm); DlmGraph.read(Norm); tmpChar=' '; while (tmpChar!='\n') DlmGraph.read(tmpChar); for (k=0; k=0; Si1--) DlmGraph.close_file(); DlmImageModelData[0] = DlmGImage; // ---- read in models and compute similarity matrices ----------------- ; // ---- variables ; int modelInGal; // ---- open file of model names and read basic parameters ; // DlmModels.init_file(modelsFileName,""); // join with next call if (DlmModels.new_file(modelsFileName,NSL_INPUT) == -1) { NSLoutput("Bad file name: "); modelsFileName.nslPrint(); exit(1); } DlmModels.read(DlmModelPath); DlmModels.read(models); // ---- check whether enough models are in DlmModels ; if (modelsmodels) { NSLoutput("\nERROR : person "); objectName.nslPrint(); NSLoutput(" not in DlmModels !!\n"); } // ---- model loop ; NSLoutput("\n\n Gallery : "); for (model=1; model<=gallerySize; model++) { // ---- read name of next model if (model!=1) { modelInGal++; if (modelInGal>models) { DlmModels.close_file(); // DlmModels.init_file(modelsFileName,""); // join with next call if (DlmModels.new_file(modelsFileName,NSL_INPUT) == -1) { NSLoutput("Bad file name: "); modelsFileName.nslPrint(); exit(1); } DlmModels.read(dummyString); DlmModels.read(dummyInt); modelInGal=1; } DlmModels.read(DlmModelName); } NSLoutput("\n "); DlmModelName.nslPrint(); // ---- check whether the model name has changed since the last init ; if (DlmModelName == modelName[model]) continue; modelName[model] = DlmModelName; // ---- open model graph file and read basic parameters ; tmpStr = DlmModelPath + DlmModelName; // DlmGraph.init_file(tmpStr,""); // join with next call if (DlmGraph.new_file(tmpStr,NSL_INPUT,NSL_BINARY) == -1) { NSLoutput("Bad file name: "); tmpStr.nslPrint(); exit(1); } DlmGraph.read(floatDummy); DlmGraph.read(floatDummy); DlmGraph.read(Sj2max); DlmGraph.read(floatDummy); DlmGraph.read(floatDummy); DlmGraph.read(Si2max); // ---- determine offsets ; Si2offset = (Si2max-i2max)/2; if (Si2offset<0) { NSLoutput("\nERROR : model graph has less columns (",Si2max); NSLoutput(") than required (",i2max); NSLoutput(") !!\n"); } Sj2offset = (Sj2max-j2max)/2; if (Sj2offset<0){ NSLoutput("\nERROR : model graph has less rows (",Sj2max); NSLoutput(") than required (",j2max); NSLoutput(") !!\n"); } NSLoutput(" \t(i = ",Si2offset); NSLoutput(",...,",Si2offset+i2max-1); NSLoutput("; j = ",Sj2offset); NSLoutput(",...,",Sj2offset+j2max-1); NSLoutput(")"); // ---- read trafo parameters of model graph and check them DlmGraph.read(kLevelsTmp); DlmGraph.read(kDirectionsTmp); DlmGraph.read(kMaximumTmp); DlmGraph.read(kFactorTmp); DlmGraph.read(sigmaTmp); if (kLevels!=kLevelsTmp){ NSLoutput("\nERROR: kLevels (",kLevelsTmp); NSLoutput(") in model graph != ",kLevels); NSLoutput(" !!\n"); } if (kDirections!=kDirectionsTmp){ NSLoutput("\nERROR: kDirections (",kDirectionsTmp); NSLoutput(") in model graph != ",kDirections); NSLoutput(" !!\n"); } if (kMaximum!=kMaximumTmp){ NSLoutput("\nERROR: kMaximum (",kMaximumTmp); NSLoutput(") in model graph != ",kMaximum); NSLoutput(" !!\n"); } if (kFactor!=kFactorTmp){ NSLoutput("\nERROR: kFactor (",kFactorTmp); NSLoutput(") in model graph != ",kFactor); NSLoutput(" !!\n"); } if (sigma!=sigmaTmp){ NSLoutput("\nERROR: sigma (",sigmaTmp); NSLoutput(") in model graph != ",sigma); NSLoutput(" !!\n"); } // ---- read jets of model graph ; for (Sj2=0, j2=-Sj2offset; Sj2=0; Si2--, i2--) { // check whether the node belongs to the selected subgraph ; char tmpChar; if ((i2<0)||(i2>=i2max)||(j2<0)||(j2>=j2max)) { DlmGraph.read(Norm); DlmGraph.read(Norm); tmpChar=' '; while (tmpChar!='\n') DlmGraph.read(tmpChar); for (k=0; k=0; Si2--) DlmGraph.close_file(); DlmImageModelData[model] = DlmGModel; // ---- compute similarity matrix ; for (i2=0; i20) for (int i2=0; i2