7
7
#include " depthmap.h"
8
8
#include " ../src/bni_normal_integration.h"
9
9
#include < QFile>
10
+ #include < QDebug>
10
11
#include < QRegularExpression>
11
12
#include < fstream>
12
13
@@ -252,7 +253,7 @@ bool Depthmap::loadTiff(const char *tiff, vector<float> &values, uint32_t &w, ui
252
253
// Check if the TIFF is tiled
253
254
uint32_t tileWidth, tileLength;
254
255
if (!TIFFGetField (inTiff, TIFFTAG_TILEWIDTH, &tileWidth) ||
255
- !TIFFGetField (inTiff, TIFFTAG_TILELENGTH, &tileLength)) {
256
+ !TIFFGetField (inTiff, TIFFTAG_TILELENGTH, &tileLength)) {
256
257
return loadStripedTiff (inTiff, values, w, h, bitsPerSample);
257
258
} else {
258
259
return loadTiledTiff (inTiff, values, w, h, tileWidth, tileLength, bitsPerSample);
@@ -452,10 +453,10 @@ bool Depthmap::loadNormals(const char *normals_path){
452
453
int i = x + y * width;
453
454
454
455
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
+ );
459
460
}
460
461
}
461
462
@@ -693,7 +694,6 @@ void Depthmap::resizeNormals (int factorPowerOfTwo, int step) {
693
694
width = targetWidth;
694
695
height = targetHeight;
695
696
696
- // QString filename = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/surface.jpg";
697
697
// saveNormals(filename.toStdString().c_str());
698
698
// depthIntegrateNormals();
699
699
@@ -774,7 +774,7 @@ bool OrthoDepthmap::loadXml(const char *xmlPath){
774
774
775
775
776
776
if (originePlaniNodes.isEmpty () || resolutionPlaniNodes.isEmpty () ||
777
- origineAltiNodes.isEmpty () || resolutionAltiNodes.isEmpty ()) {
777
+ origineAltiNodes.isEmpty () || resolutionAltiNodes.isEmpty ()) {
778
778
cerr << " OriginePlani, ResolutionPlani, OrigineAlti, or ResolutionAlti not found in XML." << endl;
779
779
return false ;
780
780
@@ -980,9 +980,9 @@ void OrthoDepthmap::verifyPointCloud(){
980
980
<< "Coordinate pixel: (" << pixelX << ", " << pixelY << " " << elevation[pixelX + pixelY * width]<< ")\n";
981
981
*/
982
982
}
983
+ }
983
984
984
985
985
- }
986
986
void OrthoDepthmap::integratedCamera (const CameraDepthmap& camera, const char *outputFile){
987
987
988
988
QFile outFile (outputFile);
@@ -994,11 +994,11 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
994
994
float z = point_cloud[0 ][2 ];
995
995
auto o = camera.camera .projectionToImage (Eigen::Vector3f (0 , 0 , z));
996
996
auto u = camera.camera .projectionToImage (Eigen::Vector3f (1 , 0 , z));
997
- float pixel_size = 1 / (u[0 ] - o[0 ]);
998
-
999
997
1000
998
QTextStream out (&outFile);
1001
- std::vector<Eigen::Vector3f> differences;
999
+ std::vector<Eigen::Vector3f> imageCloud;
1000
+ std::vector<float > source;
1001
+
1002
1002
for (size_t i = 0 ; i < point_cloud.size (); i++) {
1003
1003
1004
1004
Eigen::Vector3f& realCoord = point_cloud[i];
@@ -1012,8 +1012,7 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
1012
1012
1013
1013
1014
1014
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 ];
1017
1016
if (fabs (pixelCoord[0 ] - 170 ) < 3 && fabs (pixelCoord[1 ] - 150 ) < 3 ){
1018
1017
cout << " elevation: " << elevation[int (pixelCoord[0 ]) + int (pixelCoord[1 ]) *width] << endl;
1019
1018
@@ -1025,20 +1024,22 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
1025
1024
}
1026
1025
1027
1026
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);
1029
1029
out << depthValue << " \t " << h << " \t " << imageCoords[0 ]/camera.width << " \t " << imageCoords[1 ]/camera.height <<" \n " ; // red e green
1030
1030
1031
1031
}
1032
+
1032
1033
}
1033
1034
1034
1035
GaussianGrid gaussianGrid;
1035
- gaussianGrid.init (differences );
1036
+ gaussianGrid.init (imageCloud, source );
1036
1037
gaussianGrid.imageGrid ((" test.png" ));
1037
1038
1038
1039
float ortho_z = realToPixelCoord (0 ,0 ,z)[2 ];
1039
1040
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++){
1042
1043
// if(x,y )
1043
1044
Eigen::Vector3f r = pixelToRealCoordinates (x, y, ortho_z);
1044
1045
assert (fabs (z-r[2 ]) < 0 .1f );
@@ -1048,10 +1049,10 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
1048
1049
int py = round (p[1 ]);
1049
1050
1050
1051
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 ];
1052
1053
p[0 ] /= camera.width ;
1053
1054
p[1 ] /= camera.height ;
1054
- float h = gaussianGrid.value (p[0 ], p[1 ]) + depthValue;
1055
+ float h = gaussianGrid.target (p[0 ], p[1 ], depthValue) ;
1055
1056
Eigen::Vector3f d = realToPixelCoord (r[0 ], r[1 ], h);
1056
1057
1057
1058
// 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
1068
1069
cout << " d: " << d << endl;
1069
1070
}
1070
1071
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
+ }
1071
1083
1084
+ int n = x.size ();
1085
+ float sum_x = 0.0 , sum_y = 0.0 , sum_xy = 0.0 , sum_x2 = 0.0 ;
1072
1086
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];
1074
1092
}
1075
1093
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;
1077
1097
}
1078
1098
1079
1099
// 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 ;
1082
1102
sigma = 1 .0f / side;
1083
1103
width = side;
1084
1104
height = side;
1085
1105
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);
1086
1112
// 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);
1088
1117
fillLaplacian (precision);
1089
1118
}
1090
1119
@@ -1183,6 +1212,11 @@ float GaussianGrid::value(float x, float y){
1183
1212
return values[pixelX + pixelY * width];
1184
1213
}
1185
1214
1215
+ float GaussianGrid::target (float x, float y, float h) {
1216
+ h = depthmapToCloud (h);
1217
+ return h - value (x, y);
1218
+ }
1219
+
1186
1220
void GaussianGrid::computeGaussianWeightedGrid (std::vector<Eigen::Vector3f> &differences) {
1187
1221
1188
1222
float x_min = 0 ;
0 commit comments