经过Gabor后的图像,经由这个程序进行DCT的降维处理并保存成arff文件--2012-12-20
2012-12-20 15:30
791 查看
/* cv200d.lib cvaux200d.lib cxcore200d.lib cxts200d.lib highgui200d.lib ml200d.lib */ #include <iostream> #include <vector> #include <cv.h> #include <highgui.h> #include <fstream> #include <string> using namespace std; //zigzag变换,只在把矩阵变成一个向量。 void ZigZag(double *DCTMatrix,int width,int height,double *zigzagVector) { int h = 1,v = 1; int vmin = 1,hmin = 1; int vmax = height, hmax = width; //vertical, horizontal int i; memset(zigzagVector, 0, sizeof(float) * height * width ); i=1; while(v<=vmax && h<=hmax) { if((h+v)%2==0) { if(v==vmin) { zigzagVector[i-1]=*(DCTMatrix + width*(v-1) + h-1); if(h==hmax) { v=v+1; } else { h=h+1; } i=i+1; } else if((h==hmax) && (v<vmax)) { zigzagVector[i-1]=*(DCTMatrix + width*(v-1) + h-1); v=v+1; i=i+1; } else if((v>vmin) && (h<hmax)) { zigzagVector[i-1]=*(DCTMatrix + width*(v-1) + h-1); v=v-1; h=h+1; i=i+1; } } else { if((v==vmax) && (h<=hmax)) { zigzagVector[i-1]=*(DCTMatrix + width*(v-1) + h-1); h=h+1; i=i+1; } else if(h==hmin) { zigzagVector[i-1]=*(DCTMatrix + width*(v-1) + h-1); if(v==vmax) h=h+1; else v=v+1; i=i+1; } else if((v<vmax) && (h>hmin)) { zigzagVector[i-1]=*(DCTMatrix + width*(v-1) + h-1); v=v+1; h=h-1; i=i+1; } } if((v==vmax) && (h==hmax)) { zigzagVector[i-1]=*(DCTMatrix + width*(v-1) + h-1); break; } } } vector<double> DCTtransform(IplImage* image) { //CvMat* OldMat=cvCreateMat(image->height,image->width,CV_32FC1); //原矩阵 cout << image->nChannels <<endl; IplImage *r = cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1); IplImage *g = cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1); IplImage *b = cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1); cvSplit(image,r,g,b,NULL); IplImage* s = cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1); cvAddWeighted(r,1./3.,g,1./3.,0.0,s); cvAddWeighted(s,2./3.,b,1./3.,0.0,s); CvMat* Mat=cvCreateMat(s->height,s->width,CV_64FC1);//因为特征信息是以列向量存储的,所以要将矩阵进行转置,这个矩阵为转置之后的矩阵 CvMat* DCT=cvCreateMat(s->height,s->width,CV_64FC1);//DCT变换后的矩阵,用DCT变换要保证图像的高和宽都要为偶数 cvConvert(s, Mat);//将源图像内容赋值给矩阵(Mat)cvSacle //cvTranspose(image,Mat);//转置操作 cvDCT(Mat,DCT, CV_DXT_FORWARD);//对矩阵进行DCT变化,结果在矩阵DCT中 //将DCT变换的矩阵结果通过ZigZag算法,取其前100位,实现DCT降维 unsigned int width = DCT->cols; unsigned int height =DCT->rows; double * orig = new double[height*width]; int i1 = 0; for( int y=0;y<DCT->rows;++y)//取矩阵值 { for(int x=0;x<DCT->cols;++x) { *(orig + i1) = cvmGet(DCT,y,x); i1++; } } double * zigzagVector = new double[width*height]; memset(zigzagVector , 0 ,sizeof(float) * width*height); ZigZag(orig,width,height,zigzagVector); //ZigZag变换 vector<double> vec; //ZigZag之后的完整向量 vector<double> vec1; //截取前100位的向量 for(unsigned int i = 0; i < height; ++i) { for(unsigned int j = 0;j < width;++j) { if(vec.size()<=200) { vec.push_back(*(zigzagVector + i * height +j)); } else vec.push_back(0); } } for(vector<double>::size_type i = 0; i <200; ++i) { vec1.push_back(vec[i]); } cvReleaseMat(&DCT); //释放内存空间 cvReleaseMat(&Mat); delete orig; delete zigzagVector; return vec1; } void main() { char num[3]; vector<vector<double>> AllVector; vector<double> v; for(int i=1;i<=39;i++)//图片个数 { itoa(i,num,10); string FileName=string("C:\\Users\\user\\Desktop\\mouth\\Gabor\\angry\\rename\\angry-")+num;//图片的存储路径和命名习惯 FileName = FileName + string(".png"); cout << FileName <<endl; IplImage *p =cvLoadImage((char *)FileName.c_str()); v = DCTtransform(p); AllVector.push_back(v); } //输出到文件 ofstream OutFile("C:\\Users\\user\\Desktop\\mouth\\Gabor\\angry\\rename\\mouth-angry-200.arff");//arff的保存路径 if(!OutFile.is_open()) { cout << "输出文件,未被打开。。。"<<endl; } OutFile<<"@relation 'FER'"<<endl; char *ch = new char; for(int i=1;i<=200;i++) { itoa(i,ch,10); OutFile<<"@attribute feature"+string(ch)+" real"<<endl; } OutFile << "@attribute 'class' {angry,disgust,fear,happy,sad,surprise}"<<endl; OutFile << "@data" << endl; for(vector<vector<double>>::size_type i=0; i != AllVector.size(); ++i) { for(vector<double>::size_type j=0; j != v.size(); ++j) { OutFile << AllVector.at(i).at(j) << ","; } OutFile <<"angry" << endl;//arff文件中,每一条特征的标识位 } OutFile.close(); }
这是第二篇论文的程序。
以上程序,是将昨晚Gabor变换的图片,进行DCT变换,然后再用zigzag变换提取200维特征,然后格式化成arff文件,为weka提供数据。在进行分类识别。。
相关文章推荐
- 图解使用Win8Api进行Metro风格的程序开发二----使用文件选择器访问和保存文件
- 自定义字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。
- 使用 Video4Linux 采集USB摄像头的图像,并保存到一张 jpg图像文件中的程序
- 利用opengl将3ds文件导入场景并显示,然后保存这个场景为bmp格式图像,其结果与显示不同,问题出在哪儿了
- 在ASP.NET 2.0中,有时候需要对ASP.NET生成的HTML代码进行处理,或者是保存成静态文件。ASP.NET 提供了直接将请求保存成文件的方法:HttpRequest.SaveAs方法。下面这个方法就是在ASP.NET 2.0中得到ASP.NET
- 利用OpenCV进行【视频的读取】与【保存视频文件或者图像】源码!~
- OpenCV入门七:OpenCV打开视频文件,并对其中的每一帧图像进行Canny算子边缘化提取,并将结果保存为视频文件
- "以前进行的程序安装创建了挂起的文件操作。运行程序之前,必须重新起动计算机。"的解决办法
- Linux 动态清空文件后 程序再向这个文件写内容时 文件大小不变 并文件开头多了很多^@符号
- MATLAB双目标定——棋盘格左右图像自动保存MATLAB程序
- 尝试用库文件对程序进行封装
- 黑马程序员 编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中, a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔。
- 以前进行的程序安装创建了挂起的文件操作(SqlServer2000或SqlServer 2000 SP4补丁安装) .
- Android——配置文件的保存SharedPreferences进行数据存储
- Android将程序崩溃信息保存本地文件
- java中split()函数,"."应该为''\\.",注意转义字符! 程序对分割后的文件碎片进行合并。
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- Barcode Professional for ASP.NET使用教程:条码图像保存到数据库或者XML文件
- Android将程序崩溃信息保存本地文件
- 保存UIImage到文件--到本地自己的程序目录