Skip to content

Commit e690254

Browse files
committed
scaling rti depthmap before computing differences
1 parent ae577e6 commit e690254

File tree

3 files changed

+80
-39
lines changed

3 files changed

+80
-39
lines changed

depthmap/depthmap.cpp

+58-24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "depthmap.h"
88
#include "../src/bni_normal_integration.h"
99
#include <QFile>
10+
#include <QDebug>
1011
#include <QRegularExpression>
1112
#include <fstream>
1213

@@ -252,7 +253,7 @@ bool Depthmap::loadTiff(const char *tiff, vector<float> &values, uint32_t &w, ui
252253
// Check if the TIFF is tiled
253254
uint32_t tileWidth, tileLength;
254255
if (!TIFFGetField(inTiff, TIFFTAG_TILEWIDTH, &tileWidth) ||
255-
!TIFFGetField(inTiff, TIFFTAG_TILELENGTH, &tileLength)) {
256+
!TIFFGetField(inTiff, TIFFTAG_TILELENGTH, &tileLength)) {
256257
return loadStripedTiff(inTiff, values, w, h, bitsPerSample);
257258
} else {
258259
return loadTiledTiff(inTiff, values, w, h, tileWidth, tileLength, bitsPerSample);
@@ -452,10 +453,10 @@ bool Depthmap::loadNormals(const char *normals_path){
452453
int i = x + y * width;
453454

454455
normals[i] = Eigen::Vector3f(
455-
(qRed(rgb) / 255.0f) * 2.0f - 1.0f,
456-
(qGreen(rgb) / 255.0f) * 2.0f - 1.0f,
457-
(qBlue(rgb) / 255.0f) * 2.0f - 1.0f
458-
);
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+
);
459460
}
460461
}
461462

@@ -693,7 +694,6 @@ void Depthmap::resizeNormals (int factorPowerOfTwo, int step) {
693694
width = targetWidth;
694695
height = targetHeight;
695696

696-
//QString filename = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/surface.jpg";
697697
//saveNormals(filename.toStdString().c_str());
698698
//depthIntegrateNormals();
699699

@@ -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

@@ -980,9 +980,9 @@ void OrthoDepthmap::verifyPointCloud(){
980980
<< "Coordinate pixel: (" << pixelX << ", " << pixelY << " " << elevation[pixelX + pixelY * width]<< ")\n";
981981
*/
982982
}
983+
}
983984

984985

