//============================================================================ void AAM_TDM::CalcParams(const CvMat* t, CvMat* lamda) { cvProjectPCA(t, __MeanTexture, __TextureEigenVectors, lamda); }
void FindOneWayDescriptor(cv::flann::Index* m_pca_descriptors_tree, CvSize patch_size, int m_pca_dim_low, int m_pose_count, IplImage* patch, int& desc_idx, int& pose_idx, float& distance, CvMat* avg, CvMat* eigenvectors) { desc_idx = -1; pose_idx = -1; distance = 1e10; //-------- //PCA_coeffs precalculating CvMat* pca_coeffs = cvCreateMat(1, m_pca_dim_low, CV_32FC1); int patch_width = patch_size.width; int patch_height = patch_size.height; //if (avg) //{ CvRect _roi = cvGetImageROI((IplImage*)patch); IplImage* test_img = cvCreateImage(cvSize(patch_width,patch_height), IPL_DEPTH_8U, 1); if(_roi.width != patch_width|| _roi.height != patch_height) { cvResize(patch, test_img); _roi = cvGetImageROI(test_img); } else { cvCopy(patch,test_img); } IplImage* patch_32f = cvCreateImage(cvSize(_roi.width, _roi.height), IPL_DEPTH_32F, 1); float sum = cvSum(test_img).val[0]; cvConvertScale(test_img, patch_32f, 1.0f/sum); //ProjectPCASample(patch_32f, avg, eigenvectors, pca_coeffs); //Projecting PCA CvMat* patch_mat = ConvertImageToMatrix(patch_32f); CvMat* temp = cvCreateMat(1, eigenvectors->cols, CV_32FC1); cvProjectPCA(patch_mat, avg, eigenvectors, temp); CvMat temp1; cvGetSubRect(temp, &temp1, cvRect(0, 0, pca_coeffs->cols, 1)); cvCopy(&temp1, pca_coeffs); cvReleaseMat(&temp); cvReleaseMat(&patch_mat); //End of projecting cvReleaseImage(&patch_32f); cvReleaseImage(&test_img); // } //-------- //float* target = new float[m_pca_dim_low]; //::flann::KNNResultSet res(1,pca_coeffs->data.fl,m_pca_dim_low); //::flann::SearchParams params; //params.checks = -1; //int maxDepth = 1000000; //int neighbors_count = 1; //int* neighborsIdx = new int[neighbors_count]; //float* distances = new float[neighbors_count]; //if (m_pca_descriptors_tree->findNearest(pca_coeffs->data.fl,neighbors_count,maxDepth,neighborsIdx,0,distances) > 0) //{ // desc_idx = neighborsIdx[0] / m_pose_count; // pose_idx = neighborsIdx[0] % m_pose_count; // distance = distances[0]; //} //delete[] neighborsIdx; //delete[] distances; cv::Mat m_object(1, m_pca_dim_low, CV_32F); cv::Mat m_indices(1, 1, CV_32S); cv::Mat m_dists(1, 1, CV_32F); float* object_ptr = m_object.ptr<float>(0); for (int i=0;i<m_pca_dim_low;i++) { object_ptr[i] = pca_coeffs->data.fl[i]; } m_pca_descriptors_tree->knnSearch(m_object, m_indices, m_dists, 1, cv::flann::SearchParams(-1) ); desc_idx = ((int*)(m_indices.ptr<int>(0)))[0] / m_pose_count; pose_idx = ((int*)(m_indices.ptr<int>(0)))[0] % m_pose_count; distance = ((float*)(m_dists.ptr<float>(0)))[0]; // delete[] target; // for(int i = 0; i < desc_count; i++) // { // int _pose_idx = -1; // float _distance = 0; // //#if 0 // descriptors[i].EstimatePose(patch, _pose_idx, _distance); //#else // if (!avg) // { // descriptors[i].EstimatePosePCA(patch, _pose_idx, _distance, avg, eigenvectors); // } // else // { // descriptors[i].EstimatePosePCA(pca_coeffs, _pose_idx, _distance, avg, eigenvectors); // } //#endif // // if(_distance < distance) // { // desc_idx = i; // pose_idx = _pose_idx; // distance = _distance; // } // } cvReleaseMat(&pca_coeffs); }
//** void FindOneWayDescriptor(int desc_count, const CvOneWayDescriptor* descriptors, IplImage* patch, int n, std::vector<int>& desc_idxs, std::vector<int>& pose_idxs, std::vector<float>& distances, CvMat* avg, CvMat* eigenvectors) { for (int i=0;i<n;i++) { desc_idxs[i] = -1; pose_idxs[i] = -1; distances[i] = 1e10; } //-------- //PCA_coeffs precalculating int m_pca_dim_low = descriptors[0].GetPCADimLow(); CvMat* pca_coeffs = cvCreateMat(1, m_pca_dim_low, CV_32FC1); int patch_width = descriptors[0].GetPatchSize().width; int patch_height = descriptors[0].GetPatchSize().height; if (avg) { CvRect _roi = cvGetImageROI((IplImage*)patch); IplImage* test_img = cvCreateImage(cvSize(patch_width,patch_height), IPL_DEPTH_8U, 1); if(_roi.width != patch_width|| _roi.height != patch_height) { cvResize(patch, test_img); _roi = cvGetImageROI(test_img); } else { cvCopy(patch,test_img); } IplImage* patch_32f = cvCreateImage(cvSize(_roi.width, _roi.height), IPL_DEPTH_32F, 1); float sum = cvSum(test_img).val[0]; cvConvertScale(test_img, patch_32f, 1.0f/sum); //ProjectPCASample(patch_32f, avg, eigenvectors, pca_coeffs); //Projecting PCA CvMat* patch_mat = ConvertImageToMatrix(patch_32f); CvMat* temp = cvCreateMat(1, eigenvectors->cols, CV_32FC1); cvProjectPCA(patch_mat, avg, eigenvectors, temp); CvMat temp1; cvGetSubRect(temp, &temp1, cvRect(0, 0, pca_coeffs->cols, 1)); cvCopy(&temp1, pca_coeffs); cvReleaseMat(&temp); cvReleaseMat(&patch_mat); //End of projecting cvReleaseImage(&patch_32f); cvReleaseImage(&test_img); } //-------- for(int i = 0; i < desc_count; i++) { int _pose_idx = -1; float _distance = 0; #if 0 descriptors[i].EstimatePose(patch, _pose_idx, _distance); #else if (!avg) { descriptors[i].EstimatePosePCA(patch, _pose_idx, _distance, avg, eigenvectors); } else { descriptors[i].EstimatePosePCA(pca_coeffs, _pose_idx, _distance, avg, eigenvectors); } #endif for (int j=0;j<n;j++) { if(_distance < distances[j]) { for (int k=(n-1);k > j;k--) { desc_idxs[k] = desc_idxs[k-1]; pose_idxs[k] = pose_idxs[k-1]; distances[k] = distances[k-1]; } desc_idxs[j] = i; pose_idxs[j] = _pose_idx; distances[j] = _distance; break; } } } cvReleaseMat(&pca_coeffs); }
void CvOneWayDescriptor::GenerateSamplesFast(IplImage* frontal, CvMat* pca_hr_avg, CvMat* pca_hr_eigenvectors, CvOneWayDescriptor* pca_descriptors) { CvRect roi = cvGetImageROI(frontal); if(roi.width != GetInputPatchSize().width || roi.height != GetInputPatchSize().height) { cvResize(frontal, m_train_patch); frontal = m_train_patch; } CvMat* pca_coeffs = cvCreateMat(1, pca_hr_eigenvectors->cols, CV_32FC1); double maxval; cvMinMaxLoc(frontal, 0, &maxval); CvMat* frontal_data = ConvertImageToMatrix(frontal); float sum = cvSum(frontal_data).val[0]; cvConvertScale(frontal_data, frontal_data, 1.0f/sum); cvProjectPCA(frontal_data, pca_hr_avg, pca_hr_eigenvectors, pca_coeffs); for(int i = 0; i < m_pose_count; i++) { cvSetZero(m_samples[i]); for(int j = 0; j < m_pca_dim_high; j++) { float coeff = cvmGet(pca_coeffs, 0, j); IplImage* patch = pca_descriptors[j + 1].GetPatch(i); cvAddWeighted(m_samples[i], 1.0, patch, coeff, 0, m_samples[i]); #if 0 printf("coeff%d = %f\n", j, coeff); IplImage* test = cvCreateImage(cvSize(12, 12), IPL_DEPTH_8U, 1); double maxval; cvMinMaxLoc(patch, 0, &maxval); cvConvertScale(patch, test, 255.0/maxval); cvNamedWindow("1", 1); cvShowImage("1", test); cvWaitKey(0); #endif } cvAdd(pca_descriptors[0].GetPatch(i), m_samples[i], m_samples[i]); float sum = cvSum(m_samples[i]).val[0]; cvConvertScale(m_samples[i], m_samples[i], 1.0/sum); #if 0 IplImage* test = cvCreateImage(cvSize(12, 12), IPL_DEPTH_8U, 1); /* IplImage* temp1 = cvCreateImage(cvSize(12, 12), IPL_DEPTH_32F, 1); eigenvector2image(pca_hr_avg, temp1); IplImage* test = cvCreateImage(cvSize(12, 12), IPL_DEPTH_8U, 1); cvAdd(m_samples[i], temp1, temp1); cvMinMaxLoc(temp1, 0, &maxval); cvConvertScale(temp1, test, 255.0/maxval);*/ cvMinMaxLoc(m_samples[i], 0, &maxval); cvConvertScale(m_samples[i], test, 255.0/maxval); cvNamedWindow("1", 1); cvShowImage("1", frontal); cvNamedWindow("2", 1); cvShowImage("2", test); cvWaitKey(0); #endif } cvReleaseMat(&pca_coeffs); cvReleaseMat(&frontal_data); }