Skip to content

Commit

Permalink
new projected depth map
Browse files Browse the repository at this point in the history
  • Loading branch information
Erika committed Dec 3, 2024
1 parent bef3029 commit 4a532de
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 25 deletions.
60 changes: 51 additions & 9 deletions depthmap/depthmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,53 @@ void Depthmap::computeNormals() {
}
}
}

void Depthmap::projectToCameraDepthMap(const Camera& camera, const QString& outputPath) {

QImage depthMapImage(camera.width, camera.height, QImage::Format_RGB888);
depthMapImage.fill(qRgb(0, 0, 0));
//find the minimum and maximum for the Z coordinates
float minZ = std::numeric_limits<float>::max();
float maxZ = std::numeric_limits<float>::lowest();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float pixelZ = elevation[x + y * width];

Eigen::Vector3f realCoordinates = pixelToRealCoordinates(x, y, pixelZ);
Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);
if(pixelZ > 0){
minZ = std::min(minZ, pixelZ);
maxZ = std::max(maxZ, pixelZ);
}
}
}
if (minZ >= maxZ) {
qWarning("MinZ and MaxZ invalid. Skip depth map generation.");
return;
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float pixelZ = elevation[x + y * width];

if (pixelZ <= 0) continue;
int pixelValue = (int)round(((pixelZ - minZ) / (maxZ - minZ)) * 255);
pixelValue = std::min(std::max(pixelValue, 0), 255);

Eigen::Vector3f realCoordinates = pixelToRealCoordinates(x, y, pixelZ);
Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);

int imageX = (int)round(imageCoords[0]);
int imageY= (int)round(imageCoords[1]);
if (imageX >= 0 && imageX < camera.width && imageY >= 0 && imageY < camera.height) {
depthMapImage.setPixel(imageX, imageY, qRgb(pixelValue, pixelValue, pixelValue));
//cout << "Pixel projected (" << x << ", " << y << ") -> (" << imageX << ", " << imageY << "), Z = "
// << pixelZ << ", pixelValue = " << pixelValue << endl;
}
}
}
depthMapImage.save(outputPath, "png");
}