985-
}
986986
void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *outputFile){
987987

988988
QFile outFile(outputFile);
@@ -994,11 +994,11 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
994994
float z = point_cloud[0][2];
995995
auto o = camera.camera.projectionToImage(Eigen::Vector3f(0, 0, z));
996996
auto u = camera.camera.projectionToImage(Eigen::Vector3f(1, 0, z));
997-
float pixel_size = 1 / (u[0] - o[0]);
998-
999997

1000998
QTextStream out(&outFile);
1001-
std::vector<Eigen::Vector3f> differences;
999+
std::vector<Eigen::Vector3f> imageCloud;
1000+
std::vector<float> source;
1001+
10021002
for (size_t i = 0; i < point_cloud.size(); i++) {
10031003

10041004
Eigen::Vector3f& realCoord = point_cloud[i];
@@ -1012,8 +1012,7 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
10121012

10131013

10141014
if (pixelX >= 0 && pixelX < camera.width && pixelY >= 0 && pixelY < camera.height) {
1015-
float depthValue = z - camera.elevation[pixelX + pixelY * camera.width]* pixel_size;
1016-
1015+
float depthValue = camera.elevation[pixelX + pixelY * camera.width];
10171016
if(fabs(pixelCoord[0] - 170) < 3 && fabs(pixelCoord[1] - 150) < 3){
10181017
cout << "elevation: " << elevation[int(pixelCoord[0]) + int(pixelCoord[1]) *width] << endl;
10191018

@@ -1025,20 +1024,22 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
10251024
}
10261025

10271026

1028-
differences.push_back(Eigen::Vector3f(imageCoords[0]/camera.width, imageCoords[1]/camera.height, h - depthValue));
1027+
imageCloud.push_back(Eigen::Vector3f(imageCoords[0]/camera.width, imageCoords[1]/camera.height, h));
1028+
source.push_back(depthValue);
10291029
out << depthValue << "\t" << h << "\t" << imageCoords[0]/camera.width << "\t" << imageCoords[1]/camera.height <<"\n"; //red e green
10301030

10311031
}
1032+
10321033
}
10331034

10341035
GaussianGrid gaussianGrid;
1035-
gaussianGrid.init(differences);
1036+
gaussianGrid.init(imageCloud, source);
10361037
gaussianGrid.imageGrid(("test.png"));
10371038

10381039
float ortho_z = realToPixelCoord(0,0,z)[2];
10391040

1040-
for(int y=0; y< height/2; y++){
1041-
for(int x=0; x<width; x++){
1041+
for(size_t y=0; y < height; y++){
1042+
for(size_t x=0; x < width; x++){
10421043
//if(x,y )
10431044
Eigen::Vector3f r = pixelToRealCoordinates(x, y, ortho_z);
10441045
assert(fabs(z-r[2]) < 0.1f);
@@ -1048,10 +1049,10 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
10481049
int py = round(p[1]);
10491050

10501051
if(px >= 0 && px< camera.width && py >= 0 && py< camera.height){
1051-
float depthValue = z - camera.elevation[px + py * camera.width]* pixel_size;
1052+
float depthValue = camera.elevation[px + py * camera.width];
10521053
p[0] /= camera.width;
10531054
p[1] /= camera.height;
1054-
float h = gaussianGrid.value(p[0], p[1]) + depthValue;
1055+
float h = gaussianGrid.target(p[0], p[1], depthValue);
10551056
Eigen::Vector3f d = realToPixelCoord(r[0], r[1], h);
10561057

10571058
//p0 e p1 devono venire uguale e vedi se depth è ugusle, h dovrebbe venire simile
@@ -1068,23 +1069,51 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
10681069
cout << " d: " << d << endl;
10691070
}
10701071
elevation[x + y * width] = d[2];
1072+
} else {
1073+
//elevation[x + y*width] = origin[2] + resolution[2] * elevation[x + y*width];
1074+
}
1075+
}
1076+
}
1077+
}
1078+
void GaussianGrid::fitLinear(std::vector<float> &x, std::vector<float> &y, float &a, float &b) {
1079+
if (x.size() != y.size()) {
1080+
cout << "Errore: i vettori x e y devono avere la stessa lunghezza." << endl;
1081+
return;
1082+
}
10711083

1084+
int n = x.size();
1085+
float sum_x = 0.0, sum_y = 0.0, sum_xy = 0.0, sum_x2 = 0.0;
10721086

1073-
}
1087+
for (int i = 0; i < n; i++) {
1088+
sum_x += x[i];
1089+
sum_y += y[i];
1090+
sum_xy += x[i] * y[i];
1091+
sum_x2 += x[i] * x[i];
10741092
}
10751093

1076-
}
1094+
// Calcolo dei coefficienti a e b
1095+
a = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x);
1096+
b = (sum_y - a * sum_x) / n;
10771097
}
10781098

