Skip to content

Commit ce5dbdb

Browse files
author
swayfreeda
committed
add task5
1 parent 62618de commit ce5dbdb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3565
-260
lines changed

CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
cmake_minimum_required(VERSION 3.5)
22
project(ImageBasedModellingEdu)
3+
34
set(CMAKE_CXX_STANDARD 11)
45
set(CMAKE_CXX_FLAGS "-fPIC")
56

@@ -8,4 +9,5 @@ add_subdirectory(core)
89
add_subdirectory(util)
910
add_subdirectory(features)
1011
add_subdirectory(sfm)
11-
add_subdirectory(examples)
12+
add_subdirectory(mvs)
13+
add_subdirectory(examples)

core/CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set(HEADERS
3131
bundle.h
3232
bundle_io.h
3333
camera.h
34+
depthmap.h
3435
image.h
3536
image_base.h
3637
image_color.h
@@ -40,17 +41,37 @@ set(HEADERS
4041
image_tools.h
4142
scene.h
4243
view.h
44+
mesh.h
45+
mesh_info.h
46+
mesh_tools.h
47+
mesh_io_ply.h
48+
mesh_io_obj.h
49+
mesh_io_off.h
50+
mesh_io_npts.h
51+
mesh_io_smf.h
52+
mesh_io_pbrt.h
4353
)
4454

4555
set(SOURCE_FILES
4656
bundle.cc
4757
bundle_io.cc
4858
camera.cc
59+
depthmap.cc
4960
image_exif.cc
5061
image_io.cc
5162
image_tools.cc
5263
scene.cc
5364
view.cc
65+
mesh.cc
66+
mesh_info.cc
67+
mesh_tools.cc
68+
mesh_io.cc
69+
mesh_io_ply.cc
70+
mesh_io_obj.cc
71+
mesh_io_off.cc
72+
mesh_io_npts.cc
73+
mesh_io_smf.cc
74+
mesh_io_pbrt.cc
5475
)
5576
add_library(core ${HEADERS} ${SOURCE_FILES})
5677
target_link_libraries(core util ${PNG_LIBRARIES} ${JPEG_LIBRARIES} ${TIFF_LIBRARIES})

core/depthmap.cc

+88-61
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
#include "math/defines.h"
1616
#include "math/matrix.h"
17-
#include "mve/mesh_info.h"
18-
#include "mve/depthmap.h"
19-
#include "mve/mesh_tools.h"
17+
#include "core/mesh_info.h"
18+
#include "core/depthmap.h"
19+
#include "core/mesh_tools.h"
2020

21-
MVE_NAMESPACE_BEGIN
22-
MVE_IMAGE_NAMESPACE_BEGIN
21+
CORE_NAMESPACE_BEGIN
22+
CORE_IMAGE_NAMESPACE_BEGIN
2323

2424
void
2525
depthmap_cleanup_grow (FloatImage::ConstPtr dm, FloatImage::Ptr ret,
@@ -127,14 +127,15 @@ depthmap_confidence_clean (FloatImage::Ptr dm, FloatImage::ConstPtr cm)
127127
dm->at(i, 0) = 0.0f;
128128
}
129129

130-
MVE_IMAGE_NAMESPACE_END
131-
MVE_NAMESPACE_END
130+
CORE_IMAGE_NAMESPACE_END
131+
CORE_NAMESPACE_END
132132

133133
/* ---------------------------------------------------------------- */
134134

135-
MVE_NAMESPACE_BEGIN
136-
MVE_GEOM_NAMESPACE_BEGIN
135+
CORE_NAMESPACE_BEGIN
136+
CORE_GEOM_NAMESPACE_BEGIN
137137

138+
// todo foot print 是什么意思??
138139
float
139140
pixel_footprint (std::size_t x, std::size_t y, float depth,
140141
math::Matrix3f const& invproj)
@@ -158,22 +159,20 @@ pixel_3dpos (std::size_t x, std::size_t y, float depth,
158159
/* ---------------------------------------------------------------- */
159160

160161
void
161-
dm_make_triangle (TriangleMesh* mesh, mve::Image<unsigned int>& vidx,
162+
dm_make_triangle (TriangleMesh* mesh, core::Image<unsigned int>& vidx,
162163
FloatImage const* dm, math::Matrix3f const& invproj,
163164
std::size_t i, int* tverts)
164165
{
165166
int const width = vidx.width();
166167
//int const height = vidx.height();
167-
mve::TriangleMesh::VertexList& verts(mesh->get_vertices());
168-
mve::TriangleMesh::FaceList& faces(mesh->get_faces());
168+
core::TriangleMesh::VertexList& verts(mesh->get_vertices());
169+
core::TriangleMesh::FaceList& faces(mesh->get_faces());
169170

170-
for (int j = 0; j < 3; ++j)
171-
{
171+
for (int j = 0; j < 3; ++j) {
172172
int iidx = i + (tverts[j] % 2) + width * (tverts[j] / 2);
173173
int x = iidx % width;
174174
int y = iidx / width;
175-
if (vidx.at(iidx) == MATH_MAX_UINT)
176-
{
175+
if (vidx.at(iidx) == MATH_MAX_UINT) {
177176
/* Add vertex for depth pixel. */
178177
vidx.at(iidx) = verts.size();
179178
float depth = dm->at(iidx, 0);
@@ -209,61 +208,81 @@ dm_is_depthdisc (float* widths, float* depths, float dd_factor, int i1, int i2)
209208

210209
TriangleMesh::Ptr
211210
depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const& invproj,
212-
float dd_factor, mve::Image<unsigned int>* vids)
211+
float dd_factor, core::Image<unsigned int>* vids)
213212
{
213+
// 深度图不为空
214214
if (dm == nullptr)
215215
throw std::invalid_argument("Null depthmap given");
216216

217+
// 图像的宽和高
217218
int const width = dm->width();
218219
int const height = dm->height();
219220

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

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

230+
// 在深度图中遍历2x2 blocks,并且创建三角面片
227231
/* Iterate over 2x2-blocks in the depthmap and create triangles. */
228232
int i = 0;
229-
for (int y = 0; y < height - 1; ++y, ++i)
230-
{
231-
for (int x = 0; x < width - 1; ++x, ++i)
232-
{
233+
for (int y = 0; y < height - 1; ++y, ++i) {
234+
for (int x = 0; x < width - 1; ++x, ++i) {
235+
233236
/* Cache the four depth values. */
234-
float depths[4] = { dm->at(i, 0), dm->at(i + 1, 0),
235-
dm->at(i + width, 0), dm->at(i + width + 1, 0) };
237+
/*
238+
* 0, 1
239+
* 2, 3
240+
*/
241+
float depths[4] = { dm->at(i, 0), dm->at(i + 1, 0),
242+
dm->at(i + width, 0), dm->at(i + width + 1, 0)
243+
};
236244

237245
/* Create a mask representation of the available depth values. */
246+
/* 创建mask记录深度有效的像素个数
247+
* mask=0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001,
248+
* 1010, 1011, 1100, 1101, 1110, 1111
249+
*/
238250
int mask = 0;
239251
int pixels = 0;
240-
for (int j = 0; j < 4; ++j)
241-
if (depths[j] > 0.0f)
242-
{
252+
for (int j = 0; j < 4; ++j){
253+
if (depths[j] > 0.0f) {
243254
mask |= 1 << j;
244255
pixels += 1;
245256
}
257+
}
246258

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

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

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

259-
switch (mask)
260-
{
261-
case 7: tri[0] = 1; break;
262-
case 11: tri[0] = 2; break;
263-
case 13: tri[0] = 3; break;
264-
case 14: tri[0] = 4; break;
265-
case 15:
277+
switch (mask) {
278+
279+
case 7: tri[0] = 1; break; // 0111- 0,1,2
280+
case 11: tri[0] = 2; break; // 1011- 0,1,3
281+
case 13: tri[0] = 3; break; // 1101- 0,2,3
282+
case 14: tri[0] = 4; break; // 1110- 1,2,3
283+
case 15: // 1111- 0,1,2,3
266284
{
285+
// 空圆特性
267286
/* Choose the triangulation with smaller diagonal. */
268287
float ddiff1 = std::abs(depths[0] - depths[3]);
269288
float ddiff2 = std::abs(depths[1] - depths[2]);
@@ -277,21 +296,18 @@ depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const& invproj,
277296
}
278297

279298
/* Omit depth discontinuity detection if dd_factor is zero. */
280-
if (dd_factor > 0.0f)
281-
{
299+
if (dd_factor > 0.0f) {
282300
/* Cache pixel footprints. */
283301
float widths[4];
284-
for (int j = 0; j < 4; ++j)
285-
{
302+
for (int j = 0; j < 4; ++j) {
286303
if (depths[j] == 0.0f)
287304
continue;
288-
widths[j] = pixel_footprint(x + (j % 2), y + (j / 2),
289-
depths[j], invproj);// w, h, focal_len);
305+
widths[j] = pixel_footprint(x + (j % 2), y + (j / 2), depths[j], invproj);// w, h, focal_len);
290306
}
291307

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

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

344+
// 图像的宽和高
328345
int const width = dm->width();
329346
int const height = dm->height();
330347

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

334352
/* Triangulate depth map. */
335-
mve::Image<unsigned int> vids;
336-
mve::TriangleMesh::Ptr mesh;
337-
mesh = mve::geom::depthmap_triangulate(dm, invproj, dd_factor, &vids);
353+
// 对深度图进行三角化
354+
core::Image<unsigned int> vids;
355+
core::TriangleMesh::Ptr mesh;
356+
mesh = core::geom::depthmap_triangulate(dm, invproj, dd_factor, &vids);
338357

358+
// 获取颜色
339359
if (ci == nullptr)
340360
return mesh;
341361

362+
/*计算顶点的颜色*/
342363
/* Use vertex index mapping to color the mesh. */
343-
mve::TriangleMesh::ColorList& colors(mesh->get_vertex_colors());
344-
mve::TriangleMesh::VertexList const& verts(mesh->get_vertices());
364+
core::TriangleMesh::ColorList& colors(mesh->get_vertex_colors());
365+
core::TriangleMesh::VertexList const& verts(mesh->get_vertices());
345366
colors.resize(verts.size());
346367

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

353376
math::Vec4f color(ci->at(i, 0), 0.0f, 0.0f, 255.0f);
354-
if (ci->channels() >= 3)
355-
{
377+
if (ci->channels() >= 3) {
356378
color[1] = ci->at(i, 1);
357379
color[2] = ci->at(i, 2);
358380
}
359-
else
360-
{
381+
else {
361382
color[1] = color[2] = color[0];
362383
}
363384
colors[vids[i]] = color / 255.0f;
@@ -372,22 +393,29 @@ TriangleMesh::Ptr
372393
depthmap_triangulate (FloatImage::ConstPtr dm, ByteImage::ConstPtr ci,
373394
CameraInfo const& cam, float dd_factor)
374395
{
396+
// 确保深度图不为空
375397
if (dm == nullptr)
376398
throw std::invalid_argument("Null depthmap given");
399+
400+
// 确保相机参数已经恢复
377401
if (cam.flen == 0.0f)
378402
throw std::invalid_argument("Invalid camera given");
379403

380404
/* Triangulate depth map. */
405+
// 计算投影矩阵的逆矩阵
381406
math::Matrix3f invproj;
382407
cam.fill_inverse_calibration(*invproj, dm->width(), dm->height());
383-
mve::TriangleMesh::Ptr mesh;
384-
mesh = mve::geom::depthmap_triangulate(dm, ci, invproj, dd_factor);
408+
409+
// 对深度图进行三角化,注意此时的mesh顶点坐标位于相机坐标系中
410+
core::TriangleMesh::Ptr mesh;
411+
mesh = core::geom::depthmap_triangulate(dm, ci, invproj, dd_factor);
385412

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

392420
return mesh;
393421
}
@@ -508,8 +536,7 @@ depthmap_mesh_confidences (TriangleMesh::Ptr mesh, int iterations)
508536
std::vector<std::size_t> vidx;
509537
MeshInfo mesh_info(mesh);
510538

511-
for (std::size_t i = 0; i < mesh_info.size(); ++i)
512-
{
539+
for (std::size_t i = 0; i < mesh_info.size(); ++i) {
513540
if (mesh_info[i].vclass == MeshInfo::VERTEX_CLASS_BORDER)
514541
vidx.push_back(i);
515542
}
@@ -578,5 +605,5 @@ depthmap_mesh_peeling (TriangleMesh::Ptr mesh, int iterations)
578605
math::algo::vector_clean(delete_list, &faces);
579606
}
580607

581-
MVE_GEOM_NAMESPACE_END
582-
MVE_NAMESPACE_END
608+
CORE_GEOM_NAMESPACE_END
609+
CORE_NAMESPACE_END

0 commit comments

Comments
 (0)