Skip to content

Commit

Permalink
fix problems of texturing
Browse files Browse the repository at this point in the history
  • Loading branch information
swayfreeda committed Nov 3, 2018
1 parent 3170c83 commit d0a48c8
Show file tree
Hide file tree
Showing 59 changed files with 1,497 additions and 1,553 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ add_subdirectory(mvs)
add_subdirectory(examples)
add_subdirectory(surface)
add_subdirectory(texturing)
add_subdirectory(3rdParty/rayint)
add_subdirectory(3rdParty/mrf)
add_subdirectory(3rdParty/coldet)
add_subdirectory(3rdParty/gco)
#add_subdirectory(3rdParty/rayint)
#add_subdirectory(3rdParty/eigen)
#add_subdirectory(3rdParty/mapmap)
116 changes: 45 additions & 71 deletions core/depthmap.cc
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ CORE_NAMESPACE_END
CORE_NAMESPACE_BEGIN
CORE_GEOM_NAMESPACE_BEGIN

// todo foot print 是什么意思??
float
pixel_footprint (std::size_t x, std::size_t y, float depth,
math::Matrix3f const& invproj)
Expand Down Expand Up @@ -168,11 +167,13 @@ dm_make_triangle (TriangleMesh* mesh, core::Image<unsigned int>& vidx,
core::TriangleMesh::VertexList& verts(mesh->get_vertices());
core::TriangleMesh::FaceList& faces(mesh->get_faces());

for (int j = 0; j < 3; ++j) {
for (int j = 0; j < 3; ++j)
{
int iidx = i + (tverts[j] % 2) + width * (tverts[j] / 2);
int x = iidx % width;
int y = iidx / width;
if (vidx.at(iidx) == MATH_MAX_UINT) {
if (vidx.at(iidx) == MATH_MAX_UINT)
{
/* Add vertex for depth pixel. */
vidx.at(iidx) = verts.size();
float depth = dm->at(iidx, 0);
Expand Down Expand Up @@ -210,79 +211,59 @@ TriangleMesh::Ptr
depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const& invproj,
float dd_factor, core::Image<unsigned int>* vids)
{
// 深度图不为空
if (dm == nullptr)
throw std::invalid_argument("Null depthmap given");

// 图像的宽和高
int const width = dm->width();
int const height = dm->height();

// 创建三角网格接结构体
/* Prepare triangle mesh. */
TriangleMesh::Ptr mesh(TriangleMesh::create());

/* Generate image that maps image pixels to vertex IDs. */
// 创建映射图,将图像像素映射到三维点的索引
core::Image<unsigned int> vidx(width, height, 1);
vidx.fill(MATH_MAX_UINT);

// 在深度图中遍历2x2 blocks,并且创建三角面片
/* Iterate over 2x2-blocks in the depthmap and create triangles. */
int i = 0;
for (int y = 0; y < height - 1; ++y, ++i) {
for (int x = 0; x < width - 1; ++x, ++i) {

for (int y = 0; y < height - 1; ++y, ++i)
{
for (int x = 0; x < width - 1; ++x, ++i)
{
/* Cache the four depth values. */
/*
* 0, 1
* 2, 3
*/
float depths[4] = { dm->at(i, 0), dm->at(i + 1, 0),
dm->at(i + width, 0), dm->at(i + width + 1, 0)
};
float depths[4] = { dm->at(i, 0), dm->at(i + 1, 0),
dm->at(i + width, 0), dm->at(i + width + 1, 0) };

/* Create a mask representation of the available depth values. */
/* 创建mask记录深度有效的像素个数
* mask=0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001,
* 1010, 1011, 1100, 1101, 1110, 1111
*/
int mask = 0;
int pixels = 0;
for (int j = 0; j < 4; ++j){
if (depths[j] > 0.0f) {
for (int j = 0; j < 4; ++j)
if (depths[j] > 0.0f)
{
mask |= 1 << j;
pixels += 1;
}
}

// 至少保证3个深度值是可靠的
/* At least three valid depth values are required. */
if (pixels < 3)
continue;


/* Possible triangles, vertex indices relative to 2x2 block. */
/* 可能出现的三角面片对,4个点有2个面片
*/
int tris[4][3] = {
{ 0, 2, 1 }, { 0, 3, 1 }, { 0, 2, 3 }, { 1, 2, 3 }
};

/* Decide which triangles to issue. */
/* 决定用哪对面片
*/
int tri[2] = { 0, 0 };

switch (mask) {

case 7: tri[0] = 1; break; // 0111- 0,1,2
case 11: tri[0] = 2; break; // 1011- 0,1,3
case 13: tri[0] = 3; break; // 1101- 0,2,3
case 14: tri[0] = 4; break; // 1110- 1,2,3
case 15: // 1111- 0,1,2,3
switch (mask)
{
case 7: tri[0] = 1; break;
case 11: tri[0] = 2; break;
case 13: tri[0] = 3; break;
case 14: tri[0] = 4; break;
case 15:
{
// 空圆特性
/* Choose the triangulation with smaller diagonal. */
float ddiff1 = std::abs(depths[0] - depths[3]);
float ddiff2 = std::abs(depths[1] - depths[2]);
Expand All @@ -296,18 +277,21 @@ depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const& invproj,
}

/* Omit depth discontinuity detection if dd_factor is zero. */
if (dd_factor > 0.0f) {
if (dd_factor > 0.0f)
{
/* Cache pixel footprints. */
float widths[4];
for (int j = 0; j < 4; ++j) {
for (int j = 0; j < 4; ++j)
{
if (depths[j] == 0.0f)
continue;
widths[j] = pixel_footprint(x + (j % 2), y + (j / 2), depths[j], invproj);// w, h, focal_len);
widths[j] = pixel_footprint(x + (j % 2), y + (j / 2),
depths[j], invproj);// w, h, focal_len);
}

// 检查深度不一致性,相邻像素的深度差值不要超过像素宽度(三维空间中)的dd_factor倍
/* Check for depth discontinuities. */
for (int j = 0; j < 2 && tri[j] != 0; ++j) {
for (int j = 0; j < 2 && tri[j] != 0; ++j)
{
int* tv = tris[tri[j] - 1];
#define DM_DD_ARGS widths, depths, dd_factor
if (dm_is_depthdisc(DM_DD_ARGS, tv[0], tv[1])) tri[j] = 0;
Expand All @@ -317,7 +301,8 @@ depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const& invproj,
}

/* Build triangles. */
for (int j = 0; j < 2; ++j) {
for (int j = 0; j < 2; ++j)
{
if (tri[j] == 0) continue;
#define DM_MAKE_TRI_ARGS mesh.get(), vidx, dm.get(), invproj, i
dm_make_triangle(DM_MAKE_TRI_ARGS, tris[tri[j] - 1]);
Expand All @@ -337,48 +322,42 @@ TriangleMesh::Ptr
depthmap_triangulate (FloatImage::ConstPtr dm, ByteImage::ConstPtr ci,
math::Matrix3f const& invproj, float dd_factor)
{
// 深度图像不为空
if (dm == nullptr)
throw std::invalid_argument("Null depthmap given");

// 图像的宽和高
int const width = dm->width();
int const height = dm->height();

// 彩色图像不为空,且和深度图像宽和高一致
if (ci != nullptr && (ci->width() != width || ci->height() != height))
throw std::invalid_argument("Color image dimension mismatch");

/* Triangulate depth map. */
// 对深度图进行三角化
core::Image<unsigned int> vids;
core::TriangleMesh::Ptr mesh;
mesh = core::geom::depthmap_triangulate(dm, invproj, dd_factor, &vids);

// 获取颜色
if (ci == nullptr)
return mesh;

/*计算顶点的颜色*/
/* Use vertex index mapping to color the mesh. */
core::TriangleMesh::ColorList& colors(mesh->get_vertex_colors());
core::TriangleMesh::VertexList const& verts(mesh->get_vertices());
colors.resize(verts.size());

// 像素个数
int num_pixel = vids.get_pixel_amount();
for (int i = 0; i < num_pixel; ++i)
{
// 像素没有对应的顶点
if (vids[i] == MATH_MAX_UINT)
continue;

math::Vec4f color(ci->at(i, 0), 0.0f, 0.0f, 255.0f);
if (ci->channels() >= 3) {
if (ci->channels() >= 3)
{
color[1] = ci->at(i, 1);
color[2] = ci->at(i, 2);
}
else {
else
{
color[1] = color[2] = color[0];
}
colors[vids[i]] = color / 255.0f;
Expand All @@ -393,29 +372,22 @@ TriangleMesh::Ptr
depthmap_triangulate (FloatImage::ConstPtr dm, ByteImage::ConstPtr ci,
CameraInfo const& cam, float dd_factor)
{
// 确保深度图不为空
if (dm == nullptr)
throw std::invalid_argument("Null depthmap given");

// 确保相机参数已经恢复
if (cam.flen == 0.0f)
throw std::invalid_argument("Invalid camera given");

/* Triangulate depth map. */
// 计算投影矩阵的逆矩阵
math::Matrix3f invproj;
cam.fill_inverse_calibration(*invproj, dm->width(), dm->height());

// 对深度图进行三角化,注意此时的mesh顶点坐标位于相机坐标系中
core::TriangleMesh::Ptr mesh;
mesh = core::geom::depthmap_triangulate(dm, ci, invproj, dd_factor);

/* Transform mesh to world coordinates. */
// 将网格从相机坐标系转化到世界坐标系中
math::Matrix4f ctw;
cam.fill_cam_to_world(*ctw);
core::geom::mesh_transform(mesh, ctw);
//mesh->recalc_normals(false, true); // Remove this?
mesh->recalc_normals(false, true); // Remove this?

return mesh;
}
Expand Down Expand Up @@ -534,10 +506,12 @@ depthmap_mesh_confidences (TriangleMesh::Ptr mesh, int iterations)

/* Find boundary vertices and remember them. */
std::vector<std::size_t> vidx;
MeshInfo mesh_info(mesh);
VertexInfoList vinfo(mesh);

for (std::size_t i = 0; i < mesh_info.size(); ++i) {
if (mesh_info[i].vclass == MeshInfo::VERTEX_CLASS_BORDER)
for (std::size_t i = 0; i < vinfo.size(); ++i)
{
MeshVertexInfo const& info(vinfo[i]);
if (info.vclass == VERTEX_CLASS_BORDER)
vidx.push_back(i);
}

Expand All @@ -557,7 +531,7 @@ depthmap_mesh_confidences (TriangleMesh::Ptr mesh, int iterations)
std::swap(vidx, cvidx);
for (std::size_t i = 0; i < cvidx.size(); ++i)
{
MeshInfo::VertexInfo info = mesh_info[cvidx[i]];
MeshVertexInfo info = vinfo[cvidx[i]];
for (std::size_t j = 0; j < info.verts.size(); ++j)
if (confs[info.verts[j]] == 1.0f)
vidx.push_back(info.verts[j]);
Expand Down Expand Up @@ -586,11 +560,11 @@ depthmap_mesh_peeling (TriangleMesh::Ptr mesh, int iterations)
/* Iteratively invalidate triangles at the boundary. */
for (int iter = 0; iter < iterations; ++iter)
{
MeshInfo mesh_info(mesh);
for (std::size_t i = 0; i < mesh_info.size(); ++i)
VertexInfoList::Ptr vinfo(VertexInfoList::create(mesh));
for (std::size_t i = 0; i < vinfo->size(); ++i)
{
MeshInfo::VertexInfo const& info(mesh_info[i]);
if (info.vclass == MeshInfo::VERTEX_CLASS_BORDER)
MeshVertexInfo const& info(vinfo->at(i));
if (info.vclass == VERTEX_CLASS_BORDER)
for (std::size_t j = 0; j < info.faces.size(); ++j)
for (int k = 0; k < 3; ++k)
{
Expand Down
46 changes: 15 additions & 31 deletions core/depthmap.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#ifndef MVE_DEPTHMAP_HEADER
#define MVE_DEPTHMAP_HEADER

#include "core/defines.h"
#include "math/vector.h"
#include "math/matrix.h"
#include "core/defines.h"
#include "core/camera.h"
#include "core/image.h"
#include "core/mesh.h"
Expand Down Expand Up @@ -61,7 +61,7 @@ depthmap_bilateral_filter (FloatImage::ConstPtr dm,
template <typename T>
void
depthmap_convert_conventions (typename Image<T>::Ptr dm,
math::Matrix3f const& invproj, bool to_mve);
math::Matrix3f const& invproj, bool to_core);

CORE_IMAGE_NAMESPACE_END
CORE_NAMESPACE_END
Expand Down Expand Up @@ -89,7 +89,7 @@ pixel_3dpos (std::size_t x, std::size_t y, float depth,
math::Matrix3f const& invproj);

/**
* \description * Algorithm to triangulate depth maps.
* Algorithm to triangulate depth maps.
*
* A factor may be specified that guides depth discontinuity detection. A
* depth discontinuity between pixels is assumed if depth difference is
Expand All @@ -100,41 +100,25 @@ pixel_3dpos (std::size_t x, std::size_t y, float depth,
* If 'vids' is not null, image content is replaced with vertex indices for
* each pixel that generated the vertex. Index MATH_MAX_UINT corresponds to
* a pixel that did not generate a vertex.
* @param dm -- 深度图
* @param invproj -- 投影矩阵的逆矩阵
* @param dd_factor -- 不确定因子
* @param vids --可视图像
* @return
*/
TriangleMesh::Ptr
depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const& invproj,
float dd_factor = 5.0f, core::Image<unsigned int>* vids = nullptr);


/**
* \description A helper function that triangulates the given depth map with optional
* color image (which generates additional per-vertex colors) in local
* image coordinates.
* @param dm
* @param ci
* @param invproj
* @param dd_factor
* @return
*/
/**
* A helper function that triangulates the given depth map with optional
* color image (which generates additional per-vertex colors) in local
* image coordinates.
*/
TriangleMesh::Ptr
depthmap_triangulate (FloatImage::ConstPtr dm, ByteImage::ConstPtr ci,
math::Matrix3f const& invproj, float dd_factor = 5.0f);

/**
* \description A helper function that triangulates the given depth map with optional
* color image (which generates additional per-vertex colors) and transforms
* the mesh into the global coordinate system.
* @param dm -- 深度图像
* @param ci -- 彩色图像
* @param cam -- 相机参数
* @param dd_factor -- ??
* @return
*/
/**
* A helper function that triangulates the given depth map with optional
* color image (which generates additional per-vertex colors) and transforms
* the mesh into the global coordinate system.
*/
TriangleMesh::Ptr
depthmap_triangulate (FloatImage::ConstPtr dm, ByteImage::ConstPtr ci,
CameraInfo const& cam, float dd_factor = 5.0f);
Expand Down Expand Up @@ -176,7 +160,7 @@ CORE_IMAGE_NAMESPACE_BEGIN
template <typename T>
inline void
depthmap_convert_conventions (typename Image<T>::Ptr dm,
math::Matrix3f const& invproj, bool to_mve)
math::Matrix3f const& invproj, bool to_core)
{
std::size_t w = dm->width();
std::size_t h = dm->height();
Expand All @@ -190,7 +174,7 @@ depthmap_convert_conventions (typename Image<T>::Ptr dm,
// Measure length of viewing ray
double len = px.norm();
// Either divide or multiply with the length
dm->at(pos) *= (to_mve ? len : 1.0 / len);
dm->at(pos) *= (to_core ? len : 1.0 / len);
}
}

Expand Down
Loading

0 comments on commit d0a48c8

Please sign in to comment.