Skip to content

Commit

Permalink
added functions for pointCloud and integrated camera
Browse files Browse the repository at this point in the history
  • Loading branch information
Erika committed Jan 10, 2025
1 parent b46eea3 commit 8ccca38
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 46 deletions.
194 changes: 163 additions & 31 deletions depthmap/depthmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,55 +698,56 @@ void Depthmap::resizeNormals (int factorPowerOfTwo, int step) {
//depthIntegrateNormals();

}
void Depthmap::loadPointCloud(const char *textPath, std::vector<float> &points){

//apri file di testo e scarta tutto quello che non è un numero. fai un char vettore di punti
QFile file(textPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
throw QString("Error opening input file: ")+ textPath;
}

QTextStream in(&file);
/*
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
QStringList parts = line.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
if (line.isEmpty() || (!line[0].isDigit() && line[0] != '-' && line[0] != '+') || parts.size() != 6) {
continue;
}
void Depthmap::depth(const char *depth_path){
std::vector<float> depthValues;
if (!load(depth_path)) {
cerr << "Failed to load depth map." << endl;
return;
}
for (size_t i = 0; i < depthValues.size(); i++) {
float realX = depthValues[i];
float realY = depthValues[++i];
float realZ = depthValues[++i];
//check the line if the line not begin with float number break
bool isValid = true;
float v[3];
for (int i = 0; i < 3; ++i) {
bool isNumber = false;
v[i] = parts[i].toFloat(&isNumber);
if (!isNumber) {
isValid = false;
break;
}
}
if (!isValid)
throw QString("Invalide ply");
Eigen::Vector3f pixelCoords = realToPixelCoord(realX, realY, realZ);
int pixelX = pixelCoords[0];
int pixelY = pixelCoords[1];
float calcZ = pixelCoords[2];
Eigen::Vector3f = projectToCameraDepthMap(pixelCoord);
for (int i = 0; i < 3; ++i){
points.push_back(v[i]);
}
}
}
//funzione inversa da 3d a xyh; xy z=0 h? pixel,pixel, h
void Depthmap::t3DToCamera(P){
void Depthmap::t3DToCamera(float h, ){
//xyz coord 3d fai l'inversa: aperi txt, x e y. prendi funz 3d la trosformi in x y e h e poi applichi quella già fatta camerato3d e deve venire uguale
//poi prendi apericloud e vedi se coincide con la depth
//2. scrivi la funzione inversa da un punto in 3d ci trova la x e la y in pixel e la h dalla depth del mic mac
//3. verifica se la h trovata per i punti di aperi corrisponde all h della depth map
for (size_t i = 0; i < points.size(); i += 3) {
float realX = points[i];
float realY = points[i + 1];
float realZ = points[i + 2];
}
Eigen::Vector3f pixelCoords = realToPixelCoordinates(realX, realY, realZ);
int pixelX = pixelCoords[0];
int pixelY = pixelCoords[1];
float calcZ = pixelCoords[2];
float invH= (h - origin[2]) / resolution[2];
}
}
*/
//take the x=160,14 e y=140
//the coordinates of the pixel step 0.016 are in the depth map xml Z_num etc.

Expand Down Expand Up @@ -897,14 +898,145 @@ void OrthoDepthmap::projectToCameraDepthMap(const Camera& camera, const QString&
}
depthMapImage.save(outputPath, "png");
}

void OrthoDepthmap::resizeNormals (int factorPowerOfTwo, int step) {
int factor = 1 << factorPowerOfTwo;
Depthmap::resizeNormals(factorPowerOfTwo, step);
resolution *= factor;
origin /= factor;
}

void OrthoDepthmap::loadPointCloud(const char *textPath){

//apri file di testo e scarta tutto quello che non è un numero. fai un char vettore di punti
QFile file(textPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
throw QString("Error opening input file: ")+ textPath;
}

QTextStream in(&file);


while (!in.atEnd()) {
QString line = in.readLine().trimmed();
QStringList parts = line.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
if (line.isEmpty() || (!line[0].isDigit() && line[0] != '-' && line[0] != '+') || parts.size() != 6) {
continue;
}

//check the line if the line not begin with float number break
bool isValid = true;
Eigen::Vector3f v;
for (int i = 0; i < 3; ++i) {
bool isNumber = false;
v[i] = parts[i].toFloat(&isNumber);
if (!isNumber) {
isValid = false;
break;
}
}
if (!isValid)
throw QString("Invalide ply");

point_cloud.push_back(v);

}


}

Eigen::Vector3f OrthoDepthmap::realToPixelCoord(float realX, float realY, float realZ){
float pixelX = (realX - origin[0]) / resolution[0];
float pixelY = (realY - origin[1]) / resolution[1];
float h = (realZ - origin[2]) / resolution[2];
return Eigen::Vector3f(pixelX, pixelY, h);

}

