openMVS icon indicating copy to clipboard operation
openMVS copied to clipboard

TextureMesh crash

Open jlygit opened this issue 2 years ago • 1 comments

hello,

i use TextureMesh api base on my mesh and multi images and it throw an exception named that "invalid unordered_map<K, T> key". and I don't know what's wrong. the codes as follow and the test file is mesh_texture.zip

#include<string>
#include<cstring>
#include<iostream>
#include<fstream>
#include <Eigen/Dense>
#include"OpenMVS/MVS.h"
using namespace std;
using namespace SEACAVE;
using namespace MVS;
typedef Eigen::Matrix<double,3,3,Eigen::RowMajor> EMat33d;
typedef Eigen::Matrix<double,4,4,Eigen::RowMajor> EMat44d;
typedef Eigen::Matrix<double,3,1> EVec3d;
int readMetaData(const std::string& path, std::vector<std::string>& names,  std::vector<EMat33d > & ks,  std::vector<EMat44d> & poses, int& img_w, int& img_h) {
    std::ifstream file(path);
    if(!file.is_open()) {
        std::cerr<<"can not open file "<<path<<"\n";
        return -1;
    }
    string line;
    getline(file, line);
    auto sline = istringstream(line);
    int cam_n;
    sline >> img_h >> img_w >> cam_n;
    float nor_scale_size = std::max(img_h, img_w); 
    printf("h w n:%d %d %d\n", img_h, img_w, cam_n);
    int idx=0;
    while (getline(file, line)) {
        auto sline = istringstream(line);
        Eigen::Matrix3d k = Eigen::Matrix3d::Zero();
        Eigen::Matrix4d pose;
        {
            std::string name;
            sline>>name;
            names.push_back(name);
            sline>>k(0,0)>>k(1,1)>>k(0,2)>>k(1,2);
            k(2, 2)=1;
            for(int i=0;i<4;++i) { 
                for(int j=0;j<4;++j) {
                    sline >> pose(i, j);
                }
            }
            ks.push_back(k);
            poses.push_back(pose);
        }
    }
    file.close();
    if(cam_n != poses.size()) {
        return -1;
    }
    return 0;
}

int texture(const std::string& basepath="C:/Users/lyn/Desktop/test2271/mesh_texture") {
    int number_of_thread = 1;
    bool res;
    Scene scene(number_of_thread);
    std::string meta_path = basepath+"/metadata.txt";
    std::vector<EMat33d>  ks;
    std::vector<EMat44d> poses;
    std::vector<std::string> names;
    int img_w, img_h;
    if(readMetaData(meta_path, names, ks, poses, img_w, img_h) != 0) {
        printf("readMetaData fail\n");
        return -1;
    }

    Platform &plat = scene.platforms.AddEmpty();
    //1、name
    plat.name = "platform";
    for(int index=0;index<ks.size();++index) {
        CameraIntern &cam = plat.cameras.AddEmpty();
        cam.R = RMatrix::IDENTITY;
        cam.C = Point3(0,0,0);
        cam.K = ks[index];
        //normalize intrinsics
        cam.K = Camera::ScaleK<double>(cam.K, 1.0/Camera::GetNormalizationScale(img_w, img_h));

        // extrinsic
        EMat33d r_temp = poses[index].block<3, 3>(0, 0);
        EVec3d t_temp = poses[index].block<3, 1>(0, 3);
        Platform::Pose &pose = plat.poses.AddEmpty();

        // c=-R.transpose()*T
        Eigen::Map<EVec3d>(&pose.C.x) = -(r_temp.transpose() * t_temp );
        Eigen::Map<EMat33d>(pose.R.val) = r_temp;

        // load images 
               
        MVS::Image &img = scene.images.AddEmpty();
        img.ID = index;
        img.platformID = 0;
        img.cameraID = index;
        img.poseID = index; 

        std::string img_path = basepath+"/"+names[index];     
        res = img.LoadImage(SEACAVE::String(img_path));
        if(!res) {
            printf("load image fail, path:%s\n", img_path.c_str());
            return -2;
        }
    }
    
    res = scene.mesh.Load(basepath+"/mesh.ply"); 
    if(!res) {
        printf("mesh load fail\n");
        return -3;
    }
    scene.Save(basepath+"/init_mvs.mvs",ARCHIVE_TEXT);

    try{
        res = scene.TextureMesh(0, 640);
        if(!res) {
            printf("TextureMesh fail\n");
            return -4;
        }

        scene.Save(basepath+"/final_mvs.mvs",ARCHIVE_TEXT);
        scene.mesh.Save(basepath+"/final_mess.ply");
    }catch(std::exception &exp) {
        std::cout<<"error:"<<exp.what()<<"\n";
    }
    
    return 0;
}
int main( int argc, char* argv[] )
{
    return texture();
}

jlygit avatar Jul 01 '22 03:07 jlygit

your code is fine, the mesh however is non-manifold, clean it before texturing it

cdcseacave avatar Jul 01 '22 09:07 cdcseacave