save.h
资源名称:gabor.rar [点击查看]
上传用户:soukeisyuu
上传日期:2022-07-03
资源大小:5943k
文件大小:8k
源码类别:
波变换
开发平台:
Visual C++
- #ifndef SAVE_H
- #define SAVE_H
- int main( int argc, char** argv )
- {
- CvMat* train_m = cvCreateMat(120,10,CV_32FC1);
- CvMat* response_m = cvCreateMat(120,1,CV_32FC1);
- CvMat* test_m = cvCreateMat(30,10,CV_32FC1);
- CvMat* result_m = cvCreateMat(30,1,CV_32FC1);
- FILE* fp;
- fp = fopen("../aaa.txt","r");
- if(fp == NULL)
- return -1;
- int train_step = train_m->step/sizeof(float);
- float* train_data = train_m->data.fl;
- int test_step = test_m->step/sizeof(float);
- float* test_data = test_m->data.fl;
- int response_step = response_m->step/sizeof(float);
- float* response_data = response_m->data.fl;
- int result_step = result_m->step/sizeof(float);
- float* result_data = result_m->data.fl;
- float lineData[12];
- for(int k=0;k<3;k++)
- {
- for(int i=0;i<50;i++)
- {
- for(int t=0;t<12;t++)
- fscanf(fp,"%f",&lineData[t]);
- for(int j=0;j<=10;j++)
- if(i<40)
- {
- train_data[(40*k+i)*train_step + j] = lineData[j+2];
- response_data[(40*k+i)*response_step] = k;
- }
- else
- {
- test_data[(10*k+(i-40))*test_step + j] = lineData[j+2];
- }
- }
- }
- /*
- printf("training datan");
- for(int i=0;i<120;i++)
- {
- for(int j=0;j<10;j++)
- printf("%.3f_",train_data[i*train_step+j]);
- printf("n");
- }
- printf("n");
- printf("testing datan");
- for(int i=0;i<30;i++)
- {
- for(int j=0;j<10;j++)
- printf("%.3f_",test_data[i*test_step+j]);
- printf("n");
- }
- printf("n");
- */
- CvNormalBayesClassifier bayesClassifier(train_m,response_m);
- CvMat* rowMat = cvCreateMat(1,10,CV_32FC1);
- float category;
- int right=0;
- printf("resulting datan");
- for(int k=0;k<3;k++)
- {
- for(int i=0;i<10;i++)
- {
- cvGetRow(test_m,rowMat,k*10+i);
- category = bayesClassifier.predict(rowMat,0);
- if( (int)category == k )
- right++;
- printf("%.3fn",category);
- }
- }
- printf("n%f",right/30.0*100);
- /*
- bayesClassifier.predict(test_m,result_m);
- printf("resulting datan");
- for(int i=0;i<30;i++)
- printf("%.3fn",result_data[i*result_step]);
- printf("n");
- int right=0;
- for(int k=0;k<3;k++)
- {
- for(int i=0;i<10;i++)
- {
- if( (int)result_data[(10*k+i)*result_step] == k )
- right++;
- }
- }
- printf("%f",(float)right/30*100);
- */
- fclose(fp);
- while(1);
- return 0;
- }
- void meanDevi(CvMat *arr, CvMat *mean_devi)
- {
- if(arr->cols != mean_devi->cols)
- return;
- int i,row,col;
- CvMat *temp_col;
- CvScalar mean = cvScalar(0.0, 0.0, 0.0, 0.0);
- CvScalar devi = cvScalar(0.0, 0.0, 0.0, 0.0);
- row = arr->rows;
- col = arr->cols;
- temp_col = cvCreateMat(row,1,CV_32FC1);
- float *data = (float*)mean_devi->data.fl;
- int step = mean_devi->step/sizeof(float);
- for(i=0;i<col;i++)
- {
- cvGetCol(arr,temp_col,i);
- cvAvgSdv(temp_col,&mean,&devi,NULL);
- data[i] = mean.val[0];
- data[step+i] = devi.val[0];
- }
- }
- void dataNormalize(CvMat *arr, CvMat *mean_devi)
- {
- if(arr->cols != mean_devi->cols)
- return;
- int i,j,row,col;
- row = arr->rows;
- col = arr->cols;
- float *arr_data = (float*)arr->data.fl;
- int arr_step = arr->step/sizeof(float);
- float *data = (float*)mean_devi->data.fl;
- int step = mean_devi->step/sizeof(float);
- for(i=0;i<col;i++)
- {
- float mean = data[i];
- float devi = data[step+i];
- for(j=0;j<row;j++)
- arr_data[j*arr_step+i] = (arr_data[j*arr_step+i] - mean)/devi;
- }
- }
- void minMax(CvMat *arr, CvMat *min_max)
- {
- if(arr->cols != min_max->cols)
- return;
- int i,row,col;
- CvMat *temp_col;
- double min;
- double max;
- row = arr->rows;
- col = arr->cols;
- temp_col = cvCreateMat(row,1,CV_32FC1);
- float *data = (float*)min_max->data.fl;
- int step = min_max->step/sizeof(float);
- for(i=0;i<col;i++)
- {
- cvGetCol(arr,temp_col,i);
- cvMinMaxLoc(temp_col,&min,&max);
- data[i] = (float)min;
- data[step+i] = (float)max;
- }
- }
- void dataLinear(CvMat *arr, CvMat *min_max)
- {
- if(arr->cols != min_max->cols)
- return;
- int i,j,row,col;
- row = arr->rows;
- col = arr->cols;
- float *arr_data = (float*)arr->data.fl;
- int arr_step = arr->step/sizeof(float);
- float *data = (float*)min_max->data.fl;
- int step = min_max->step/sizeof(float);
- for(i=0;i<col;i++)
- {
- float min = data[i];
- float max = data[step+i];
- for(j=0;j<row;j++)
- arr_data[j*arr_step+i] = (arr_data[j*arr_step+i] - min)/(max-min);
- }
- }
- int main( int argc, char** argv )
- {
- CvMat* train_m = cvCreateMat(120,10,CV_32FC1);
- CvMat* response_m = cvCreateMat(120,1,CV_32FC1);
- CvMat* test_m = cvCreateMat(30,10,CV_32FC1);
- CvMat* result_m = cvCreateMat(30,1,CV_32FC1);
- CvMat* mean_devi = cvCreateMat(2,10,CV_32FC1);
- CvMat* min_max = cvCreateMat(2,10,CV_32FC1);
- FILE* fp;
- fp = fopen("../aaa.txt","r");
- if(fp == NULL)
- return -1;
- int train_step = train_m->step/sizeof(float);
- float* train_data = train_m->data.fl;
- int test_step = test_m->step/sizeof(float);
- float* test_data = test_m->data.fl;
- int response_step = response_m->step/sizeof(float);
- float* response_data = response_m->data.fl;
- int result_step = result_m->step/sizeof(float);
- float* result_data = result_m->data.fl;
- float lineData[12];
- for(int k=0;k<3;k++)
- {
- for(int i=0;i<50;i++)
- {
- for(int t=0;t<12;t++)
- fscanf(fp,"%f",&lineData[t]);
- for(int j=0;j<=10;j++)
- if(i<40)
- {
- train_data[(40*k+i)*train_step + j] = lineData[j+2];
- response_data[(40*k+i)*response_step] = k;
- }
- else
- {
- test_data[(10*k+(i-40))*test_step + j] = lineData[j+2];
- }
- }
- }
- meanDevi(train_m,mean_devi);
- dataNormalize(train_m,mean_devi);
- dataNormalize(test_m,mean_devi);
- //minMax(train_m,min_max);
- //dataLinear(train_m,min_max);
- //dataLinear(test_m,min_max);
- printf("training datan");
- for(int i=0;i<120;i++)
- {
- for(int j=0;j<10;j++)
- printf("%.3f_",train_data[i*train_step+j]);
- printf("n");
- }
- printf("n");
- printf("testing datan");
- for(int i=0;i<30;i++)
- {
- for(int j=0;j<10;j++)
- printf("%.3f_",test_data[i*test_step+j]);
- printf("n");
- }
- printf("n");
- CvTermCriteria criteria;
- CvSVMParams param;
- criteria= cvTermCriteria(CV_TERMCRIT_EPS, 0, FLT_EPSILON);
- param= CvSVMParams(CvSVM::C_SVC, CvSVM::POLY,
- 1.0, 1.0, 1.0,
- 2, 0.0, 0.0,
- NULL, criteria);
- CvSVM svmClassifier = CvSVM();
- svmClassifier.train(train_m,response_m,NULL,NULL,param);
- CvMat* rowMat = cvCreateMat(1,10,CV_32FC1);
- float category;
- int right=0;
- printf("resulting datan");
- for(int k=0;k<3;k++)
- {
- for(int i=0;i<10;i++)
- {
- cvGetRow(test_m,rowMat,k*10+i);
- category = svmClassifier.predict(rowMat);
- if( (int)category == k )
- right++;
- printf("%.3fn",category);
- }
- }
- printf("n%f",right/30.0*100);
- fclose(fp);
- while(1);
- return 0;
- }
- #endif // SAVE_H