Skip to content

Commit 1655afd

Browse files
author
Erika
committed
modify depth functions with issues
1 parent 2fcf01f commit 1655afd

File tree

3 files changed

+109
-31
lines changed

3 files changed

+109
-31
lines changed

depthmap/depthmap.cpp

+91-30
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

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

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+
10021036
for (size_t i = 0; i < point_cloud.size(); i++) {
10031037

10041038
Eigen::Vector3f realCoord = point_cloud[i];
@@ -1023,14 +1057,19 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
10231057
}
10241058

10251059
GaussianGrid gaussianGrid;
1060+
gaussianGrid.minSamples = 3; // 1, 3, 5
1061+
gaussianGrid.sideFactor = 1; // 0.25, 0.5, 1
10261062
gaussianGrid.init(imageCloud, source);
10271063
gaussianGrid.imageGrid(("test.png"));
10281064

1065+
static float c = 1.0f;
10291066
float ortho_z = realToPixelCoord(0,0,z)[2];
10301067

10311068
for(size_t y=0; y < height; y++){
10321069
for(size_t x=0; x < width; x++){
1033-
//if(x,y )
1070+
if(mask[x + y * width] != 0.0f){
1071+
continue;
1072+
}
10341073
Eigen::Vector3f r = pixelToRealCoordinates(x, y, ortho_z);
10351074
assert(fabs(z-r[2]) < 0.1f);
10361075
Eigen::Vector3f p = camera.camera.projectionToImage(r);
@@ -1046,16 +1085,19 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
10461085
Eigen::Vector3f d = realToPixelCoord(r[0], r[1], h);
10471086

10481087
float w = camera.calculateWeight(px, py);
1049-
10501088
//p0 e p1 devono venire uguale e vedi se depth è ugusle, h dovrebbe venire simile
1089+
10511090
elevation[x + y * width] += w * d[2];
1052-
mask[x+ y * width] += w;
1053-
} else {
1091+
1092+
weights[x+ y * width] += w;
1093+
1094+
}else {
10541095
//elevation[x + y*width] = origin[2] + resolution[2] * elevation[x + y*width];
10551096
}
10561097
}
10571098
}
1058-
saveObj("testElev.obj");
1099+
1100+
10591101
}
10601102
/*_-----------------------------------------------------------------------------------------*/
10611103
void GaussianGrid::fitLinear(std::vector<float> &x, std::vector<float> &y, float &a, float &b) {
@@ -1107,7 +1149,7 @@ float GaussianGrid::bilinearInterpolation(float x, float y) {
11071149
}
11081150
//fit h = a+b*elev
11091151
void GaussianGrid::init(std::vector<Eigen::Vector3f> &cloud, std::vector<float> &source) {
1110-
int side = static_cast<int>(sqrt(cloud.size())) / 2;
1152+
int side = static_cast<int>(sideFactor * sqrt(cloud.size()));
11111153
sigma = 1.0f / side;
11121154
width = side;
11131155
height = side;
@@ -1220,7 +1262,7 @@ float GaussianGrid::value(float x, float y){
12201262
float pixelX = x * (width-1);
12211263
float pixelY = y * (height-1);
12221264

1223-
return bilinearInterpolation(pixelX, pixelY);
1265+
return bilinearInterpolation(pixelX, pixelY);
12241266

12251267
//return values[pixelX + pixelY * width];
12261268

@@ -1247,7 +1289,7 @@ void GaussianGrid::computeGaussianWeightedGrid(std::vector<Eigen::Vector3f> &dif
12471289
std::vector<int> count(width*height, 0);
12481290

12491291

1250-
float max_distance = 1.2 * sigma;
1292+
float max_distance = 2 * sigma;
12511293
for (auto &p : differences) {
12521294

12531295

@@ -1271,14 +1313,14 @@ void GaussianGrid::computeGaussianWeightedGrid(std::vector<Eigen::Vector3f> &dif
12711313
}
12721314
}
12731315
}
1274-
//chiama camere tutte e 4 e vedi come vengono
1316+
//chiama camere tutte e 4 e vedi come vengono
12751317
// pesare per il blanding funzione intervallo 0, 1 * 0, 1 0 ai bordi 1 al centro, che sia una funzione continua
12761318
//polinomio di 2 grado in x * pol 2 grado in y. derivata e peso a 0 sul bordo
12771319
//fai somma pesata e veedi come vieni
12781320
//funz target ritorna valore e peso
12791321

12801322
for (int i = 0; i < values.size(); i++) {
1281-
if(count[i] < 3)
1323+
if(count[i] < minSamples)
12821324
weights[i] = 0;
12831325
if (weights[i] != 0) {
12841326
values[i] /= (weights[i]);
@@ -1290,29 +1332,48 @@ void GaussianGrid::computeGaussianWeightedGrid(std::vector<Eigen::Vector3f> &dif
12901332
}
12911333
}*/
12921334
}
1293-
//
1294-
float Depthmap::calculateWeight(float x, float y) const{
1335+
//se < 1/4 è -8x^2, else se è < 3/4. 8*(x-1)^2, data una x mi ritorna una x+1
1336+
1337+
static float bell(float x){
1338+
if(x < 0.25f){
1339+
return 8.0f* x * x;
1340+
}
1341+
else if(x< 0.75f){
1342+
return -8.0f * x *x + 8.0f * x-1.0f;
1343+
}
1344+
else {
1345+
x = 1.0f - x;
1346+
return 8.0f * x * x;
1347+
}
12951348

1296-
x/=width;
1297-
y/= height;
1349+
}
12981350

1299-
float weightX =pow(cos(M_PI * (x-0.5f)), 2);
1300-
float weightY = pow(cos(M_PI * (y-0.5f)), 2);
1351+
float Depthmap::calculateWeight(float x, float y) const{
13011352

1302-
return weightX * weightY;
1353+
x /= width;
1354+
y /= height;
1355+
1356+
// float weightX =pow(cos(M_PI * (x-0.5f)), 2);
1357+
//float weightY = pow(cos(M_PI * (y-0.5f)), 2);
1358+
return bell(x)*bell(y);
1359+
//return weightX * weightY;
13031360
}
13041361
void OrthoDepthmap::beginIntegration(){
1305-
elevation.clear();
1306-
elevation.resize(width * height, 0);
1307-
mask.clear();
1308-
mask.resize(width * height, 0);
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);
13091371

13101372
}
13111373
void OrthoDepthmap::endIntegration(){
13121374
for(size_t i =0; i < elevation.size(); i++){
1313-
if(mask[i]){
1314-
elevation[i] /= mask[i];
1315-
}
1375+
//if(mask[i] == 0.0f) {// && weights[i] != 0.0f){
1376+
elevation[i] =4; ///= weights[i];
13161377
}
13171378
}
13181379

depthmap/depthmap.h

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class Depthmap {
3131
uint32_t width, height;
3232
std::vector<float> elevation;
3333
std::vector<float> mask;
34+
std::vector<float> weights;
35+
3436
std::vector<Eigen::Vector3f> normals;
3537

3638

@@ -66,6 +68,8 @@ class GaussianGrid {
6668
int width, height;
6769
std::vector<float> values;
6870
std::vector<float> weights;
71+
float sideFactor = 0.5f; // corrective factor over the 1/sqrt(n) formula.
72+
int minSamples = 3; // minimum number of samples needed in a pixel
6973
float sigma;
7074
float a, b;//coefficient of linear transform from source to point cloud space.
7175

depthmap/main.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ int main(int argc, char *argv[]) {
4141
QString output_points = base + "points_h.txt";
4242
QString output_grid = base + "tgrid.png";
4343

44+
4445
OrthoDepthmap ortho;
4546

4647
if(!ortho.load(qPrintable(depthmapPath), qPrintable(maskPath))){
@@ -65,6 +66,9 @@ int main(int argc, char *argv[]) {
6566
return -1;
6667
}
6768

69+
//doortho = 1 domec =0;
70+
71+
6872
//ortho.computeNormals();
6973
//ortho.saveNormals(qPrintable(base + "testcenterRel_copia/photogrammetry/original.png"));
7074
//ortho.saveObj(qPrintable(base + "testcenterRel_copia/photogrammetry/original.obj"));
@@ -80,6 +84,10 @@ int main(int argc, char *argv[]) {
8084
ortho.verifyPointCloud();
8185
ortho.beginIntegration();
8286

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;
8391

8492
for (const QFileInfo &tiffFile : tiffFiles) {
8593

@@ -114,7 +122,12 @@ int main(int argc, char *argv[]) {
114122

115123
}
116124
ortho.endIntegration();
117-
ortho.saveDepth(qPrintable("final_depth.tif"));
125+
ortho.saveDepth(qPrintable(depthmapPath));
126+
ortho.saveMask(qPrintable(maskPath));
127+
ortho.saveObj("weightsElev2.obj");
128+
129+
130+
118131

119132

120133
//depthCam.camera.loadXml(orientationXmlPath);

0 commit comments

Comments
 (0)