void GeneralizedProcrustes::computeGPAlignment(vector < Shape >& outShapes) { int i, counter; double oldEnergy, newEnergy; if(shapes.size() == 0 || numberOfShapes < 1) return; centerAllShapes(); printf("Performing alignment\n"); if(partialAverages == NULL) { partialAverages = new double * [numberOfShapes]; for(i = 0; i < numberOfShapes; i++) partialAverages[i] = new double[3*minNumNodes]; } oldEnergy = newEnergy = -1; counter = 0; do { computePartialAverages(); // printf("Antes %f \n",shapes[0].vertexCoordinates[0][0]); for(i = 0; i < numberOfShapes; i++) { computeRotationAlignment(shapes[i] , partialAverages[i]); outShapes[i]=shapes[i]; } oldEnergy = newEnergy; newEnergy = evaluateEnergy(); counter++; }while((oldEnergy == -1) || (fabs(newEnergy-oldEnergy) > ROTATE_THRESHOLD) && (counter < MAX_WHILE)); }
void SecondOrderOptimizeFusionMove::optimize(Depth &result, const int max_iter) const { vector<Depth> proposals; genProposal(proposals); //proposals.push_back(noisyDisp); char buffer[1024] = {}; //initialize by random result.initialize(width, height, -1); const int nLabel = model->nLabel; std::default_random_engine generator; std::uniform_int_distribution<int> distribution(0, nLabel - 1); for (auto i = 0; i < width * height; ++i) { //result.setDepthAtInd(i, (double) distribution(generator)); //result.setDepthAtInd(i, noisyDisp[i]); result[i] = 0; } sprintf(buffer, "%s/temp/init_result.jpg", file_io.getDirectory().c_str()); result.saveImage(buffer, 256.0 / (double)nLabel); list<double> diffE; double lastEnergy = evaluateEnergy(result); double initialEnergy = lastEnergy; int iter = 0; const double termination = 0.1; float timming = (float) getTickCount(); const int smoothInterval = 5; while (true) { if (iter == max_iter) break; cout << "======================" << endl; Depth newProposal; // if (iter > 0 && iter % smoothInterval == 0) { // Depth orip1; // newProposal.initialize(width, height, -1); // orip1.initialize(width, height, -1); // for (auto i = 0; i < width * height; ++i) { // orip1.setDepthAtInd(i, result[i]); // newProposal.setDepthAtInd(i, result[i]); // } // int direction = iter / smoothInterval; // if (direction % 2 == 0) { // //horizontally // for (auto y = 0; y < height; ++y) { // for (auto x = 1; x < width - 1; ++x) // newProposal.setDepthAtInt(x, y, (orip1(x + 1, y) + orip1(x - 1, y)) / 2); // newProposal.setDepthAtInt(width - 1, y, orip1(width - 1, y)); // } // } else { // //vertically // for (auto x = 0; x < width; ++x) { // for (auto y = 1; y < height - 1; ++y) // newProposal.setDepthAtInt(x, y, (orip1(x, y + 1) + orip1(x, y - 1)) / 2); // newProposal.setDepthAtInt(x, height - 1, orip1(x, height - 1)); // } // } // cout << "Iteration " << iter << " using smoothing proposal " << endl; // } else { // } newProposal = proposals[iter % (proposals.size())]; printf("Fusing with proposal %d\n", (int)(iter % proposals.size())); //after several iteration, smooth the dispartiy fusionMove(result, newProposal); double e = evaluateEnergy(result); double energyDiff = lastEnergy - e; if (diffE.size() >= average_over) diffE.pop_front(); diffE.push_back(energyDiff); double average_diffe = std::accumulate(diffE.begin(), diffE.end(), 0.0) / (double) diffE.size(); printf("Done. Final energy: %.5f, energy decrease: %.5f average decrease: %.5f\n", e, energyDiff, average_diffe); lastEnergy = e; sprintf(buffer, "%s/temp/fusionmove_iter%05d.jpg", file_io.getDirectory().c_str(), iter); result.saveImage(buffer, 256.0 / (double) nLabel); if (iter > proposals.size() * 2 && average_diffe < termination) { cout << "Converge!" << endl; break; } iter++; } timming = ((float) getTickCount() - timming) / (float) getTickFrequency(); printf("All done. Initial energy: %.5f, final energy: %.5f, time usage: %.2fs\n", initialEnergy, lastEnergy, timming); }