Point<Dim> KDTree<Dim>::findNearestNeighbor(const Point<Dim> & query, int lo, int hi, int dim) const{ if(lo >= hi ) return points[lo]; //base case int mid = (hi+lo)/2; //calculate the minPoint index Point<Dim> temp; Point<Dim> curr = points[mid]; dim = dim%Dim; int direction=0; if(smallerDimVal(query, points[mid], dim)){ direction = 0; temp = findNearestNeighbor(query, lo, mid-1, dim+1); //left half } else{ direction =1; temp = findNearestNeighbor(query, mid+1, hi, dim+1); //recusively call the function on the left half } Point<Dim> best; if(shouldReplace(query, temp, curr) ) best= curr; else best = temp; int dimD = (curr[dim] - query[dim])*(curr[dim] - query[dim]); int bestCurr = squareDistance(best, query); if(dimD > bestCurr) return best; else{ Point<Dim> t; if(direction == 0) t = findNearestNeighbor(query, mid+1, hi, dim+1); else t = findNearestNeighbor(query, lo, mid-1,dim+1); if(shouldReplace(query,t, best)) return best; else return t; } }
std::string EigenfaceRecognizer::recognize(cv::Mat &face) { //int i, nTestFaces = 0; // the number of test images //CvMat * trainPersonNumMat = 0; // the person numbers during training //float * projectedTestFace = 0; IplImage faceToRecognize = (IplImage) face; //IplImage *faceToRecognize = cvLoadImage("s1/1.pgm", CV_LOAD_IMAGE_GRAYSCALE); float *projectedTestImage = (float *)cvAlloc(sizeof(float) * numEigenvalues); //project the test image onto the PCA subspace cvEigenDecomposite( &faceToRecognize, numEigenvalues, eigenVectors, 0, 0, averageImage, projectedTestImage); int nearest = findNearestNeighbor(projectedTestImage); //take threshold into account, mark as not recognized if not found! return recognitionDescriptor[nearest].second; }
//识别阶段代码 void recognize() { int i, nTestFaces = 0; // 测试人脸数 CvMat * trainPersonNumMat = 0; // 训练阶段的人脸数 float * projectedTestFace = 0; // 加载测试图像,并返回测试人脸数 nTestFaces = loadFaceImgArray("test.txt"); printf("%d test faces loaded\n", nTestFaces); // 加载保存在.xml文件中的训练结果 if( !loadTrainingData( &trainPersonNumMat ) ) return; // projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) ); for(i=0; i<nTestFaces; i++) { int iNearest, nearest, truth; //将测试图像投影到子空间中 cvEigenDecomposite( faceImgArr[i], nEigens, eigenVectArr, 0, 0, pAvgTrainImg, projectedTestFace); iNearest = findNearestNeighbor(projectedTestFace); truth = personNumTruthMat->data.i[i]; nearest = trainPersonNumMat->data.i[iNearest]; printf("nearest = %d, Truth = %d\n", nearest, truth); } }
void recognize() { int i, nTestFaces = 0; // the number of test images CvMat * trainPersonNumMat = 0; // the person numbers during training float * projectedTestFace = 0; // load test images and ground truth for person number nTestFaces = loadFaceImgArray("test.txt"); printf("%d test faces loaded\n", nTestFaces); // load the saved training data if( !loadTrainingData( &trainPersonNumMat ) ) return; // project the test images onto the PCA subspace projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) ); for(i=0; i<nTestFaces; i++) { int iNearest, nearest, truth; //project the test image onto the PCA subspace cvEigenDecomposite( faceImgArr[i], nEigens, eigenVectArr, 0, 0, pAvgTrainImg, projectedTestFace); iNearest = findNearestNeighbor(projectedTestFace); truth = personNumTruthMat->data.i[i]; nearest = trainPersonNumMat->data.i[iNearest]; printf("nearest = %d, Truth = %d\n", nearest, truth); } }
/** * 顔を認識して、該当する人のIDを返す */ int EigenFace::recognize(IplImage* testFace) { // 事前加工 IplImage* resizedFaceImage = preProcess(testFace); float * projectedTestFace = 0; // project the test images onto the PCA subspace projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) ); int iNearest, nearest; // project the test image onto the PCA subspace cvEigenDecomposite( resizedFaceImage, nEigens, eigenVectArr, 0, 0, pAvgTrainImg, projectedTestFace); iNearest = findNearestNeighbor(projectedTestFace); nearest = trainPersonNumMat->data.i[iNearest]; cvReleaseImage(&resizedFaceImage); return nearest; }
////////////////////////////////// // recognize() //返回本次训练通过的图片数 int faceRecognition::recognize() { int i, nTestFaces = 0; // the number of test images CvMat * trainPersonNumMat = 0; // the person numbers during training float * projectedTestFace = 0; // load test images and ground truth for person number nTestFaces = loadFaceImgArray(testFileListPath); // load the saved training data if( !loadTrainingData( &trainPersonNumMat ) ) return 0; // project the test images onto the PCA subspace projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) ); //处理输出结果,记录一次训练中正确匹配的图片数 int count=0; for(i=0; i<nTestFaces; i++) { int iNearest, nearest, truth; // project the test image onto the PCA subspace cvEigenDecomposite( faceImgArr[i], nEigens, eigenVectArr, 0, 0, pAvgTrainImg, projectedTestFace); iNearest = findNearestNeighbor(projectedTestFace); truth = personNumTruthMat->data.i[i]; nearest = trainPersonNumMat->data.i[iNearest]; // AfxMessageBox("nearest = %d, Truth = %d", nearest, truth); if(nearest==truth) count++; }//for return count; }
Point<Dim> KDTree<Dim>::findNearestNeighbor(const Point<Dim> & query) const { return findNearestNeighbor(query, 0, points.size()-1, 0); /** * @todo Implement this function! */ }
int main(int argc, char* argv[]) { if(argc < 3) { std::cerr << "Expected two command line arguments" << std::endl; } // The first command line argument is the name of the file, // the second is the prototype file and the third is the unknown file char* prototype_file = argv[1]; char* unknown_file = argv[2]; std::vector<std::vector<double>> prototypes = parsePrototypes(prototype_file); findNearestNeighbor(prototypes, unknown_file); return 0; }
//get intersection points void get_intr_points(MyPointCloud& source_mpc, MyPointCloud& sample_mpc, float search_r, int* intr_points_num){ *intr_points_num=0; PointCloudPtr source_cloud(new PointCloud); MyPointCloud2PointCloud(source_mpc, source_cloud); PointCloudPtr sample_cloud(new PointCloud); MyPointCloud2PointCloud(sample_mpc, sample_cloud); PointCloudPtr intr_cloud(new PointCloud); float resolution = 0.005f; pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> octree (resolution); octree.setInputCloud (source_cloud); octree.addPointsFromInputCloud (); for(int i=0;i<sample_mpc.mypoints.size();i++){ // Neighbors within radius search std::vector<int> pointIdxRadiusSearch; std::vector<float> pointRadiusSquaredDistance; float radius = search_r; if (octree.radiusSearch(sample_cloud->at(i), radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0) { if(pointIdxRadiusSearch.size()>0){ PointCloudPtr cloud_tem(new PointCloud); for (size_t j = 0; j < pointIdxRadiusSearch.size (); ++j){ cloud_tem->push_back(source_cloud->points[ pointIdxRadiusSearch[j]]); } Point finded_pt; if(findNearestNeighbor(cloud_tem, intr_cloud, sample_cloud->at(i), finded_pt)){ intr_cloud->push_back(finded_pt); (*intr_points_num)+=1; } } } } }
// Recognize the face in each of the test images given, and compare the results with the truth. void recognizeFileList(char *szFileTest) { int i, nTestFaces = 0; // the number of test images CvMat * trainPersonNumMat = 0; // the person numbers during training float * projectedTestFace = 0; char *answer; int nCorrect = 0; int nWrong = 0; double timeFaceRecognizeStart; double tallyFaceRecognizeTime; float confidence; // load test images and ground truth for person number nTestFaces = loadFaceImgArray(szFileTest); printf("%d test faces loaded\n", nTestFaces); // load the saved training data if( !loadTrainingData( &trainPersonNumMat ) ) return; // project the test images onto the PCA subspace projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) ); timeFaceRecognizeStart = (double)cvGetTickCount(); // Record the timing. for(i=0; i<nTestFaces; i++) { int iNearest, nearest, truth; // project the test image onto the PCA subspace cvEigenDecomposite( faceImgArr[i], nEigens, eigenVectArr, 0, 0, pAvgTrainImg, projectedTestFace); iNearest = findNearestNeighbor(projectedTestFace, &confidence); truth = personNumTruthMat->data.i[i]; if(iNearest!=-1) { nearest = trainPersonNumMat->data.i[iNearest]; } else { nearest = 0; } if (nearest == truth) { answer = "Correct"; nCorrect++; } else { answer = "WRONG!"; nWrong++; } printf("nearest = %d, Truth = %d (%s). Confidence = %f\n", nearest, truth, answer, confidence); } tallyFaceRecognizeTime = (double)cvGetTickCount() - timeFaceRecognizeStart; if (nCorrect+nWrong > 0) { printf("TOTAL ACCURACY: %d%% out of %d tests.\n", nCorrect * 100/(nCorrect+nWrong), (nCorrect+nWrong)); printf("TOTAL TIME: %.1fms average.\n", tallyFaceRecognizeTime/((double)cvGetTickFrequency() * 1000.0 * (nCorrect+nWrong) ) ); } }