void Depthmap::depthIntegrateNormals(){
if (normals.empty()){
cerr << "Error: no normals found" << endl;
Expand Down Expand Up @@ -284,7 +331,7 @@ void Depthmap::resizeNormals (int factorPowerOfTwo, int step = 1) {

//QString filename = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/surface.jpg";
//saveNormals(filename.toStdString().c_str());
//chiama l'integrale integra le normali e salva il ply
//chiama l'integrale integra le normali e salva il ply
//depthIntegrateNormals();
//QString plyFilename = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/resize_normals.obj";
//saveObj(plyFilename.toStdString().c_str());
Expand Down Expand Up @@ -325,6 +372,7 @@ void Depthmap::saveObj(const char *filename){
}
}
}

//prendi x=160,14 e y=140 dell'img
//le coordinate del passo del pixel 0,016 sono nel xml della depth map Z_num ecc.

Expand Down Expand Up @@ -488,7 +536,7 @@ bool Camera::loadInternParameters(const QString &internePath){
// ritorna pixel x e y img di l12 ori coord di pixel devono finire nell'ori-rel
// Pc = Rk(Pg − Ok)
// Pg = Ground point Pc = point camera. x y z orientati come la camera, moltiplica la matrice. Poi fai la proiezione.
Eigen::Vector3f Camera::projectionToImage(Eigen::Vector3f realPosition){
Eigen::Vector3f Camera::projectionToImage(Eigen::Vector3f realPosition) const{
//centre origine
//r matrice
//matrice r inversa rotation
Expand All @@ -513,7 +561,7 @@ Eigen::Vector3f Camera::projectionToImage(Eigen::Vector3f realPosition){

}

Eigen::Vector3f Camera::applyIntrinsicCalibration(Eigen::Vector3f& uvz) {
Eigen::Vector3f Camera::applyIntrinsicCalibration(Eigen::Vector3f& uvz) const{

float u = uvz.x();
float v = uvz.y();
Expand All @@ -539,9 +587,3 @@ Eigen::Vector3f Camera::applyRadialDistortion(Eigen::Vector3f& uvz) {

return Eigen::Vector3f(u_dist, v_dist, uvz.z());
}






5 changes: 3 additions & 2 deletions depthmap/depthmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class Camera {
Eigen::Vector3f center;
bool loadXml(const QString &path); //read the MicMac xml origin, origin resolution ecc.
bool loadInternParameters(const QString &internePath); // read the xml with the center, rotation, focal parameter, principal points parameters ecc.
Eigen::Vector3f projectionToImage(Eigen::Vector3f realPosition);
Eigen::Vector3f projectionToImage(Eigen::Vector3f realPosition) const;
Eigen::Vector3f applyRadialDistortion(Eigen::Vector3f& u);
Eigen::Vector3f applyIntrinsicCalibration(Eigen::Vector3f& u);
Eigen::Vector3f applyIntrinsicCalibration(Eigen::Vector3f& u) const;

Camera() {}
};
Expand All @@ -44,6 +44,7 @@ class Depthmap {
void saveObj(const char *filename);
void depthIntegrateNormals();
void resizeNormals(int factorPowerOfTwo, int step);
void projectToCameraDepthMap(const Camera& camera, const QString& outputPath);
// void getOrientationVector(const QString &xmlPath, Eigen::Matrix3f &rotation, Eigen::Vector3f &center);

};
Expand Down
31 changes: 17 additions & 14 deletions depthmap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ int main(int argc, char *argv[]) {

QString depthmapPath = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/Malt/Z_Num7_DeZoom4_STD-MALT.tif";
QString orientationXmlPath = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/Ori-Relative/Orientation-L05C12.tif.xml";
QString output = "out.png";
Depthmap depth;
depth.load(qPrintable(depthmapPath));
depth.computeNormals();

depth.saveNormals("/Users/erika/Desktop/testcenterRel_copia/photogrammetry/original.obj");
depth.saveObj("/Users/erika/Desktop/testcenterRel_copia/photogrammetry/original.obj");

Expand All @@ -32,33 +32,36 @@ int main(int argc, char *argv[]) {
depth.saveNormals("/Users/erika/Desktop/testcenterRel_copia/photogrammetry/resized_integrated.jpg");
depth.saveObj("/Users/erika/Desktop/testcenterRel_copia/photogrammetry/resized_integrated.obj");

QString outputPath = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/depthmap_project.png";



Camera camera;
camera.loadXml(orientationXmlPath);
int pixelX = 165;
int pixelY = 144;
float pixelZ = 4.5;

//int pixelX = 165;
//int pixelY = 144;
//float pixelZ = 4.5;
depth.projectToCameraDepthMap(camera, outputPath);

Eigen::Matrix3f rotationMatrix;
Eigen::Vector3f center;

//depth.getOrientationVector(orientationXmlPath, rotationMatrix, center);
Eigen::Vector3f realCoordinates = depth.pixelToRealCoordinates(pixelX, pixelY, pixelZ);
realCoordinates[0] = -0.1216933;
realCoordinates[1] = 0.7094725;
realCoordinates[2] = -10.4198828;
Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);
//Eigen::Vector3f realCoordinates = depth.pixelToRealCoordinates(pixelX, pixelY, pixelZ);
//realCoordinates[0] = -0.1216933;
//realCoordinates[1] = 0.7094725;
//realCoordinates[2] = -10.4198828;
//Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);

cout << "Rotation matrix:"<< endl << camera.rotation << endl;
cout << "Central vector: (" << camera.center << endl;
cout << "Real coordinates: (" << realCoordinates[0] << ", "
<< realCoordinates[1] << ", " << realCoordinates[2] << ")" << endl;

cout << "Coordinate immagine: (" << imageCoords[0] << ", " << imageCoords[1] << ")" << endl;
//cout << "Real coordinates: (" << realCoordinates[0] << ", "
// << realCoordinates[1] << ", " << realCoordinates[2] << ")" << endl;

//cout << "Coordinate immagine: (" << imageCoords[0] << ", " << imageCoords[1] << ")" << endl;


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

0 comments on commit 4a532de

Please sign in to comment.