Skip to content

Commit f6e2025

Browse files
committed
fixed point cloud refinement
1 parent 1655afd commit f6e2025

File tree

2 files changed

+95
-75
lines changed

2 files changed

+95
-75
lines changed

depthmap/depthmap.cpp

+76-62
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ bool Depthmap::loadTiff(const char *tiff, vector<float> &values, uint32_t &w, ui
253253
// Check if the TIFF is tiled
254254
uint32_t tileWidth, tileLength;
255255
if (!TIFFGetField(inTiff, TIFFTAG_TILEWIDTH, &tileWidth) ||
256-
!TIFFGetField(inTiff, TIFFTAG_TILELENGTH, &tileLength)) {
256+
!TIFFGetField(inTiff, TIFFTAG_TILELENGTH, &tileLength)) {
257257
return loadStripedTiff(inTiff, values, w, h, bitsPerSample);
258258
} else {
259259
return loadTiledTiff(inTiff, values, w, h, tileWidth, tileLength, bitsPerSample);
@@ -453,10 +453,10 @@ bool Depthmap::loadNormals(const char *normals_path){
453453
int i = x + y * width;
454454

455455
normals[i] = Eigen::Vector3f(
456-
(qRed(rgb) / 255.0f) * 2.0f - 1.0f,
457-
(qGreen(rgb) / 255.0f) * 2.0f - 1.0f,
458-
(qBlue(rgb) / 255.0f) * 2.0f - 1.0f
459-
);
456+
(qRed(rgb) / 255.0f) * 2.0f - 1.0f,
457+
(qGreen(rgb) / 255.0f) * 2.0f - 1.0f,
458+
(qBlue(rgb) / 255.0f) * 2.0f - 1.0f
459+
);
460460
}
461461
}
462462

