第一篇论文实验中的格式转换代码

从文本(一行一行的数据)转成weka所需的格式

其实就是加上了Head和每行后面的标签而已。

下面是针对两个人脸库JAFFE和CMU的,

因为两个人脸库的现有数据集合的数量不同,

CMU的是 训练集:6种表情*5副  测试集:6种表情*3副

JAFFE的是 训练集:7种表情*20副 测试集:7种表情*10

包含的表情总数也不同,所以单独各自写了两个程序,

一个用来转换Training Set

一个用来转换Testing Set

 

这样就写了4个程序。后来师哥的实验数据,又是另一种数量的,又稍加修改了程序,这些代码大体都一样的,

未达目的,不择手段啊。。。

 

后来用Gabor小波,每副图片又拓展出6=(3个方向)*(2个尺度)副出来。。。。处理的数量激增啊。。

 

首先这个是CMU的训练集(6种表情*5副=30),得到PCA特征后的转换程序

///*******************************
//作者:David Bao
//时间:2012年3月15日
//描述:
//		利用matlab中的PCA程序,将30张图片进行合成一个大矩阵,进行pca操作后,得到一个大矩阵,每一行为每幅图的pca特征
//		6种表情{angry,disgust,fear,sadness,smile,surprise}
//		每种表情5个图片
//		共30副
//		此程序专门针对train集!
//		矩阵大小为:30*200 导入成为arff格式
//功能:将PCA得出的文本数据转成weka所需的arff格式
//********************************/
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void main()
{
	string FileName;
	char name[30];
	fstream InFile;
	ofstream OutFile("C:\\Users\\user\\Desktop\\MyDoc\\ExpressionData\\JAFFE\\DCT\\Up\\DCT-train-Up.arff");
	if(OutFile.is_open())
	{
		OutFile<<"@relation 'FER'"<<endl;
		char *ch = new char;
		for(int i=1;i<=300;i++)
		{
			itoa(i,ch,10);
			OutFile<<"@attribute feature"+string(ch)+" real"<<endl;
		}
		OutFile << "@attribute 'class' {angry,disgust,fear,happy,neutral,sad,surprise}"<<endl;
		OutFile << "@data" << endl;
		FileName = string("C:\\Users\\user\\Desktop\\MyDoc\\ExpressionData\\JAFFE\\DCT\\TXT\\DCT-train-Up.txt");
		cout << FileName.c_str() <<endl;
		InFile.open(FileName.c_str(),ios::in);
		string line;
		int i=1;
		while(getline(InFile,line))
		{
			istringstream stream(line);
			string word;
			while(stream >> word)
			{
				OutFile << word.c_str() << ",";
			}
			//JAFFE Database
			if(i>=1 && i<= 20)
			{
				OutFile << "angry" <<endl;
			}
			if(i>=21 && i<= 40)
			{
				OutFile << "disgust" <<endl;
			}
			if(i>=41 && i<= 60)
			{
				OutFile << "fear" <<endl;
			}
			if(i>=61 && i<= 80)
			{
				OutFile << "happy" <<endl;
			}
			if(i>=81 && i<= 100)
			{
				OutFile << "neutral" <<endl;
			}
			if(i>=101 && i<= 120)
			{
				OutFile << "sad" <<endl;
			}
			if(i>=121 && i<= 140)
			{
				OutFile << "surprise" <<endl;
			}
			//CMU database
			//switch(i)
			//{
			//case 1:
			//case 2:
			//case 3:
			//case 4:
			//case 5:
			//	OutFile << "angry"<<endl;
			//	break;
			//case 6:
			//case 7:
			//case 8:
			//case 9:
			//case 10:
			//	OutFile << "disgust"<<endl;
			//	break;
			//case 11:
			//case 12:
			//case 13:
			//case 14:
			//case 15:
			//	OutFile << "fear"<<endl;
			//	break;
			//case 16:
			//case 17:
			//case 18:
			//case 19:
			//case 20:
			//	OutFile << "sadness"<<endl;
			//	break;
			//case 21:
			//case 22:
			//case 23:
			//case 24:
			//case 25:
			//	OutFile << "smile"<<endl;
			//	break;				
			//case 26:
			//case 27:
			//case 28:
			//case 29:
			//case 30:
			//	OutFile << "surprise"<<endl;
			//	break;
			//}
			i++;
		}
	}
	else
	{
		cout << "打开文件失败哇……" <<endl;
	}
	InFile.close();
	OutFile.close();
	
}


 两个表情库的 PCA特征和DCT特征的训练集