void OrthoDepthmap::verifyPointCloud(){

for(const auto& point : point_cloud){

float realX = point[0];
float realY = point[1];
float realZ = point[2];

Eigen::Vector3f pixelCoord = realToPixelCoord(realX, realY, realZ);

Eigen::Vector3f realCoord = pixelToRealCoordinates(pixelCoord[0], pixelCoord[1], pixelCoord[2]);


// int pixelX = static_cast<int>(round(pixelCoord[0]));
// int pixelY = static_cast<int>(round(pixelCoord[1]));

int pixelX = static_cast<int>(round(pixelCoord[0]));
int pixelY = static_cast<int>(round(pixelCoord[1]));

if (pixelX < 0 || pixelX >= width || pixelY < 0 || pixelY >= height) {
continue;
}
cerr << "point inside the image limits "
<< "Point 3D: (" << realX << ", " << realY << ", " << pixelCoord[2] << "), "
<< "Coordinate pixel: (" << pixelX << ", " << pixelY << " " << elevation[pixelX + pixelY * width]<< ")\n";

}


}
void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera){
int count =0;
for (size_t i = 0; i < point_cloud.size(); i++) {

Eigen::Vector3f& realCoord = point_cloud[i];
Eigen::Vector3f imageCoords = camera.camera.projectionToImage(realCoord);
Eigen::Vector3f pixelCoord = realToPixelCoord(realCoord[0], realCoord[1], realCoord[2]);

int pixelX = static_cast<int>(round(imageCoords[0]));
int pixelY = static_cast<int>(round(imageCoords[1]));

if (pixelX >= 0 && pixelX < camera.width && pixelY >= 0 && pixelY < camera.height) {
float depthValue = elevation[pixelX + pixelY * camera.width];
float h = pixelCoord[2];
count++;
cout << depthValue << h << endl;


}
}
cout << count << endl;
}
//fit h = a+b*elev

void OrthoDepthmap::fitLinearRegressionFromPairs() {
float sumElevation = 0; // Somma delle elevazioni (x)
float sumH = 0; // Somma dei valori h (y)
float sumElevationH = 0; // Somma delle moltiplicazioni elevation * h
float sumElevationSquared = 0; // Somma delle elevazioni al quadrato
size_t n = point_cloud.size(); // Numero di punti nella nuvola di punti

// Ciclo sui punti per raccogliere i dati
for (const auto& point : point_cloud) {
float elevation = point[0]; // Elevation (x)
float h = point[2]; // Altezza (y)

sumElevation += elevation;
sumH += h;
sumElevationH += elevation * h;
sumElevationSquared += elevation * elevation;
}

float b = (n * sumElevationH - sumElevation * sumH) / (n * sumElevationSquared - sumElevation * sumElevation);
float a = (sumH - b * sumElevation) / n;

for (const auto& point : point_cloud) {
float elevation = point[0];
float hCalculated = a + b * elevation;
cout << "For elevation:" << elevation << "calculated h:" << hCalculated << endl;
}

}
//Real to Pixel Coordinates: (-0.981, 2.041, -11.132) -> Pixel: (110.688, 65.4375, -27.75)
//point outside the image limits Point 3D: (-0.981, 2.041, -11.132), Coordinate pixel: (-1, 2)


