Esempio n. 1
0
void ProjectorCalibration :: loadFromFile(const char* filename)
{
    QFileInfo f (filename);
    ntk_throw_exception_if(!f.exists(), "Could not find calibration file.");
    cv::FileStorage calibration_file (filename, CV_STORAGE_READ);
    readMatrix(calibration_file, "proj_intrinsics", intrinsics);
    readMatrix(calibration_file, "proj_distortion", distortion);
    readMatrix(calibration_file, "R", R);
    readMatrix(calibration_file, "T", T);
    cv::Mat1i size_mat;
    readMatrix(calibration_file, "proj_size", size_mat);
    proj_size = cv::Size(size_mat(0,0), size_mat(0,1));
    calibration_file.release();

    pose = new Pose3D();
    pose->toRightCamera(intrinsics, R, T);

    initUndistortRectifyMap(intrinsics, distortion,
                            Mat(), intrinsics, proj_size, CV_16SC2,
                            undistort_map1, undistort_map2);
}
Esempio n. 2
0
void Video3DUberShader::draw(RenderContext const& ctx,
                             std::string const& ksfile_name,
                             std::string const& material_name,
                             scm::math::mat4 const& model_matrix,
                             scm::math::mat4 const& normal_matrix,
                             Frustum const& /*frustum*/,
                             View const& view) const
{  if (!GeometryDatabase::instance()->is_supported(ksfile_name) ||
      !MaterialDatabase::instance()->is_supported(material_name)) {
    gua::Logger::LOG_WARNING << "Video3DUberShader::draw(): No such video or material." << ksfile_name << ", " << material_name << std::endl;
    return;
  }

  auto video3d_ressource = std::static_pointer_cast<Video3DRessource>(GeometryDatabase::instance()->lookup(ksfile_name));
  auto material          = MaterialDatabase::instance()->lookup(material_name);

  if (!video3d_ressource || !material) {
    gua::Logger::LOG_WARNING << "Video3DUberShader::draw(): Invalid video or material." << std::endl;
    return;
  }

  // update stream data
  video3d_ressource->update_buffers(ctx);

  // make sure ressources are on the GPU
  upload_to(ctx);
  {
    // single texture only
    scm::gl::context_all_guard guard(ctx.render_context);

    ctx.render_context->set_rasterizer_state(no_bfc_rasterizer_state_);
    ctx.render_context->set_depth_stencil_state(depth_stencil_state_warp_pass_);

    // set uniforms
    ctx.render_context->bind_texture(video3d_ressource->depth_array(ctx), nearest_sampler_state_, 0);
    get_program(warp_pass)->get_program(ctx)->uniform_sampler("depth_video3d_texture", 0);


    get_program(warp_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix");
    get_program(warp_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix");
    get_program(warp_pass)->set_uniform(ctx, int(1), "bbxclip");

    auto bbox(video3d_ressource->get_bounding_box());
    get_program(warp_pass)->set_uniform(ctx, bbox.min, "bbx_min");
    get_program(warp_pass)->set_uniform(ctx, bbox.max, "bbx_max");

    // pre passes
    for (unsigned layer = 0; layer != video3d_ressource->number_of_cameras(); ++layer)
    {
      // configure fbo
      warp_result_fbo_->clear_attachments();
      warp_result_fbo_->attach_depth_stencil_buffer(warp_depth_result_, 0, layer);
      warp_result_fbo_->attach_color_buffer(0, warp_color_result_, 0, layer);

      // bind and clear fbo
      ctx.render_context->set_frame_buffer(warp_result_fbo_);
      ctx.render_context->clear_depth_stencil_buffer(warp_result_fbo_);
      ctx.render_context->clear_color_buffer(warp_result_fbo_, 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f));
      ctx.render_context->set_viewport(scm::gl::viewport(scm::math::vec2ui(0,0), warp_color_result_->dimensions()));

      // set uniforms
      get_program(warp_pass)->set_uniform(ctx, video3d_ressource->calibration_file(layer).getTexSizeInvD(), "tex_size_inv");
      get_program(warp_pass)->set_uniform(ctx, int(layer), "layer");


      if (material && video3d_ressource)
      {
        get_program(warp_pass)->set_uniform(ctx, video3d_ressource->calibration_file(layer).getImageDToEyeD(), "image_d_to_eye_d");
        get_program(warp_pass)->set_uniform(ctx, video3d_ressource->calibration_file(layer).getEyeDToWorld(), "eye_d_to_world");
        get_program(warp_pass)->set_uniform(ctx, video3d_ressource->calibration_file(layer).getEyeDToEyeRGB(), "eye_d_to_eye_rgb");
        get_program(warp_pass)->set_uniform(ctx, video3d_ressource->calibration_file(layer).getEyeRGBToImageRGB(), "eye_rgb_to_image_rgb");

	      ctx.render_context->bind_texture(video3d_ressource->cv_xyz(ctx,layer), linear_sampler_state_, 1);
	      get_program(warp_pass)->get_program(ctx)->uniform_sampler("cv_xyz", 1);

	      ctx.render_context->bind_texture(video3d_ressource->cv_uv(ctx,layer), linear_sampler_state_, 2);
	      get_program(warp_pass)->get_program(ctx)->uniform_sampler("cv_uv", 2);

	      get_program(warp_pass)->set_uniform(ctx, video3d_ressource->calibration_file(layer).cv_min_d, "cv_min_d");
	      get_program(warp_pass)->set_uniform(ctx, video3d_ressource->calibration_file(layer).cv_max_d, "cv_max_d");

        get_program(warp_pass)->use(ctx);
        {
          video3d_ressource->draw(ctx);
        }
        get_program(warp_pass)->unuse(ctx);
      }

      ctx.render_context->reset_framebuffer();
    }
  }

  {
    // single texture only
    scm::gl::context_all_guard guard(ctx.render_context);

    ctx.render_context->set_depth_stencil_state(depth_stencil_state_warp_pass_);

    // second pass
    get_program(blend_pass)->use(ctx);
    {
      if (material && video3d_ressource)
      {
        set_uniform(ctx, material->get_id(), "gua_material_id");
        set_uniform(ctx, normal_matrix, "gua_normal_matrix");
        set_uniform(ctx, model_matrix, "gua_model_matrix");

        // needs to be multiplied with scene scaling
        set_uniform(ctx, 0.075f, "epsilon");
        set_uniform(ctx, int(video3d_ressource->number_of_cameras()), "numlayers");
        get_program(blend_pass)->set_uniform(ctx, int(material_name == default_video_material_name()), "using_default_video_material");
        get_program(blend_pass)->set_uniform(ctx, int(video3d_ressource->do_overwrite_normal()), "overwrite_normal");
        get_program(blend_pass)->set_uniform(ctx, video3d_ressource->get_overwrite_normal(), "o_normal");

        ctx.render_context->bind_texture(warp_color_result_, nearest_sampler_state_, 0);
        get_program(blend_pass)->get_program(ctx)->uniform_sampler("quality_texture", 0);

        ctx.render_context->bind_texture(warp_depth_result_, nearest_sampler_state_, 1);
        get_program(blend_pass)->get_program(ctx)->uniform_sampler("depth_texture", 1);

        ctx.render_context->bind_texture(video3d_ressource->color_array(ctx), linear_sampler_state_, 2);
        get_program(blend_pass)->get_program(ctx)->uniform_sampler("video_color_texture", 2);

        fullscreen_quad_->draw(ctx.render_context);
      }
    }
    get_program(blend_pass)->unuse(ctx);
  }
}
Esempio n. 3
0
void RGBDCalibration :: loadFromFile(const char* filename)
{
    QFileInfo f (filename);
    ntk_throw_exception_if(!f.exists(), "Could not find calibration file.");
    cv::FileStorage calibration_file (filename, CV_STORAGE_READ);
    readMatrix(calibration_file, "rgb_intrinsics", rgb_intrinsics);
    readMatrix(calibration_file, "rgb_distortion", rgb_distortion);
    zero_rgb_distortion = rgb_distortion(0,0) < 1e-5 ? true : false;
    readMatrix(calibration_file, "depth_intrinsics", depth_intrinsics);
    readMatrix(calibration_file, "depth_distortion", depth_distortion);
    zero_depth_distortion = depth_distortion(0,0) < 1e-5 ? true : false;

    readMatrix(calibration_file, "R", R);
    readMatrix(calibration_file, "T", T);
    cv::Mat1i size_mat;
    readMatrix(calibration_file, "rgb_size", size_mat);
    rgb_size = cv::Size(size_mat(0,0), size_mat(0,1));
    readMatrix(calibration_file, "raw_rgb_size", size_mat);
    raw_rgb_size = cv::Size(size_mat(0,0), size_mat(0,1));
    readMatrix(calibration_file, "depth_size", size_mat);
    depth_size = cv::Size(size_mat(0,0), size_mat(0,1));
    readMatrix(calibration_file, "raw_depth_size", size_mat);
    raw_depth_size = cv::Size(size_mat(0,0), size_mat(0,1));

    try {
        readMatrix(calibration_file, "R_extrinsics", R_extrinsics);
        readMatrix(calibration_file, "T_extrinsics", T_extrinsics);
    }
    catch (...)
    {
        ntk_dbg(0) << "Warning: could not load extrinsics (R_extrinsics, T_extrinsics).";
    }

    try
    {
        cv::Mat1i size_mat;
        readMatrix(calibration_file, "infrared_size", size_mat);
        infrared_size = cv::Size(size_mat(0,0), size_mat(0,1));
    }
    catch (...)
    {
        ntk_dbg(1) << "Could not read infrared image size, setting default 1280x1024";
        infrared_size = cv::Size(1280, 1024);
    }

    try
    {
        readMatrix(calibration_file, "infrared_intrinsics", infrared_intrinsics);
        readMatrix(calibration_file, "infrared_distortion", infrared_distortion);
    } catch (...)
    {
        ntk_dbg(1) << "Warning: cannot read infrared camera intrinsics, computing from depth.";
        computeInfraredIntrinsicsFromDepth();
    }

    cv::Mat1f depth_calib (1,2);
    try {
        readMatrix(calibration_file, "depth_base_and_offset", depth_calib);
        depth_baseline = depth_calib(0,0);
        depth_offset = depth_calib(0,1);
    }
    catch(...)
    {
        ntk_dbg(1) << "Warning: could not load depth offset";
    }

    try {
        cv::Mat1f depth_multiplicative_correction_factor (1,1);
        readMatrix(calibration_file, "depth_multiplicative_correction_factor", depth_multiplicative_correction_factor);
        this->depth_multiplicative_correction_factor = depth_multiplicative_correction_factor(0,0);
    }
    catch(...)
    {
        ntk_dbg(1) << "Warning: could not load depth multiplicative factor";
    }

    try {
        cv::Mat1f depth_additive_correction_factor (1,1);
        readMatrix(calibration_file, "depth_additive_correction_factor", depth_additive_correction_factor);
        this->depth_additive_correction_factor = depth_additive_correction_factor(0,0);
    }
    catch(...)
    {
        ntk_dbg(1) << "Warning: could not load depth additive correction factor";
    }

    calibration_file.release();

    depth_pose = new Pose3D();
    rgb_pose = new Pose3D();
    updatePoses();
}