Skip to content

Commit 4a532de

Browse files
author
Erika
committed
new projected depth map
1 parent bef3029 commit 4a532de

File tree

3 files changed

+71
-25
lines changed

3 files changed

+71
-25
lines changed

depthmap/depthmap.cpp

+51-9
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,53 @@ void Depthmap::computeNormals() {
211211
}
212212
}
213213
}
214+
215+
void Depthmap::projectToCameraDepthMap(const Camera& camera, const QString& outputPath) {
216+
217+
QImage depthMapImage(camera.width, camera.height, QImage::Format_RGB888);
218+
depthMapImage.fill(qRgb(0, 0, 0));
219+
//find the minimum and maximum for the Z coordinates
220+
float minZ = std::numeric_limits<float>::max();
221+
float maxZ = std::numeric_limits<float>::lowest();
222+
for (int y = 0; y < height; y++) {
223+
for (int x = 0; x < width; x++) {
224+
float pixelZ = elevation[x + y * width];
225+
226+
Eigen::Vector3f realCoordinates = pixelToRealCoordinates(x, y, pixelZ);
227+
Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);
228+
if(pixelZ > 0){
229+
minZ = std::min(minZ, pixelZ);
230+
maxZ = std::max(maxZ, pixelZ);
231+
}
232+
}
233+
}
234+
if (minZ >= maxZ) {
235+
qWarning("MinZ and MaxZ invalid. Skip depth map generation.");
236+
return;
237+
}
238+
for (int y = 0; y < height; y++) {
239+
for (int x = 0; x < width; x++) {
240+
float pixelZ = elevation[x + y * width];
241+
242+
if (pixelZ <= 0) continue;
243+
int pixelValue = (int)round(((pixelZ - minZ) / (maxZ - minZ)) * 255);
244+
pixelValue = std::min(std::max(pixelValue, 0), 255);
245+
246+
Eigen::Vector3f realCoordinates = pixelToRealCoordinates(x, y, pixelZ);
247+
Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);
248+
249+
int imageX = (int)round(imageCoords[0]);
250+
int imageY= (int)round(imageCoords[1]);
251+
if (imageX >= 0 && imageX < camera.width && imageY >= 0 && imageY < camera.height) {
252+
depthMapImage.setPixel(imageX, imageY, qRgb(pixelValue, pixelValue, pixelValue));
253+
//cout << "Pixel projected (" << x << ", " << y << ") -> (" << imageX << ", " << imageY << "), Z = "
254+
// << pixelZ << ", pixelValue = " << pixelValue << endl;
255+
}
256+
}
257+
}
258+
depthMapImage.save(outputPath, "png");
259+
}
260+
214261
void Depthmap::depthIntegrateNormals(){
215262
if (normals.empty()){
216263
cerr << "Error: no normals found" << endl;
@@ -284,7 +331,7 @@ void Depthmap::resizeNormals (int factorPowerOfTwo, int step = 1) {
284331

285332
//QString filename = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/surface.jpg";
286333
//saveNormals(filename.toStdString().c_str());
287-
//chiama l'integrale integra le normali e salva il ply
334+
//chiama l'integrale integra le normali e salva il ply
288335
//depthIntegrateNormals();
289336
//QString plyFilename = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/resize_normals.obj";
290337
//saveObj(plyFilename.toStdString().c_str());
@@ -325,6 +372,7 @@ void Depthmap::saveObj(const char *filename){
325372
}
326373
}
327374
}
375+
328376
//prendi x=160,14 e y=140 dell'img
329377
//le coordinate del passo del pixel 0,016 sono nel xml della depth map Z_num ecc.
330378

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

514562
}
515563

516-
Eigen::Vector3f Camera::applyIntrinsicCalibration(Eigen::Vector3f& uvz) {
564+
Eigen::Vector3f Camera::applyIntrinsicCalibration(Eigen::Vector3f& uvz) const{
517565

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

540588
return Eigen::Vector3f(u_dist, v_dist, uvz.z());
541589
}
542-
543-
544-
545-
546-
547-

depthmap/depthmap.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class Camera {
1818
Eigen::Vector3f center;
1919
bool loadXml(const QString &path); //read the MicMac xml origin, origin resolution ecc.
2020
bool loadInternParameters(const QString &internePath); // read the xml with the center, rotation, focal parameter, principal points parameters ecc.
21-
Eigen::Vector3f projectionToImage(Eigen::Vector3f realPosition);
21+
Eigen::Vector3f projectionToImage(Eigen::Vector3f realPosition) const;
2222
Eigen::Vector3f applyRadialDistortion(Eigen::Vector3f& u);
23-
Eigen::Vector3f applyIntrinsicCalibration(Eigen::Vector3f& u);
23+
Eigen::Vector3f applyIntrinsicCalibration(Eigen::Vector3f& u) const;
2424

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

4950
};

depthmap/main.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ int main(int argc, char *argv[]) {
1616

1717
QString depthmapPath = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/Malt/Z_Num7_DeZoom4_STD-MALT.tif";
1818
QString orientationXmlPath = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/Ori-Relative/Orientation-L05C12.tif.xml";
19-
QString output = "out.png";
2019
Depthmap depth;
2120
depth.load(qPrintable(depthmapPath));
2221
depth.computeNormals();
22+
2323
depth.saveNormals("/Users/erika/Desktop/testcenterRel_copia/photogrammetry/original.obj");
2424
depth.saveObj("/Users/erika/Desktop/testcenterRel_copia/photogrammetry/original.obj");
2525

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

35+
QString outputPath = "/Users/erika/Desktop/testcenterRel_copia/photogrammetry/depthmap_project.png";
36+
37+
3538

3639
Camera camera;
3740
camera.loadXml(orientationXmlPath);
38-
int pixelX = 165;
39-
int pixelY = 144;
40-
float pixelZ = 4.5;
41-
41+
//int pixelX = 165;
42+
//int pixelY = 144;
43+
//float pixelZ = 4.5;
44+
depth.projectToCameraDepthMap(camera, outputPath);
4245

4346
Eigen::Matrix3f rotationMatrix;
4447
Eigen::Vector3f center;
4548

4649
//depth.getOrientationVector(orientationXmlPath, rotationMatrix, center);
47-
Eigen::Vector3f realCoordinates = depth.pixelToRealCoordinates(pixelX, pixelY, pixelZ);
48-
realCoordinates[0] = -0.1216933;
49-
realCoordinates[1] = 0.7094725;
50-
realCoordinates[2] = -10.4198828;
51-
Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);
50+
//Eigen::Vector3f realCoordinates = depth.pixelToRealCoordinates(pixelX, pixelY, pixelZ);
51+
//realCoordinates[0] = -0.1216933;
52+
//realCoordinates[1] = 0.7094725;
53+
//realCoordinates[2] = -10.4198828;
54+
//Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);
5255

5356
cout << "Rotation matrix:"<< endl << camera.rotation << endl;
5457
cout << "Central vector: (" << camera.center << endl;
55-
cout << "Real coordinates: (" << realCoordinates[0] << ", "
56-
<< realCoordinates[1] << ", " << realCoordinates[2] << ")" << endl;
57-
58-
cout << "Coordinate immagine: (" << imageCoords[0] << ", " << imageCoords[1] << ")" << endl;
58+
//cout << "Real coordinates: (" << realCoordinates[0] << ", "
59+
// << realCoordinates[1] << ", " << realCoordinates[2] << ")" << endl;
5960

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

6163

64+
//d = (h + zerolevel) *f scala
6265
return 0;
6366
}
6467

0 commit comments

Comments
 (0)