/// Compute the Root Mean Square Error of the residuals static double RMSE(const SfM_Data & sfm_data) { // Compute residuals for each observation std::vector<double> vec; for(Landmarks::const_iterator iterTracks = sfm_data.GetLandmarks().begin(); iterTracks != sfm_data.GetLandmarks().end(); ++iterTracks) { const Observations & obs = iterTracks->second.obs; for(Observations::const_iterator itObs = obs.begin(); itObs != obs.end(); ++itObs) { const View * view = sfm_data.GetViews().find(itObs->first)->second.get(); const geometry::Pose3 pose = sfm_data.GetPoseOrDie(view); const std::shared_ptr<cameras::IntrinsicBase> intrinsic = sfm_data.GetIntrinsics().at(view->id_intrinsic); const Vec2 residual = intrinsic->residual(pose, iterTracks->second.X, itObs->second.x); //std::cout << residual << " "; vec.push_back( residual(0) ); vec.push_back( residual(1) ); } } const Eigen::Map<Eigen::RowVectorXd> residuals(&vec[0], vec.size()); const double RMSE = std::sqrt(residuals.squaredNorm() / vec.size()); return RMSE; }
void Frustum_Filter::init_z_near_z_far_depth ( const SfM_Data & sfm_data, const double zNear, const double zFar ) { // If z_near & z_far are -1 and structure if not empty, // compute the values for each camera and the structure const bool bComputed_Z = (zNear == -1. && zFar == -1.) && !sfm_data.structure.empty(); if (bComputed_Z) // Compute the near & far planes from the structure and view observations { for (Landmarks::const_iterator itL = sfm_data.GetLandmarks().begin(); itL != sfm_data.GetLandmarks().end(); ++itL) { const Landmark & landmark = itL->second; const Vec3 & X = landmark.X; for (Observations::const_iterator iterO = landmark.obs.begin(); iterO != landmark.obs.end(); ++iterO) { const IndexT id_view = iterO->first; const View * view = sfm_data.GetViews().at(id_view).get(); if (!sfm_data.IsPoseAndIntrinsicDefined(view)) continue; const Pose3 pose = sfm_data.GetPoseOrDie(view); const double z = Depth(pose.rotation(), pose.translation(), X); NearFarPlanesT::iterator itZ = z_near_z_far_perView.find(id_view); if (itZ != z_near_z_far_perView.end()) { if ( z < itZ->second.first) itZ->second.first = z; else if ( z > itZ->second.second) itZ->second.second = z; } else z_near_z_far_perView[id_view] = {z,z}; } } } else { // Init the same near & far limit for all the valid views for (Views::const_iterator it = sfm_data.GetViews().begin(); it != sfm_data.GetViews().end(); ++it) { const View * view = it->second.get(); if (!sfm_data.IsPoseAndIntrinsicDefined(view)) continue; if (z_near_z_far_perView.find(view->id_view) == z_near_z_far_perView.end()) z_near_z_far_perView[view->id_view] = {zNear, zFar}; } } }
/// Build a list of pair that share visibility content from the SfM_Data structure Pair_Set BuildPairsFromStructureObservations(const SfM_Data & sfm_data) { Pair_Set pairs; for (Landmarks::const_iterator itL = sfm_data.GetLandmarks().begin(); itL != sfm_data.GetLandmarks().end(); ++itL) { const Landmark & landmark = itL->second; for (Observations::const_iterator iterI = landmark.obs.begin(); iterI != landmark.obs.end(); ++iterI) { const IndexT id_viewI = iterI->first; Observations::const_iterator iterJ = landmark.obs.begin(); std::advance(iterJ, 1); for (; iterJ != landmark.obs.end(); ++iterJ) { const IndexT id_viewJ = iterJ->first; pairs.insert( std::make_pair(id_viewI,id_viewJ)); } } } return pairs; }
inline bool Generate_SfM_Report ( const SfM_Data & sfm_data, const std::string & htmlFilename ) { // Compute mean,max,median residual values per View IndexT residualCount = 0; Hash_Map< IndexT, std::vector<double> > residuals_per_view; for ( const auto & iterTracks : sfm_data.GetLandmarks() ) { const Observations & obs = iterTracks.second.obs; for ( const auto & itObs : obs ) { const View * view = sfm_data.GetViews().at(itObs.first).get(); const geometry::Pose3 pose = sfm_data.GetPoseOrDie(view); const cameras::IntrinsicBase * intrinsic = sfm_data.GetIntrinsics().at(view->id_intrinsic).get(); // Use absolute values const Vec2 residual = intrinsic->residual(pose, iterTracks.second.X, itObs.second.x).array().abs(); residuals_per_view[itObs.first].push_back(residual(0)); residuals_per_view[itObs.first].push_back(residual(1)); ++residualCount; } } using namespace htmlDocument; // extract directory from htmlFilename const std::string sTableBegin = "<table border=\"1\">", sTableEnd = "</table>", sRowBegin= "<tr>", sRowEnd = "</tr>", sColBegin = "<td>", sColEnd = "</td>", sNewLine = "<br>", sFullLine = "<hr>"; htmlDocument::htmlDocumentStream htmlDocStream("SFM report."); htmlDocStream.pushInfo( htmlDocument::htmlMarkup("h1", std::string("SFM report."))); htmlDocStream.pushInfo(sFullLine); htmlDocStream.pushInfo( "Dataset info:" + sNewLine ); std::ostringstream os; os << " #views: " << sfm_data.GetViews().size() << sNewLine << " #poses: " << sfm_data.GetPoses().size() << sNewLine << " #intrinsics: " << sfm_data.GetIntrinsics().size() << sNewLine << " #tracks: " << sfm_data.GetLandmarks().size() << sNewLine << " #residuals: " << residualCount << sNewLine; htmlDocStream.pushInfo( os.str() ); htmlDocStream.pushInfo( sFullLine ); htmlDocStream.pushInfo( sTableBegin); os.str(""); os << sRowBegin << sColBegin + "IdView" + sColEnd << sColBegin + "Basename" + sColEnd << sColBegin + "#Observations" + sColEnd << sColBegin + "Residuals min" + sColEnd << sColBegin + "Residuals median" + sColEnd << sColBegin + "Residuals mean" + sColEnd << sColBegin + "Residuals max" + sColEnd << sRowEnd; htmlDocStream.pushInfo( os.str() ); for (const auto & iterV : sfm_data.GetViews() ) { const View * v = iterV.second.get(); const IndexT id_view = v->id_view; os.str(""); os << sRowBegin << sColBegin << id_view << sColEnd << sColBegin + stlplus::basename_part(v->s_Img_path) + sColEnd; // IdView | basename | #Observations | residuals min | residual median | residual max if (sfm_data.IsPoseAndIntrinsicDefined(v)) { if( residuals_per_view.find(id_view) != residuals_per_view.end() ) { const std::vector<double> & residuals = residuals_per_view.at(id_view); if (!residuals.empty()) { double min, max, mean, median; minMaxMeanMedian(residuals.begin(), residuals.end(), min, max, mean, median); os << sColBegin << residuals.size()/2 << sColEnd // #observations << sColBegin << min << sColEnd << sColBegin << median << sColEnd << sColBegin << mean << sColEnd << sColBegin << max <<sColEnd; } } } os << sRowEnd; htmlDocStream.pushInfo( os.str() ); } htmlDocStream.pushInfo( sTableEnd ); htmlDocStream.pushInfo( sFullLine ); // combine all residual values into one vector // export the SVG histogram { IndexT residualCount = 0; for (Hash_Map< IndexT, std::vector<double> >::const_iterator it = residuals_per_view.begin(); it != residuals_per_view.end(); ++it) { residualCount += it->second.size(); } // Concat per view residual values into one vector std::vector<double> residuals(residualCount); residualCount = 0; for (Hash_Map< IndexT, std::vector<double> >::const_iterator it = residuals_per_view.begin(); it != residuals_per_view.end(); ++it) { std::copy(it->second.begin(), it->second.begin()+it->second.size(), residuals.begin()+residualCount); residualCount += it->second.size(); } if (!residuals.empty()) { // RMSE computation const Eigen::Map<Eigen::RowVectorXd> residuals_mapping(&residuals[0], residuals.size()); const double RMSE = std::sqrt(residuals_mapping.squaredNorm() / (double)residuals.size()); os.str(""); os << sFullLine << "SfM Scene RMSE: " << RMSE << sFullLine; htmlDocStream.pushInfo(os.str()); const double maxRange = *max_element(residuals.begin(), residuals.end()); Histogram<double> histo(0.0, maxRange, 100); histo.Add(residuals.begin(), residuals.end()); svg::svgHisto svg_Histo; svg_Histo.draw(histo.GetHist(), std::pair<float,float>(0.f, maxRange), stlplus::create_filespec(stlplus::folder_part(htmlFilename), "residuals_histogram", "svg"), 600, 200); os.str(""); os << sNewLine<< "Residuals histogram" << sNewLine; os << "<img src=\"" << "residuals_histogram.svg" << "\" height=\"300\" width =\"800\">\n"; htmlDocStream.pushInfo(os.str()); } } std::ofstream htmlFileStream(htmlFilename.c_str()); htmlFileStream << htmlDocStream.getDoc(); const bool bOk = !htmlFileStream.bad(); return bOk; }
/// Save SfM_Data in an ASCII BAF (Bundle Adjustment File). // --Header // #Intrinsics // #Poses // #Landmarks // --Data // Intrinsic parameters [foc ppx ppy, ...] // Poses [angle axis, camera center] // Landmarks [X Y Z #observations id_intrinsic id_pose x y ...] //-- //- Export also a _imgList.txt file with View filename and id_intrinsic & id_pose. // filename id_intrinsic id_pose // The ids allow to establish a link between 3D point observations & the corresponding views //-- // Export missing poses as Identity pose to keep tracking of the original id_pose indexes static bool Save_BAF( const SfM_Data & sfm_data, const std::string & filename, ESfM_Data flags_part) { std::ofstream stream(filename.c_str()); if (!stream.is_open()) return false; bool bOk = false; { stream << sfm_data.GetIntrinsics().size() << '\n' << sfm_data.GetViews().size() << '\n' << sfm_data.GetLandmarks().size() << '\n'; const Intrinsics & intrinsics = sfm_data.GetIntrinsics(); for (Intrinsics::const_iterator iterIntrinsic = intrinsics.begin(); iterIntrinsic != intrinsics.end(); ++iterIntrinsic) { //get params const std::vector<double> intrinsicsParams = iterIntrinsic->second.get()->getParams(); std::copy(intrinsicsParams.begin(), intrinsicsParams.end(), std::ostream_iterator<double>(stream, " ")); stream << '\n'; } const Poses & poses = sfm_data.GetPoses(); for (Views::const_iterator iterV = sfm_data.GetViews().begin(); iterV != sfm_data.GetViews().end(); ++ iterV) { const View * view = iterV->second.get(); if (!sfm_data.IsPoseAndIntrinsicDefined(view)) { const Mat3 R = Mat3::Identity(); const double * rotation = R.data(); std::copy(rotation, rotation+9, std::ostream_iterator<double>(stream, " ")); const Vec3 C = Vec3::Zero(); const double * center = C.data(); std::copy(center, center+3, std::ostream_iterator<double>(stream, " ")); stream << '\n'; } else { // [Rotation col major 3x3; camera center 3x1] const double * rotation = poses.at(view->id_pose).rotation().data(); std::copy(rotation, rotation+9, std::ostream_iterator<double>(stream, " ")); const double * center = poses.at(view->id_pose).center().data(); std::copy(center, center+3, std::ostream_iterator<double>(stream, " ")); stream << '\n'; } } const Landmarks & landmarks = sfm_data.GetLandmarks(); for (Landmarks::const_iterator iterLandmarks = landmarks.begin(); iterLandmarks != landmarks.end(); ++iterLandmarks) { // Export visibility information // X Y Z #observations id_cam id_pose x y ... const double * X = iterLandmarks->second.X.data(); std::copy(X, X+3, std::ostream_iterator<double>(stream, " ")); const Observations & obs = iterLandmarks->second.obs; stream << obs.size() << " "; for (Observations::const_iterator iterOb = obs.begin(); iterOb != obs.end(); ++iterOb) { const IndexT id_view = iterOb->first; const View * v = sfm_data.GetViews().at(id_view).get(); stream << v->id_intrinsic << ' ' << v->id_pose << ' ' << iterOb->second.x(0) << ' ' << iterOb->second.x(1) << ' '; } stream << '\n'; } stream.flush(); bOk = stream.good(); stream.close(); } // Export View filenames & ids as an imgList.txt file { const std::string sFile = stlplus::create_filespec( stlplus::folder_part(filename), stlplus::basename_part(filename) + std::string("_imgList"), "txt"); stream.open(sFile.c_str()); if (!stream.is_open()) return false; for (Views::const_iterator iterV = sfm_data.GetViews().begin(); iterV != sfm_data.GetViews().end(); ++ iterV) { const std::string sView_filename = stlplus::create_filespec(sfm_data.s_root_path, iterV->second->s_Img_path); stream << sView_filename << ' ' << iterV->second->id_intrinsic << ' ' << iterV->second->id_pose << "\n"; } stream.flush(); bOk = stream.good(); stream.close(); } return bOk; }
/* OpenGL draw function & timing */ static void draw(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); { // convert opengl coordinates into the document information coordinates glPushMatrix(); glMultMatrixf((GLfloat*)m_convert); // apply view offset openMVG::Mat4 offset_w = l2w_Camera(Mat3::Identity(), Vec3(x_offset,y_offset,z_offset)); glMultMatrixd((GLdouble*)offset_w.data()); // then apply current camera transformation const View * view = sfm_data.GetViews().at(vec_cameras[current_cam]).get(); const Pose3 pose = sfm_data.GetPoseOrDie(view); const openMVG::Mat4 l2w = l2w_Camera(pose.rotation(), pose.translation()); glPushMatrix(); glMultMatrixd((GLdouble*)l2w.data()); glPointSize(3); glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); //Draw Structure in GREEN (as seen from the current camera) size_t nbPoint = sfm_data.GetLandmarks().size(); size_t cpt = 0; glBegin(GL_POINTS); glColor3f(0.f,1.f,0.f); for (Landmarks::const_iterator iter = sfm_data.GetLandmarks().begin(); iter != sfm_data.GetLandmarks().end(); ++iter) { const Landmark & landmark = iter->second; glVertex3d(landmark.X(0), landmark.X(1), landmark.X(2)); } glEnd(); glDisable(GL_CULL_FACE); for (int i_cam=0; i_cam < vec_cameras.size(); ++i_cam) { const View * view = sfm_data.GetViews().at(vec_cameras[i_cam]).get(); const Pose3 pose = sfm_data.GetPoseOrDie(view); const IntrinsicBase * cam = sfm_data.GetIntrinsics().at(view->id_intrinsic).get(); if (isPinhole(cam->getType())) { const Pinhole_Intrinsic * camPinhole = dynamic_cast<const Pinhole_Intrinsic*>(cam); // Move frame to draw the camera i_cam by applying its inverse transformation // Warning: translation has to be "fixed" to remove the current camera rotation // Fix camera_i translation with current camera rotation inverse const Vec3 trans = pose.rotation().transpose() * pose.translation(); // compute inverse transformation matrix from local to world const openMVG::Mat4 l2w_i = l2w_Camera(pose.rotation().transpose(), -trans); // stack it and use it glPushMatrix(); glMultMatrixd((GLdouble*)l2w_i.data()); // 1. Draw optical center (RED) and image center (BLUE) glPointSize(3); glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glBegin(GL_POINTS); glColor3f(1.f,0.f,0.f); glVertex3f(0, 0, 0); // optical center glColor3f(0.f,0.f,1.f); glVertex3f(0, 0, normalized_focal); // image center glEnd(); // compute image corners coordinated with normalized focal (f=normalized_focal) const int w = camPinhole->w(); const int h = camPinhole->h(); const double focal = camPinhole->focal(); // use principal point to adjust image center const Vec2 pp = camPinhole->principal_point(); Vec3 c1( -pp[0]/focal * normalized_focal, (-pp[1]+h)/focal * normalized_focal, normalized_focal); Vec3 c2((-pp[0]+w)/focal * normalized_focal, (-pp[1]+h)/focal * normalized_focal, normalized_focal); Vec3 c3((-pp[0]+w)/focal * normalized_focal, -pp[1]/focal * normalized_focal, normalized_focal); Vec3 c4( -pp[0]/focal * normalized_focal, -pp[1]/focal * normalized_focal, normalized_focal); // 2. Draw thumbnail if (i_cam == current_cam) { glEnable(GL_TEXTURE_2D); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glBindTexture(GL_TEXTURE_2D, m_image_vector[i_cam].texture); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); if (i_cam == current_cam) { glColor4f(0.5f,0.5f,0.5f, 0.7f); } else { glColor4f(0.5f,0.5f,0.5f, 0.5f); } glBegin(GL_QUADS); glTexCoord2d(0.0,1.0); glVertex3d(c1[0], c1[1], c1[2]); glTexCoord2d(1.0,1.0); glVertex3d(c2[0], c2[1], c2[2]); glTexCoord2d(1.0,0.0); glVertex3d(c3[0], c3[1], c3[2]); glTexCoord2d(0.0,0.0); glVertex3d(c4[0], c4[1], c4[2]); glEnd(); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); } // 3. Draw camera cone if (i_cam == current_cam) { glColor3f(1.f,1.f,0.f); } else { glColor3f(1.f,0.f,0.f); } glBegin(GL_LINES); glVertex3d(0.0,0.0,0.0); glVertex3d(c1[0], c1[1], c1[2]); glVertex3d(0.0,0.0,0.0); glVertex3d(c2[0], c2[1], c2[2]); glVertex3d(0.0,0.0,0.0); glVertex3d(c3[0], c3[1], c3[2]); glVertex3d(0.0,0.0,0.0); glVertex3d(c4[0], c4[1], c4[2]); glVertex3d(c1[0], c1[1], c1[2]); glVertex3d(c2[0], c2[1], c2[2]); glVertex3d(c2[0], c2[1], c2[2]); glVertex3d(c3[0], c3[1], c3[2]); glVertex3d(c3[0], c3[1], c3[2]); glVertex3d(c4[0], c4[1], c4[2]); glVertex3d(c4[0], c4[1], c4[2]); glVertex3d(c1[0], c1[1], c1[2]); glEnd(); glPopMatrix(); // go back to current camera frame } } glPopMatrix(); // go back to (document +offset) frame glPopMatrix(); // go back to identity } }
bool CreatePoint3DFile( const SfM_Data & sfm_data, const std::string & sPoints3DFilename) { /* points3D.txt # 3D point list with one line of data per point: # POINT3D_ID, X, Y, Z, R, G, B, ERROR, TRACK[] as (IMAGE_ID, POINT2D_IDX) # Number of points: X, mean track length: Y */ std::ofstream points3D_file( sPoints3DFilename ); if ( ! points3D_file ) { std::cerr << "Cannot write file" << sPoints3DFilename << std::endl; return false; } points3D_file << "# 3D point list with one line of data per point:\n"; points3D_file << "# POINT3D_ID, X, Y, Z, R, G, B, ERROR, TRACK[] as (IMAGE_ID, POINT2D_IDX)\n"; points3D_file << "# Number of points: X, mean track length: Y\n"; const Landmarks & landmarks = sfm_data.GetLandmarks(); std::vector<Vec3> vec_3dPoints, vec_tracksColor; if (!ColorizeTracks(sfm_data, vec_3dPoints, vec_tracksColor)) { return false; } C_Progress_display my_progress_bar( landmarks.size(), std::cout, "\n- CREATE POINT3D FILE -\n" ); int point_index = 0; for ( Landmarks::const_iterator iterLandmarks = landmarks.begin(); iterLandmarks != landmarks.end(); ++iterLandmarks, ++my_progress_bar ) { const Vec3 exportPoint = iterLandmarks->second.X; const IndexT point3d_id = iterLandmarks->first; points3D_file << point3d_id << " " << exportPoint.x() << " " << exportPoint.y() << " " << exportPoint.z() << " " << static_cast<int>(vec_tracksColor.at(point_index)(0)) << " " << static_cast<int>(vec_tracksColor.at(point_index)(1)) << " " << static_cast<int>(vec_tracksColor.at(point_index)(2)) << " "; ++point_index; const double error = 0.0; // Some error points3D_file << error; const Observations & obs = iterLandmarks->second.obs; for ( Observations::const_iterator itObs = obs.begin(); itObs != obs.end(); ++itObs ) { const IndexT viewId = itObs->first; const IndexT featId = itObs->second.id_feat; points3D_file << " " << viewId << " " << featId; } points3D_file << "\n"; } return true; }
bool CreateImageFile( const SfM_Data & sfm_data, const std::string & sImagesFilename) { /* images.txt # Image list with two lines of data per image: # IMAGE_ID, QW, QX, QY, QZ, TX, TY, TZ, CAMERA_ID, NAME # POINTS2D[] as (X, Y, POINT3D_ID) # Number of images: X, mean observations per image: Y */ // Header std::ofstream images_file( sImagesFilename ); if ( ! images_file ) { std::cerr << "Cannot write file" << sImagesFilename << std::endl; return false; } images_file << "# Image list with two lines of data per image:\n"; images_file << "# IMAGE_ID, QW, QX, QY, QZ, TX, TY, TZ, CAMERA_ID, NAME\n"; images_file << "# POINTS2D[] as (X, Y, POINT3D_ID)\n"; images_file << "# Number of images: X, mean observations per image: Y\n"; std::map< IndexT, std::vector< std::tuple<double, double, IndexT> > > viewIdToPoints2D; const Landmarks & landmarks = sfm_data.GetLandmarks(); { for ( Landmarks::const_iterator iterLandmarks = landmarks.begin(); iterLandmarks != landmarks.end(); ++iterLandmarks) { const IndexT point3d_id = iterLandmarks->first; // Tally set of feature observations const Observations & obs = iterLandmarks->second.obs; for ( Observations::const_iterator itObs = obs.begin(); itObs != obs.end(); ++itObs ) { const IndexT currentViewId = itObs->first; const Observation & ob = itObs->second; viewIdToPoints2D[currentViewId].push_back(std::make_tuple(ob.x( 0 ), ob.x( 1 ), point3d_id)); } } } { C_Progress_display my_progress_bar( sfm_data.GetViews().size(), std::cout, "\n- CREATE IMAGE FILE -\n" ); for (Views::const_iterator iter = sfm_data.GetViews().begin(); iter != sfm_data.GetViews().end(); ++iter, ++my_progress_bar) { const View * view = iter->second.get(); if ( !sfm_data.IsPoseAndIntrinsicDefined( view ) ) { continue; } const Pose3 pose = sfm_data.GetPoseOrDie( view ); const Mat3 rotation = pose.rotation(); const Vec3 translation = pose.translation(); const double Tx = translation[0]; const double Ty = translation[1]; const double Tz = translation[2]; Eigen::Quaterniond q( rotation ); const double Qx = q.x(); const double Qy = q.y(); const double Qz = q.z(); const double Qw = q.w(); const IndexT image_id = view->id_view; // Colmap's camera_ids correspond to openMVG's intrinsic ids const IndexT camera_id = view->id_intrinsic; const std::string image_name = view->s_Img_path; // first line per image //IMAGE_ID, QW, QX, QY, QZ, TX, TY, TZ, CAMERA_ID, NAME images_file << image_id << " " << Qw << " " << Qx << " " << Qy << " " << Qz << " " << Tx << " " << Ty << " " << Tz << " " << camera_id << " " << image_name << " " << "\n"; // second line per image //POINTS2D[] as (X, Y, POINT3D_ID) for (auto point2D: viewIdToPoints2D[image_id]) { images_file << std::get<0>(point2D) << " " << std::get<1>(point2D) << " " << std::get<2>(point2D) << " "; } images_file << "\n"; } } return true; }
/// Find the color of the SfM_Data Landmarks/structure bool ColorizeTracks( const SfM_Data & sfm_data, std::vector<Vec3> & vec_3dPoints, std::vector<Vec3> & vec_tracksColor) { // Colorize each track // Start with the most representative image // and iterate to provide a color to each 3D point { C_Progress_display my_progress_bar(sfm_data.GetLandmarks().size(), std::cout, "\nCompute scene structure color\n"); vec_tracksColor.resize(sfm_data.GetLandmarks().size()); vec_3dPoints.resize(sfm_data.GetLandmarks().size()); //Build a list of contiguous index for the trackIds std::map<IndexT, IndexT> trackIds_to_contiguousIndexes; IndexT cpt = 0; for (Landmarks::const_iterator it = sfm_data.GetLandmarks().begin(); it != sfm_data.GetLandmarks().end(); ++it, ++cpt) { trackIds_to_contiguousIndexes[it->first] = cpt; vec_3dPoints[cpt] = it->second.X; } // The track list that will be colored (point removed during the process) std::set<IndexT> remainingTrackToColor; std::transform(sfm_data.GetLandmarks().begin(), sfm_data.GetLandmarks().end(), std::inserter(remainingTrackToColor, remainingTrackToColor.begin()), stl::RetrieveKey()); while( !remainingTrackToColor.empty() ) { // Find the most representative image (for the remaining 3D points) // a. Count the number of observation per view for each 3Dpoint Index // b. Sort to find the most representative view index std::map<IndexT, IndexT> map_IndexCardinal; // ViewId, Cardinal for (std::set<IndexT>::const_iterator iterT = remainingTrackToColor.begin(); iterT != remainingTrackToColor.end(); ++iterT) { const size_t trackId = *iterT; const Observations & obs = sfm_data.GetLandmarks().at(trackId).obs; for (Observations::const_iterator iterObs = obs.begin(); iterObs != obs.end(); ++iterObs) { const size_t viewId = iterObs->first; if (map_IndexCardinal.find(viewId) == map_IndexCardinal.end()) map_IndexCardinal[viewId] = 1; else ++map_IndexCardinal[viewId]; } } // Find the View index that is the most represented std::vector<IndexT> vec_cardinal; std::transform(map_IndexCardinal.begin(), map_IndexCardinal.end(), std::back_inserter(vec_cardinal), stl::RetrieveValue()); using namespace stl::indexed_sort; std::vector< sort_index_packet_descend< IndexT, IndexT> > packet_vec(vec_cardinal.size()); sort_index_helper(packet_vec, &vec_cardinal[0], 1); // First image index with the most of occurence std::map<IndexT, IndexT>::const_iterator iterTT = map_IndexCardinal.begin(); std::advance(iterTT, packet_vec[0].index); const size_t view_index = iterTT->first; const View * view = sfm_data.GetViews().at(view_index).get(); const std::string sView_filename = stlplus::create_filespec(sfm_data.s_root_path, view->s_Img_path); Image<RGBColor> image_rgb; Image<unsigned char> image_gray; const bool b_rgb_image = ReadImage(sView_filename.c_str(), &image_rgb); if (!b_rgb_image) //try Gray level { const bool b_gray_image = ReadImage(sView_filename.c_str(), &image_gray); if (!b_gray_image) { std::cerr << "Cannot open provided the image." << std::endl; return false; } } // Iterate through the remaining track to color // - look if the current view is present to color the track std::set<IndexT> set_toRemove; for (std::set<IndexT>::const_iterator iterT = remainingTrackToColor.begin(); iterT != remainingTrackToColor.end(); ++iterT) { const size_t trackId = *iterT; const Observations & obs = sfm_data.GetLandmarks().at(trackId).obs; Observations::const_iterator it = obs.find(view_index); if (it != obs.end()) { // Color the track const Vec2 & pt = it->second.x; const RGBColor color = b_rgb_image ? image_rgb(pt.y(), pt.x()) : RGBColor(image_gray(pt.y(), pt.x())); vec_tracksColor[ trackIds_to_contiguousIndexes[trackId] ] = Vec3(color.r(), color.g(), color.b()); set_toRemove.insert(trackId); ++my_progress_bar; } } // Remove colored track for (std::set<IndexT>::const_iterator iter = set_toRemove.begin(); iter != set_toRemove.end(); ++iter) { remainingTrackToColor.erase(*iter); } } } return true; }