/* ---------------------------------------------------------------------------------------------------------------------------*/
Expand Down
14 changes: 13 additions & 1 deletion depthmap/depthmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class Depthmap {
bool loadDepth(const char *depth_path);
bool loadMask(const char *mask_path);
bool loadNormals(const char *normals_path);
bool loadText(const char *textPath, const char *outputPath);
void saveDepth(const char *depth_path);
void saveMask(const char *depth_path);
void saveNormals(const char *normals_path);
Expand All @@ -65,6 +64,7 @@ class CameraDepthmap:
public Depthmap {
public:
Camera camera;

/*1. load tif filename, convertita in vector3f
*
*/
Expand All @@ -75,13 +75,25 @@ class OrthoDepthmap:
public:
Eigen::Vector3f resolution;
Eigen::Vector3f origin;
std::vector<Eigen::Vector3f> point_cloud;


Eigen::Vector3f pixelToRealCoordinates(int pixelX, int pixelY, float pixelZ);
Eigen::Vector3f realToPixelCoord(float realX, float realY, float realZ);
bool load(const char *depth_path, const char *mask_path);
bool loadXml(const char *xmlPath);
void saveObj(const char *filename);
void projectToCameraDepthMap(const Camera& camera, const QString& outputPath);
void resizeNormals(int factorPowerOfTwo, int step = 1);
void loadPointCloud(const char *textPath);
//itera sui punti, chiama l'inversa, prima converte a intero perche sono float vede se xy stanno in w e h, se non dentro problema
//legge nella depth l h corrispondente
void verifyPointCloud();
void integratedCamera(const CameraDepthmap& camera);
void fitLinearRegressionFromPairs();





/*1.
Expand Down
40 changes: 26 additions & 14 deletions depthmap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main(int argc, char *argv[]) {
return 1;
}*/
//input
//#define MACOS 1
#define MACOS 1
#ifdef MACOS
QString base = "/Users/erika/Desktop/";
#else
Expand All @@ -24,14 +24,13 @@ int main(int argc, char *argv[]) {
QString depthmapPath = base + "testcenterRel_copia/photogrammetry/Malt/Z_Num7_DeZoom4_STD-MALT.tif";
QString orientationXmlPath = base + "testcenterRel_copia/photogrammetry/Ori-Relative/Orientation-L05C12.tif.xml";
QString maskPath = base + "testcenterRel_copia/photogrammetry/Malt/Masq_STD-MALT_DeZoom4.tif";
QString plyFile = base +"testcenterRel_copia/photogrammetry/AperiCloud_Relative_mini.ply";
QString plyFile = base +"testcenterRel_copia/photogrammetry/AperiCloud_Relative__mini.ply";
Depthmap depth;

//output
QString outputPath = base + "testcenterRel_copia/photogrammetry/depthmap_projectL05C13.png";
QString output_mask = base + "testcenterRel_copia/photogrammetry/mask_test.tif";
QString output_depth = base + "testcenterRel_copia/photogrammetry/depth_test.tif";
QString output_text = base + "testcenterRel_copia/photogrammetry/point.txt";

OrthoDepthmap ortho;

Expand All @@ -42,34 +41,47 @@ int main(int argc, char *argv[]) {
//ortho.computeNormals();
//ortho.saveNormals(qPrintable(base + "testcenterRel_copia/photogrammetry/original.png"));
//ortho.saveObj(qPrintable(base + "testcenterRel_copia/photogrammetry/original.obj"));

ortho.saveDepth(qPrintable(output_depth));
ortho.saveMask(qPrintable(output_mask));
ortho.loadText(qPrintable(plyFile), qPrintable(output_text));
try{
ortho.saveDepth(qPrintable(output_depth));
ortho.saveMask(qPrintable(output_mask));
ortho.loadPointCloud(qPrintable(plyFile));
} catch(QString e){
cout << qPrintable(e) << endl;
exit(-1);
}
ortho.verifyPointCloud();
ortho.fitLinearRegressionFromPairs();

Camera camera;
camera.loadXml(orientationXmlPath);
CameraDepthmap depthCam;
//xml per camera e tiff per la depth map
depthCam.camera.loadXml(orientationXmlPath);
depthCam.loadDepth(qPrintable(depthmapPath));
ortho.integratedCamera(depthCam);


//int pixelX = 165;
//int pixelY = 144;
//float pixelZ = 4.5;
ortho.projectToCameraDepthMap(camera, outputPath);
Eigen::Matrix3f rotationMatrix;
Eigen::Vector3f center;

// depth.loadMask(qPrintable(maskPath));
// depth.saveDepth(qPrintable(depthmapPath));
// depth.saveMask(qPrintable(maskPath));
// depth.loadMask(qPrintable(maskPath));
// depth.saveDepth(qPrintable(depthmapPath));
// depth.saveMask(qPrintable(maskPath));
//QString maskObjPath = base + "testcenterRel_copia/photogrammetry/mask.obj";
// ortho.saveObj(qPrintable(base + "testcenterRel_copia/photogrammetry/depthmap_projectL05C13.obj"));
ortho.saveObj(qPrintable(base + "testcenterRel_copia/photogrammetry/depthmap_projectL05C13.obj"));


//depth.depthIntegrateNormals();
// ortho.saveObj(qPrintable(base + "testcenterRel_copia/photogrammetry/integrated.obj"));
// ortho.saveObj(qPrintable(base + "testcenterRel_copia/photogrammetry/integrated.obj"));




// modifica maschere e depth map per migliorare la depth
// modifica maschere e depth map per migliorare la depth

/*1. test prendi varie masq e modificale in maniera consistente: 1. masq depth 2. masq per ogni camera, cosa modifico?
tawny usa la masq della depth da rti a ortho piano, quelle per camera le usa quando fa l'orthopiano mosaico
Expand Down Expand Up @@ -140,7 +152,7 @@ lineare per la serie di coppie per trovare a e b). Pesa i punti.
//cout << "Coordinate immagine: (" << imageCoords[0] << ", " << imageCoords[1] << ")" << endl;


//d = (h + zerolevel) *f scala
//d = (h + zerolevel) *f scala
return 0;
}

0 comments on commit 8ccca38

Please sign in to comment.