pca_features.cpp
Go to the documentation of this file.
00001 /*
00002  *  pca_features.cpp
00003  *  online_patch
00004  *
00005  *  Created by Victor  Eruhimov on 5/15/09.
00006  *  Copyright 2009 Argus Corp. All rights reserved.
00007  *
00008  */
00009 
00010 #include <cstdio>
00011 #include "outlet_pose_estimation/detail/pca_features.h"
00012 #include <highgui.h>
00013 
00014 using namespace std;
00015 
00016 void savePCAFeatures(const char* filename, CvMat* avg, CvMat* eigenvectors)
00017 {
00018     CvMemStorage* storage = cvCreateMemStorage();
00019     
00020     CvFileStorage* fs = cvOpenFileStorage(filename, storage, CV_STORAGE_WRITE);
00021     cvWrite(fs, "avg", avg);
00022     cvWrite(fs, "eigenvectors", eigenvectors);
00023     cvReleaseFileStorage(&fs);   
00024     
00025     cvReleaseMemStorage(&storage);
00026 }
00027 
00028 void calcPCAFeatures(vector<IplImage*>& patches, const char* filename)
00029 {
00030     int width = patches[0]->width;
00031     int height = patches[0]->height;
00032     int length = width*height;
00033     int patch_count = (int)patches.size();
00034     
00035     CvMat* data = cvCreateMat(patch_count, length, CV_32FC1);
00036     CvMat* avg = cvCreateMat(1, length, CV_32FC1);
00037     CvMat* eigenvalues = cvCreateMat(length, 1, CV_32FC1);
00038     CvMat* eigenvectors = cvCreateMat(length, length, CV_32FC1);
00039     
00040     for(int i = 0; i < patch_count; i++)
00041     {
00042         float sum = cvSum(patches[i]).val[0];
00043         for(int y = 0; y < height; y++)
00044         {
00045             for(int x = 0; x < width; x++)
00046             {
00047                 *((float*)(data->data.ptr + data->step*i) + y*width + x) = (float)(unsigned char)patches[i]->imageData[y*patches[i]->widthStep + x]/sum;
00048             }
00049         }
00050     }
00051     
00052     cvCalcPCA(data, avg, eigenvalues, eigenvectors, CV_PCA_DATA_AS_ROW);
00053     
00054     // save pca data
00055     savePCAFeatures(filename, avg, eigenvectors);
00056     
00057     cvReleaseMat(&data);
00058     cvReleaseMat(&eigenvalues);
00059     cvReleaseMat(&eigenvectors);
00060 }
00061 
00062 
00063 void loadPCAFeatures(const char* path, vector<IplImage*>& patches)
00064 {
00065     const int file_count = 20;
00066     for(int i = 0; i < file_count; i++)
00067     {
00068         char buf[1024];
00069         sprintf(buf, "%s/frame%04d.jpg", path, i);
00070         IplImage* img = loadImageRed(buf);
00071         
00072         vector<feature_t> features;
00073         GetHoleFeatures(img, features);
00074         
00075         for(int j = 0; j < (int)features.size(); j++)
00076         {
00077             const int patch_width = 24;
00078             const int patch_height = 24;
00079             
00080             CvPoint center = features[j].pt;
00081             
00082             CvRect roi = cvRect(center.x - patch_width/2, center.y - patch_height/2, patch_width, patch_height);
00083             cvSetImageROI(img, roi);
00084             roi = cvGetImageROI(img);
00085             if(roi.width != patch_width || roi.height != patch_height)
00086             {
00087                 continue;
00088             }
00089             
00090             IplImage* patch = cvCreateImage(cvSize(patch_width, patch_height), IPL_DEPTH_8U, 1);
00091             cvCopy(img, patch);
00092             patches.push_back(patch);
00093             cvResetImageROI(img);
00094             
00095         }
00096         
00097         printf("Completed file %d, extracted %d features\n", i, (int)features.size());
00098         
00099         cvReleaseImage(&img);
00100     }
00101 }


outlet_pose_estimation
Author(s): Patrick Mihelich
autogenerated on Thu Nov 28 2013 11:46:23