下面是测试集

/*******************************
作者:David Bao
时间:2012年3月15日
描述:使用PCA对图像进行变幻后得到PCA系数,现在是一个大矩阵,每一行是一幅图像的PCA特征
		   6种表情,每种表情3个图片,共108副,此程序专门针对test集!
		   矩阵大小为18*200 导入转换成arff文件
********************************/
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#define N 70
using namespace std;
void main()
{
	string FileName;
	char name[30];
	
	fstream InFile;
	ofstream OutFile("C:\\Users\\user\\Desktop\\MyDoc\\ExpressionData\\JAFFE\\DCT\\Whole\\DCT-test-Whole.arff");

	if(OutFile.is_open())
	{
		OutFile<<"@relation 'FER'"<<endl;
		char *ch = new char;
		for(int i=1;i<=300;i++)
		{
			itoa(i,ch,10);
			OutFile<<"@attribute feature"+string(ch)+" real"<<endl;
		}
		OutFile << "@attribute 'class' {angry,disgust,fear,happy,neutral,sad,surprise}"<<endl;
		OutFile << "@data" << endl;
		FileName = string("C:\\Users\\user\\Desktop\\MyDoc\\ExpressionData\\JAFFE\\DCT\\TXT\\DCT-test-Whole.txt");
		cout << FileName.c_str() <<endl;
		InFile.open(FileName.c_str(),ios::in);

		//int counter = 0;
		string line;
		int i=1;
		while(getline(InFile,line))
		{
			istringstream stream(line);
			string word;
			while(stream >> word)
			{
				OutFile << word.c_str() << ",";
			}
			if(i>=1 && i<=10)
			{
				OutFile << "angry"<<endl;
			}
			if(i>=11 && i<=20)
			{
				OutFile << "disgust" <<endl;
			}
			if(i>=21 && i<=30)
			{
				OutFile << "fear" <<endl;
			}
			if(i>=31 && i<=40)
			{
				OutFile << "happy" <<endl;
			}
			if(i>=41 && i<=50)
			{
				OutFile << "neutral" <<endl;
			}
			if(i>=51 && i<=60)
			{
				OutFile << "sad" <<endl;
			}
			if(i>=61 && i<=70)
			{
				OutFile << "surprise" <<endl;
			}
			//CMU database
			//switch(i)
			//{
			//case 1:
			//case 2:
			//case 3:
			//	OutFile << "angry"<<endl;
			//	break;
			//case 4:
			//case 5:
			//case 6:
			//	OutFile << "disgust"<<endl;
			//	break;
			//case 7:
			//case 8:
			//case 9:
			//	OutFile << "fear"<<endl;
			//	break;
			//case 10:
			//case 11:
			//case 12:
			//	OutFile << "sadness"<<endl;
			//	break;
			//case 13:
			//case 14:
			//case 15:
			//	OutFile << "smile"<<endl;
			//	break;				
			//case 16:
			//case 17:
			//case 18:
			//	OutFile << "surprise"<<endl;
			//	break;
			//}
			i++;
		}
		
		InFile.close();
	}
	else
	{
		cout << "打开文件失败哇……" <<endl;
	}
	OutFile.close();
	//system("pause");
}


 

 

 

 

你可能感兴趣的:(String,Stream,user,matlab,Training)