10791099
//fit h = a+b*elev
1080-
void GaussianGrid::init(std::vector<Eigen::Vector3f> &differences){
1081-
int side = static_cast<int>(sqrt(differences.size())) / 2;
1100+
void GaussianGrid::init(std::vector<Eigen::Vector3f> &cloud, std::vector<float> &source) {
1101+
int side = static_cast<int>(sqrt(cloud.size())) / 2;
10821102
sigma = 1.0f / side;
10831103
width = side;
10841104
height = side;
10851105
float precision = 0.00001f;
1106+
1107+
vector<float> cloudZ;
1108+
for(auto &v: cloud)
1109+
cloudZ.push_back(v[2]);
1110+
1111+
fitLinear(source, cloudZ, a, b);
10861112
//TODO: w and h proportional to aspect ratio of camera
1087-
computeGaussianWeightedGrid(differences);
1113+
for(size_t i = 0; i < cloud.size(); i++) {
1114+
cloud[i][2] -= depthmapToCloud(source[i]);
1115+
}
1116+
computeGaussianWeightedGrid(cloud);
10881117
fillLaplacian(precision);
10891118
}
10901119

@@ -1183,6 +1212,11 @@ float GaussianGrid::value(float x, float y){
11831212
return values[pixelX + pixelY * width];
11841213
}
11851214

1215+
float GaussianGrid::target(float x, float y, float h) {
1216+
h = depthmapToCloud(h);
1217+
return h - value(x, y);
1218+
}
1219+
11861220
void GaussianGrid::computeGaussianWeightedGrid(std::vector<Eigen::Vector3f> &differences) {
11871221

11881222
float x_min = 0;

depthmap/depthmap.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,21 @@ class GaussianGrid {
6666
std::vector<float> values;
6767
std::vector<float> weights;
6868
float sigma;
69+
float a, b;//coefficient of linear transform from source to point cloud space.
6970

70-
void init(std::vector<Eigen::Vector3f> &differences);
71+
void init(std::vector<Eigen::Vector3f> &cloud, std::vector<float> &source);
72+
void fitLinear(std::vector<float> &x, std::vector<float> &y, float &a, float &b); //ax + b
7173

7274
void computeGaussianWeightedGrid(std::vector<Eigen::Vector3f> &differences);
7375
void fillLaplacian(float precision);
7476
void imageGrid(const char* filename);
7577
//interpola la griglia per spostare la depthmap, serve per creare la funzione
7678
float value(float x, float y);
79+
float target(float x, float y, float source); //given a point in the source depthmap compute the z in cloud coordinate space;
80+
81+
float depthmapToCloud(float h) {
82+
return a*h + b;
83+
}
7784

7885
};
7986

depthmap/main.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ int main(int argc, char *argv[]) {
1515
return 1;
1616
}*/
1717
//input
18-
#define MACOS 1
18+
//#define MACOS 1
1919
#ifdef MACOS
2020
QString base = "/Users/erika/Desktop/";
2121
#else
22-
QString base = "/home/erika/";
22+
QString base = "";
2323
#endif
2424

25-
QString depthmapPath = base + "testcenterRel_copia/photogrammetry/Malt/Z_Num7_DeZoom4_STD-MALT.tif";
26-
QString cameraDepthmap = base + "testcenterRel_copia/datasets/L04C12_depth_rti.tiff";
27-
QString orientationXmlPath = base + "testcenterRel_copia/photogrammetry/Ori-Relative/Orientation-L04C12.tif.xml";
28-
QString maskPath = base + "testcenterRel_copia/photogrammetry/Malt/Masq_STD-MALT_DeZoom4.tif";
29-
QString plyFile = base +"testcenterRel_copia/photogrammetry/AperiCloud_Relative__mini.ply";
30-
QString point_txt = base + "testcenterRel_copia/photogrammetry/points_h.txt";
25+
QString depthmapPath = base + "Z_Num7_DeZoom4_STD-MALT.tif";
26+
QString cameraDepthmap = base + "L04C12_depth_rti.tiff";
27+
QString orientationXmlPath = base + "Orientation-L04C12.tif.xml";
28+
QString maskPath = base + "Masq_STD-MALT_DeZoom4.tif";
29+
QString plyFile = base +"AperiCloud_Relative__mini.ply";
30+
QString point_txt = base + "points_h.txt";
3131
Depthmap depth;
3232

3333
//output
34-
QString outputPath = base + "testcenterRel_copia/photogrammetry/depthmap_projectL05C13.png";
35-
QString output_mask = base + "testcenterRel_copia/photogrammetry/mask_test.tif";
36-
QString output_depth = base + "testcenterRel_copia/photogrammetry/depth_test.tif";
37-
QString output_points = base + "testcenterRel_copia/photogrammetry/points_h.txt";
38-
QString output_grid = base + "testcenterRel_copia/photogrammetry/grid.png";
34+
QString outputPath = base + "tdepthmap_projectL05C13.png";
35+
QString output_mask = base + "mask_test.tif";
36+
QString output_depth = base + "depth_test.tif";
37+
QString output_points = base + "points_h.txt";
38+
QString output_grid = base + "tgrid.png";
3939

4040
OrthoDepthmap ortho;
4141

@@ -66,7 +66,7 @@ int main(int argc, char *argv[]) {
6666
return -1;
6767
}
6868
ortho.integratedCamera(depthCam, qPrintable(output_points));
69-
ortho.saveDepth(qPrintable(base + "testcenterRel_copia/photogrammetry/testDepth.tiff"));
69+
ortho.saveDepth(qPrintable(base + "testDepth.tiff"));
7070
// sqrt
7171

7272

0 commit comments

Comments
 (0)