您的位置:首页 > 其它

经过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提供数据。在进行分类识别。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