Hi there,
Im using OpenCV 2.4.12 with Visual Studio 2012. I am trying to classify facial age into three classes. i have extracted the features using PCA in image form. How do i insert the features into knn algorithm?
I do not understand these lines. What are these files for?
FILE *fp2 = fopen("C:\\Users\\Dorothea\\Desktop\\train-labels-idx1-ubyte.gz","rb");
FILE *fp2 = fopen("C:\\Users\\Dorothea\\Desktop\\train-images.idx1-ubyte","rb");
Below is my knn code.
..........
using namespace cv;
int readFlippedInteger(FILE *fp)
{
int ret = 0;
BYTE *temp;
temp = (BYTE*)(&ret);
fread(&temp[3], sizeof(BYTE), 1, fp);
fread(&temp[2], sizeof(BYTE), 1, fp);
fread(&temp[1], sizeof(BYTE), 1, fp);
fread(&temp[0], sizeof(BYTE), 1, fp);
return ret;
}
int main()
{
FILE *fp2 = fopen("C:\\Users\\Dorothea\\Desktop\\train-labels-idx1-ubyte.gz","rb");
FILE *fp2 = fopen("C:\\Users\\Dorothea\\Desktop\\train-images.idx1-ubyte","rb");
if(!fp||!fp2)
int magicNumber = readFlippedInteger(fp);
int numImages = readFlippedInteger(fp);
int numRows = readFlippedInteger(fp);
int numCols = readFlippedInteger(fp);
fseek(fp2, 0x88, SEEK_SET);
int size = numRows*numCols;
CvMat *trainingVectors = cvCreateMat(numImages, size, CV_32FC1);
CvMat *trainingLabels = cvCreateMat(numImages, 1, CV_32FC1);
BYTE *temp = new BYTE[size];
BYTE tempClass=0;
for(int=0;idata.fl[i]= tempClass;
for(int k=0; kdata.fl[i*size+k]= temp[k];
}
KNearest knn(trainingVectors, trainingLabels);
printf("Maximum k: %d", knn.get_max_k());
fclose(fp);
fclose(fp2);
cvReleaseMat(&trainingVectors);
cvReleaseMat(&trainingLabels);
//////////////////// Testing //////////////////////////
fp= fopen("C:\\Users\\Dorothea\\Desktop\\t10k-images.idx3-ubyte", "rb");
fp= fopen("C:\\Users\\Dorothea\\Desktop\\t10k-labels.idx1-ubyte", "rb");
magicNumber = readFlippedInteger(fp);
numImages = readFlippedInteger(fp);
numRows = readFlippedInteger(fp);
numCols = readFlippedInteger(fp);
fseek(fp2, 0x08, SEEK_SET);
CvMat *testVectors = cvCreateMat(numImages, size, CV_32FC1);
CvMat *testLabels = cvCreateMat(numImages, 1, CV_32FC1);
CvMat *actualLabels = cvCreateMat(numImages, 1, CV_32FC1);
temp= new BYTE[size];
tempClass=1;
CvMat *currentTest= cvCreateMat(1, size, CV_32FC1);
CvMat *currentLabel = cvCreateMat(1, 1, CV_32FC1);
int totalCorrect=0;
for(int i=0; idata.fl[i]= (float)tempClass;
for(int k=0; kdata.fl[i*size+k]= temp[k];
currentTest->data.fl[k]= temp[k];
}
knn.find_nearest(currentTest, 5, currentLabel);
testLabels->data.fl[i] = currentLabel->data.fl[0];
if(currentLabel->data.fl[0]==actualLabels->data.fl[i])
totalCorrect++;
}
printf("Time: %d Accuracy: %f ", (int)time, (double)totalCorrect*100/(double)numImages);
return 0;
}
Thank you in advance
↧