logo资料库

模板匹配算法.doc

第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
资料共9页,剩余部分请下载后查看
算法轮廓 假定数据是从两个 txt 文档(一个存放 X 轴的数据,另一个存放 Y 轴 的数据)中读取。(FILE *Axis_X,*Axis_Y) 1:首先制作模板 多做几次实验,从实验中获取 Vectors_Num个基本数据(特征向量), 制作好模板,便于比对。记录好提取数据的间隔 Delay_Time。 对于本实验来说,需要四个模板(左移(X),右移(X),上移(Y), 下移(Y)),表示如下: /*******Four Patterns To Be Matched With******************* Notes:Only use the left hand ******************************************************* ****/ int LeftShiftX[Vectors_Num] = int LeftShiftY[Vectors_Num] = int RightShiftX[Vectors_Num]= int RightShiftY[Vectors_Num]= int UpShiftX[Vectors_Num] = int UpShiftY[Vectors_Num] = int DownShiftX[Vectors_Num] = int DownShiftY[Vectors_Num] = 2:采集样本数据 如何采集数据也是很重要的。 下面的数组保存获取的数据: int Sample_X[Vectors_Num]={0}; int Sample_Y[Vectors_Num]={0}; 首先设定一个阀值 #define Threshold_Flag 100 /*start flag range*/
为了便于比较,设定两个基值: #define BaseX axis*/ 2100 /*the basic value of X #define BaseY axis*/ 然后开始打开文件,从文件中读取数据 1400 /*the basic value of Y /* open the file for read */ if((Axis_X=fopen("c:\\11.txt","r"))==NULL) { printf("Can not Open File--11.TXT !\n"); exit(1); } else { printf("Open File====11.TXT Successfully!\n"); } if((Axis_Y=fopen("c:\\22.txt","r"))==NULL) { printf("Can not Open File--22.TXT !\n"); exit(1); } else { printf("Open File====22.TXT Successfully!\n"); } 读取数据时候,我们建立了一个数组(两个元素,一个存放 X 轴的数 据,另一个存放 Y 轴的数据)作为临时存放从两个文件中获取的数据。 int current_data[2]={0}; 然后再文件没结束的时候进行读取 while(!feof(Axis_X)) { Flag=0; fscanf(Axis_X,"%d",¤t_data[0]); fscanf(Axis_Y,"%d",¤t_data[1]); 读入到数组的数据与基值的差 的绝对值跟阀值 Threshold_Flag 进 行比较,如果超过阀值,就开始采集样本数据到 Sample数组,为了 便于确定,设定一个 Flag,超过阀值就另 Flag为 1,否则为 0,继 续采集。 if((abs(BaseX-current_data[0])>Threshold_Flag)||(abs(Bas eY-current_data[1])>Threshold_Flag)) 当 Flag为 1 的时候开始采集数据到 Sample数组,采集数据的间隙 Delay_Time跟模板提取时候的一样。 for(i=0;i
delay /* for(j=0;j
*********************************************************/ float Similarity(int Sample[Vectors_Num],int Pattern[Vectors_Num]) { int counter=0; int i; for(i=0;i #include #include /*abs()*/ #define Threshold_Match 0.8 /*match accuracy*/ #define Threshold_Error 50 /*distance error*/ #define Threshold_Flag 100 /*start flag range*/ #define Delay_Time get from experiments*/ #define Vectors_Num vectors */ 2 /*pick vectors interval 10 /*numbersofthetypical #define BaseX axis*/ #define BaseY axis*/ 2100 /*the basic value of X 1400 /*the basic value of Y
/*******Four Patterns To Be Matched With******************* Notes: Only use the left hand ********************************************************** */ int LeftShiftX[Vectors_Num] = {1950,1810,1850,1950,2160,2430,2530,2230,2110,2150}; int LeftShiftY[Vectors_Num] = {1400,1400,1400,1400,1400,1400,1400,1400,1400,1400}; int RightShiftX[Vectors_Num]= {2230,2330,2430,2500,2530,2530,2510,2400,2330,2300}; int RightShiftY[Vectors_Num]= {1400,1400,1400,1400,1400,1400,1400,1400,1400,1400}; int UpShiftX[Vectors_Num] = {2100,2100,2100,2100,2100,2100,2100,2100,2100,2100}; int UpShiftY[Vectors_Num] = {1230,1180,1010,1110,1230,1350,1430,1500,1630,1530}; int DownShiftX[Vectors_Num] = {2100,2100,2100,2100,2100,2100,2100,2100,2100,2100}; int DownShiftY[Vectors_Num] = {1530,1600,1700,1730,1800,1750,1700,1600,1500,1430}; /*****************Function Prototype****************************/ float Similarity(int Sample[Vectors_Num],int Pattern[Vectors_Num]); /****************The Main Function****************************/ void main(void) { int i,j; unsigned int Flag=0; /*indicate the start of matching*/ int current_data[2]={0}; /*initialization and wait for extraction*/ /*************Sample extract from data flow******************** *if Flag =1,start extract data and put current data flow
in the arrays, data extraction's interval depends on delay(Delay_Time)*/ int Sample_X[Vectors_Num]={0}; int Sample_Y[Vectors_Num]={0}; /* two similarity container */ float sim_X; float sim_Y; FILE *Axis_X,*Axis_Y; /*two files contain the data from the acclerator*/ /* open the file for read */ if((Axis_X=fopen("c:\\11.txt","r"))==NULL) { printf("Can not Open File--11.TXT !\n"); exit(1); } else { printf("Open File====11.TXT Successfully!\n"); } if((Axis_Y=fopen("c:\\22.txt","r"))==NULL) { printf("Can not Open File--22.TXT !\n"); exit(1); } else { printf("Open File====22.TXT Successfully!\n"); } //// while(1) { /*extract current data into current_data[]*/ /*if not end of file ,process (all files are sychronous)*/ while(!feof(Axis_X)) { Flag=0; fscanf(Axis_X,"%d",¤t_data[0]); fscanf(Axis_Y,"%d",¤t_data[1]);
/*judge if it is ready to start matching*/ if((abs(BaseX-current_data[0])>Threshold_Flag)||(abs(Ba seY-current_data[1])>Threshold_Flag)) { } Flag=1; /*find the start indicator*/ if(Flag) { /*start pick vectors and put into the sample*/ for(i=0;i=Threshold_Match) && (sim_Y>=Threshold_Match) ) { // printf("%.2f\t%.2f\n",sim_X,sim_Y);
printf("Pattern One ==Left Shift Success!!\n"); continue; } ****/ /***** the second Pattern Matching sim_X= Similarity(Sample_X,RightShiftX); sim_Y= Similarity(Sample_Y,RightShiftY); if( (sim_X>=Threshold_Match) && (sim_Y>=Threshold_Match) ) { // printf("%.2f\t%.2f\n",sim_X,sim_Y); printf("Pattern Two ==Right Shift Success!!\n"); continue; } /*** the third pattern Matching ***/ sim_X= Similarity(Sample_X,UpShiftX); sim_Y= Similarity(Sample_Y,UpShiftY); if( (sim_X>=Threshold_Match) && (sim_Y>=Threshold_Match) ) { // printf("%.2f\t%.2f\n",sim_X,sim_Y); printf("Pattern Three ==Up Shift Success!!\n"); continue; } /*** the fourth pattern Matching ***/ sim_X= Similarity(Sample_X,DownShiftX); sim_Y= Similarity(Sample_Y,DownShiftY); if( (sim_X>=Threshold_Match) && (sim_Y>=Threshold_Match) ) { printf("%.2f\t%.2f\n",sim_X,sim_Y); printf("Pattern Four ==Down Shift Success!!\n");
分享到:
收藏