@@ -774,7 +774,7 @@ bool OrthoDepthmap::loadXml(const char *xmlPath){
774774

775775

776776
if (originePlaniNodes.isEmpty() || resolutionPlaniNodes.isEmpty() ||
777-
origineAltiNodes.isEmpty() || resolutionAltiNodes.isEmpty()) {
777+
origineAltiNodes.isEmpty() || resolutionAltiNodes.isEmpty()) {
778778
cerr << "OriginePlani, ResolutionPlani, OrigineAlti, or ResolutionAlti not found in XML." << endl;
779779
return false;
780780

@@ -981,7 +981,73 @@ void OrthoDepthmap::verifyPointCloud(){
981981
*/
982982
}
983983
}
984+
//#define PRESERVE_INTERIOR
984985

986+
void OrthoDepthmap::beginIntegration(){
987+
988+
bool use_depthmap = false;
989+
if(use_depthmap) {
990+
//1. togliere dalla point tutti i punti che cadono dentro la maschera
991+
//2. aggiungere un campionamento regolare dentro la maschera
992+
int count = 0;
993+
for(size_t i = 0; i < point_cloud.size(); i++) {
994+
995+
Eigen::Vector3f point = point_cloud[i];
996+
Eigen::Vector3f pixel = realToPixelCoord(point[0], point[1], point[2]);
997+
998+
int mx = std::max<float>(0, std::min<float>(width-1, int(pixel[0])));
999+
int my = std::max<float>(0, std::min<float>(height-1, int(pixel[1])));
1000+
1001+
bool inside = mask[mx + my*width] == 0.0f;
1002+
if(inside)
1003+
continue;
1004+
1005+
point_cloud[count++] = point;
1006+
}
1007+
point_cloud.resize(count);
1008+
1009+
int step = 1;
1010+
1011+
for(int y = 0; y < height; y+= step) {
1012+
for(int x = 0; x < width; x += step) {
1013+
bool inside = (mask[x + y*width] != 0.0f);
1014+
if(!inside)
1015+
continue;
1016+
auto point = pixelToRealCoordinates(x, y, elevation[x + y*width]);
1017+
point_cloud.push_back(point);
1018+
}
1019+
}
1020+
}
1021+
1022+
for(size_t i =0; i < elevation.size(); i++) {
1023+
#ifdef PRESERVE_INTERIOR
1024+
if(mask[i] == 0.0f){
1025+
#endif
1026+
elevation[i] = 0.0f;
1027+
#ifdef PRESERVE_INTERIOR
1028+
}
1029+
#endif
1030+
}
1031+
// elevation.clear();
1032+
// elevation.resize(width * height, 0);
1033+
weights.clear();
1034+
weights.resize(width * height, 0);
1035+
1036+
}
1037+
1038+
void OrthoDepthmap::endIntegration(){
1039+
for(size_t i =0; i < elevation.size(); i++){
1040+
#ifdef PRESERVE_INTERIOR
1041+
if(mask[i] == 0.0f) {
1042+
#endif
1043+
if(weights[i] != 0.0f) {
1044+
elevation[i] /= weights[i];
1045+
}
1046+
#ifdef PRESERVE_INTERIOR
1047+
}
1048+
#endif
1049+
}
1050+
}
9851051

9861052
void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *outputFile){
9871053

@@ -999,40 +1065,6 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
9991065
std::vector<Eigen::Vector3f> imageCloud;
10001066
std::vector<float> source;
10011067

1002-
//1. togliere dalla point tutti i punti che cadono dentro la maschera
1003-
//2. aggiungere un campionamento regolare dentro la maschera
1004-
1005-
int count = 0;
1006-
1007-
for(int i = 0; i < point_cloud.size(); i++) {
1008-
1009-
Eigen::Vector3f point = point_cloud[i];
1010-
Eigen::Vector3f pixel = realToPixelCoord(point[0], point[1], point[2]);
1011-
1012-
int mx = std::max<float>(0, std::min<float>(width-1, int(pixel[0])));
1013-
int my = std::max<float>(0, std::min<float>(height-1, int(pixel[1])));
1014-
1015-
bool inside = mask[mx + my*width] == 0.0f;
1016-
if(inside)
1017-
continue;
1018-
1019-
point_cloud[count++] = point;
1020-
}
1021-
point_cloud.resize(count);
1022-
1023-
1024-
int step = 10;
1025-
1026-
for(int y = 0; y < height; y+= step) {
1027-
for(int x = 0; x < width; x += step) {
1028-
bool inside = mask[x + y*width] == 0.0f;
1029-
if(!inside)
1030-
continue;
1031-
auto point = pixelToRealCoordinates(x, y, elevation[x + y*width]);
1032-
point_cloud.push_back(point);
1033-
}
1034-
}
1035-
10361068
for (size_t i = 0; i < point_cloud.size(); i++) {
10371069

10381070
Eigen::Vector3f realCoord = point_cloud[i];
@@ -1047,11 +1079,9 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
10471079

10481080
if (pixelX >= 0 && pixelX < camera.width && pixelY >= 0 && pixelY < camera.height) {
10491081
float depthValue = camera.elevation[pixelX + pixelY * camera.width];
1050-
cout << pixelX << " " << pixelY << " " << depthValue << endl;
10511082
imageCloud.push_back(Eigen::Vector3f(imageCoords[0]/camera.width, imageCoords[1]/camera.height, h));
10521083
source.push_back(depthValue);
10531084
out << depthValue << "\t" << h << "\t" << imageCoords[0]/camera.width << "\t" << imageCoords[1]/camera.height <<"\n"; //red e green
1054-
10551085
}
10561086

10571087
}
@@ -1062,14 +1092,15 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
10621092
gaussianGrid.init(imageCloud, source);
10631093
gaussianGrid.imageGrid(("test.png"));
10641094

1065-
static float c = 1.0f;
10661095
float ortho_z = realToPixelCoord(0,0,z)[2];
10671096

10681097
for(size_t y=0; y < height; y++){
10691098
for(size_t x=0; x < width; x++){
1099+
#ifdef PRESERVE_INTERIOR
10701100
if(mask[x + y * width] != 0.0f){
10711101
continue;
10721102
}
1103+
#endif
10731104
Eigen::Vector3f r = pixelToRealCoordinates(x, y, ortho_z);
10741105
assert(fabs(z-r[2]) < 0.1f);
10751106
Eigen::Vector3f p = camera.camera.projectionToImage(r);
@@ -1081,6 +1112,7 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
10811112
float depthValue = camera.elevation[px + py * camera.width];
10821113
p[0] = px / float(camera.width);
10831114
p[1] = py / float(camera.height);
1115+
10841116
float h = gaussianGrid.target(p[0], p[1], depthValue);
10851117
Eigen::Vector3f d = realToPixelCoord(r[0], r[1], h);
10861118

@@ -1091,7 +1123,7 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
10911123

10921124
weights[x+ y * width] += w;
10931125

1094-
}else {
1126+
} else {
10951127
//elevation[x + y*width] = origin[2] + resolution[2] * elevation[x + y*width];
10961128
}
10971129
}
@@ -1358,24 +1390,6 @@ float Depthmap::calculateWeight(float x, float y) const{
13581390
return bell(x)*bell(y);
13591391
//return weightX * weightY;
13601392
}
1361-
void OrthoDepthmap::beginIntegration(){
1362-
for(size_t i =0; i < elevation.size(); i++){
1363-
if(mask[i] == 0.0f){
1364-
elevation[i] = 0.0f;
1365-
}
1366-
}
1367-
// elevation.clear();
1368-
// elevation.resize(width * height, 0);
1369-
// weights.clear();
1370-
// weights.resize(width * height, 0);
1371-
1372-
}
1373-
void OrthoDepthmap::endIntegration(){
1374-
for(size_t i =0; i < elevation.size(); i++){
1375-
//if(mask[i] == 0.0f) {// && weights[i] != 0.0f){
1376-
elevation[i] =4; ///= weights[i];
1377-
}
1378-
}
13791393

13801394
void GaussianGrid::imageGrid(const char* filename) {
13811395

depthmap/main.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int main(int argc, char *argv[]) {
1717
return 1;
1818
}*/
1919
//input
20-
#define MACOS 1
20+
//#define MACOS 1
2121
#ifdef MACOS
2222
QString base = "/Users/erika/Desktop/testcenterRel_copia/";
2323
#else
@@ -26,12 +26,12 @@ int main(int argc, char *argv[]) {
2626

2727

2828

29-
QString depthmapPath = base + "photogrammetry/Malt/Z_Num7_DeZoom4_STD-MALT.tif";
29+
QString depthmapPath = base + base + "Z_Num7_DeZoom4_STD-MALT.tif";
3030
//QString cameraDepthmap = base + "datasets/L04C12.tif";
3131
//QString orientationXmlPath = base + "photogrammetry/Ori-Relative/Orientation-L04C12.tif.xml";
32-
QString maskPath = base + "photogrammetry/Malt/Masq_STD-MALT_DeZoom4.tif";
33-
QString plyFile = base +"photogrammetry/AperiCloud_Relative__mini.ply";
34-
QString point_txt = base + "photogrammetry/points_h.txt";
32+
QString maskPath = base + "Masq_STD-MALT_DeZoom4.tif";
33+
QString plyFile = base +"AperiCloud_Relative__mini.ply";
34+
//QString point_txt = base + "photogrammetry/points_h.txt";
3535
Depthmap depth;
3636

3737
//output
@@ -44,13 +44,25 @@ int main(int argc, char *argv[]) {
4444

4545
OrthoDepthmap ortho;
4646

47+
48+
QFile::remove(depthmapPath);
49+
if (!QFile::copy(depthmapPath + "_backup.tif", depthmapPath)) {
50+
cout << "Errror copying depthmap" << endl;
51+
exit(0);
52+
}
53+
QFile::remove(maskPath);
54+
if (!QFile::copy( maskPath + "_backup.tif", maskPath)) {
55+
cout << "Error copying mask" << endl;
56+
exit(0);
57+
}
58+
4759
if(!ortho.load(qPrintable(depthmapPath), qPrintable(maskPath))){
4860
cout << "accidenti" << endl;
4961
return -1;
5062
}
5163

52-
QDir datasetsDir(base + "datasets");
53-
QDir xmlDir(base + "photogrammetry/Ori-Relative");
64+
QDir datasetsDir(base + "../datasets");
65+
QDir xmlDir(base + "Ori-Relative");
5466
QStringList tiffFilters = {"*.tif"};
5567
QStringList xmlFilters = {"Orientation-*.tif.xml"};
5668

@@ -84,10 +96,6 @@ int main(int argc, char *argv[]) {
8496
ortho.verifyPointCloud();
8597
ortho.beginIntegration();
8698

87-
if (QFile::copy(depthmapPath + "_backup.tif", depthmapPath))
88-
cout << "Copia di backup salvata come: " << (depthmapPath + "_backup.tif").toStdString() << endl;
89-
if (QFile::copy( maskPath + "_backup.tif", maskPath))
90-
cout << "Copia di backup salvata come: " << (maskPath + "_backup.tif").toStdString() << endl;
9199

92100
for (const QFileInfo &tiffFile : tiffFiles) {
93101

@@ -118,8 +126,6 @@ int main(int argc, char *argv[]) {
118126
QString outputTiffPath = base +"output_" + tiffFile.fileName();
119127
cout << "Output TIFF Path: " << outputTiffPath.toStdString() << endl;
120128

121-
122-
123129
}
124130
ortho.endIntegration();
125131
ortho.saveDepth(qPrintable(depthmapPath));

0 commit comments

Comments
 (0)