Skip to content

Commit

Permalink
fixed point cloud refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
ponchio committed Jan 23, 2025
1 parent 1655afd commit f6e2025
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 75 deletions.
138 changes: 76 additions & 62 deletions depthmap/depthmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ bool Depthmap::loadTiff(const char *tiff, vector<float> &values, uint32_t &w, ui
// Check if the TIFF is tiled
uint32_t tileWidth, tileLength;
if (!TIFFGetField(inTiff, TIFFTAG_TILEWIDTH, &tileWidth) ||
!TIFFGetField(inTiff, TIFFTAG_TILELENGTH, &tileLength)) {
!TIFFGetField(inTiff, TIFFTAG_TILELENGTH, &tileLength)) {
return loadStripedTiff(inTiff, values, w, h, bitsPerSample);
} else {
return loadTiledTiff(inTiff, values, w, h, tileWidth, tileLength, bitsPerSample);
Expand Down Expand Up @@ -453,10 +453,10 @@ bool Depthmap::loadNormals(const char *normals_path){
int i = x + y * width;

normals[i] = Eigen::Vector3f(
(qRed(rgb) / 255.0f) * 2.0f - 1.0f,
(qGreen(rgb) / 255.0f) * 2.0f - 1.0f,
(qBlue(rgb) / 255.0f) * 2.0f - 1.0f
);
(qRed(rgb) / 255.0f) * 2.0f - 1.0f,
(qGreen(rgb) / 255.0f) * 2.0f - 1.0f,
(qBlue(rgb) / 255.0f) * 2.0f - 1.0f
);
}
}

Expand Down Expand Up @@ -774,7 +774,7 @@ bool OrthoDepthmap::loadXml(const char *xmlPath){


if (originePlaniNodes.isEmpty() || resolutionPlaniNodes.isEmpty() ||
origineAltiNodes.isEmpty() || resolutionAltiNodes.isEmpty()) {
origineAltiNodes.isEmpty() || resolutionAltiNodes.isEmpty()) {
cerr << "OriginePlani, ResolutionPlani, OrigineAlti, or ResolutionAlti not found in XML." << endl;
return false;

Expand Down Expand Up @@ -981,7 +981,73 @@ void OrthoDepthmap::verifyPointCloud(){
*/
}
}
//#define PRESERVE_INTERIOR

void OrthoDepthmap::beginIntegration(){

bool use_depthmap = false;
if(use_depthmap) {
//1. togliere dalla point tutti i punti che cadono dentro la maschera
//2. aggiungere un campionamento regolare dentro la maschera
int count = 0;
for(size_t i = 0; i < point_cloud.size(); i++) {

Eigen::Vector3f point = point_cloud[i];
Eigen::Vector3f pixel = realToPixelCoord(point[0], point[1], point[2]);

int mx = std::max<float>(0, std::min<float>(width-1, int(pixel[0])));
int my = std::max<float>(0, std::min<float>(height-1, int(pixel[1])));

bool inside = mask[mx + my*width] == 0.0f;
if(inside)
continue;

point_cloud[count++] = point;
}
point_cloud.resize(count);

int step = 1;

for(int y = 0; y < height; y+= step) {
for(int x = 0; x < width; x += step) {
bool inside = (mask[x + y*width] != 0.0f);
if(!inside)
continue;
auto point = pixelToRealCoordinates(x, y, elevation[x + y*width]);
point_cloud.push_back(point);
}
}
}

for(size_t i =0; i < elevation.size(); i++) {
#ifdef PRESERVE_INTERIOR
if(mask[i] == 0.0f){
#endif
elevation[i] = 0.0f;
#ifdef PRESERVE_INTERIOR
}
#endif
}
// elevation.clear();
// elevation.resize(width * height, 0);
weights.clear();
weights.resize(width * height, 0);

}

void OrthoDepthmap::endIntegration(){
for(size_t i =0; i < elevation.size(); i++){
#ifdef PRESERVE_INTERIOR
if(mask[i] == 0.0f) {
#endif
if(weights[i] != 0.0f) {
elevation[i] /= weights[i];
}
#ifdef PRESERVE_INTERIOR
}
#endif
}
}

void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *outputFile){

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

//1. togliere dalla point tutti i punti che cadono dentro la maschera
//2. aggiungere un campionamento regolare dentro la maschera

int count = 0;

for(int i = 0; i < point_cloud.size(); i++) {

Eigen::Vector3f point = point_cloud[i];
Eigen::Vector3f pixel = realToPixelCoord(point[0], point[1], point[2]);

int mx = std::max<float>(0, std::min<float>(width-1, int(pixel[0])));
int my = std::max<float>(0, std::min<float>(height-1, int(pixel[1])));

bool inside = mask[mx + my*width] == 0.0f;
if(inside)
continue;

point_cloud[count++] = point;
}
point_cloud.resize(count);


int step = 10;

for(int y = 0; y < height; y+= step) {
for(int x = 0; x < width; x += step) {
bool inside = mask[x + y*width] == 0.0f;
if(!inside)
continue;
auto point = pixelToRealCoordinates(x, y, elevation[x + y*width]);
point_cloud.push_back(point);
}
}

for (size_t i = 0; i < point_cloud.size(); i++) {

Eigen::Vector3f realCoord = point_cloud[i];
Expand All @@ -1047,11 +1079,9 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o

if (pixelX >= 0 && pixelX < camera.width && pixelY >= 0 && pixelY < camera.height) {
float depthValue = camera.elevation[pixelX + pixelY * camera.width];
cout << pixelX << " " << pixelY << " " << depthValue << endl;
imageCloud.push_back(Eigen::Vector3f(imageCoords[0]/camera.width, imageCoords[1]/camera.height, h));
source.push_back(depthValue);
out << depthValue << "\t" << h << "\t" << imageCoords[0]/camera.width << "\t" << imageCoords[1]/camera.height <<"\n"; //red e green

}

}
Expand All @@ -1062,14 +1092,15 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
gaussianGrid.init(imageCloud, source);
gaussianGrid.imageGrid(("test.png"));

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

for(size_t y=0; y < height; y++){
for(size_t x=0; x < width; x++){
#ifdef PRESERVE_INTERIOR
if(mask[x + y * width] != 0.0f){
continue;
}
#endif
Eigen::Vector3f r = pixelToRealCoordinates(x, y, ortho_z);
assert(fabs(z-r[2]) < 0.1f);
Eigen::Vector3f p = camera.camera.projectionToImage(r);
Expand All @@ -1081,6 +1112,7 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
float depthValue = camera.elevation[px + py * camera.width];
p[0] = px / float(camera.width);
p[1] = py / float(camera.height);

float h = gaussianGrid.target(p[0], p[1], depthValue);
Eigen::Vector3f d = realToPixelCoord(r[0], r[1], h);

Expand All @@ -1091,7 +1123,7 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o

weights[x+ y * width] += w;

}else {
} else {
//elevation[x + y*width] = origin[2] + resolution[2] * elevation[x + y*width];
}
}
Expand Down Expand Up @@ -1358,24 +1390,6 @@ float Depthmap::calculateWeight(float x, float y) const{
return bell(x)*bell(y);
//return weightX * weightY;
}
void OrthoDepthmap::beginIntegration(){
for(size_t i =0; i < elevation.size(); i++){
if(mask[i] == 0.0f){
elevation[i] = 0.0f;
}
}
// elevation.clear();
// elevation.resize(width * height, 0);
// weights.clear();
// weights.resize(width * height, 0);

}
void OrthoDepthmap::endIntegration(){
for(size_t i =0; i < elevation.size(); i++){
//if(mask[i] == 0.0f) {// && weights[i] != 0.0f){
elevation[i] =4; ///= weights[i];
}
}

void GaussianGrid::imageGrid(const char* filename) {

Expand Down
32 changes: 19 additions & 13 deletions depthmap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char *argv[]) {
return 1;
}*/
//input
#define MACOS 1
//#define MACOS 1
#ifdef MACOS
QString base = "/Users/erika/Desktop/testcenterRel_copia/";
#else
Expand All @@ -26,12 +26,12 @@ int main(int argc, char *argv[]) {



QString depthmapPath = base + "photogrammetry/Malt/Z_Num7_DeZoom4_STD-MALT.tif";
QString depthmapPath = base + base + "Z_Num7_DeZoom4_STD-MALT.tif";
//QString cameraDepthmap = base + "datasets/L04C12.tif";
//QString orientationXmlPath = base + "photogrammetry/Ori-Relative/Orientation-L04C12.tif.xml";
QString maskPath = base + "photogrammetry/Malt/Masq_STD-MALT_DeZoom4.tif";
QString plyFile = base +"photogrammetry/AperiCloud_Relative__mini.ply";
QString point_txt = base + "photogrammetry/points_h.txt";
QString maskPath = base + "Masq_STD-MALT_DeZoom4.tif";
QString plyFile = base +"AperiCloud_Relative__mini.ply";
//QString point_txt = base + "photogrammetry/points_h.txt";
Depthmap depth;

//output
Expand All @@ -44,13 +44,25 @@ int main(int argc, char *argv[]) {

OrthoDepthmap ortho;


QFile::remove(depthmapPath);
if (!QFile::copy(depthmapPath + "_backup.tif", depthmapPath)) {
cout << "Errror copying depthmap" << endl;
exit(0);
}
QFile::remove(maskPath);
if (!QFile::copy( maskPath + "_backup.tif", maskPath)) {
cout << "Error copying mask" << endl;
exit(0);
}

if(!ortho.load(qPrintable(depthmapPath), qPrintable(maskPath))){
cout << "accidenti" << endl;
return -1;
}

QDir datasetsDir(base + "datasets");
QDir xmlDir(base + "photogrammetry/Ori-Relative");
QDir datasetsDir(base + "../datasets");
QDir xmlDir(base + "Ori-Relative");
QStringList tiffFilters = {"*.tif"};
QStringList xmlFilters = {"Orientation-*.tif.xml"};

Expand Down Expand Up @@ -84,10 +96,6 @@ int main(int argc, char *argv[]) {
ortho.verifyPointCloud();
ortho.beginIntegration();

if (QFile::copy(depthmapPath + "_backup.tif", depthmapPath))
cout << "Copia di backup salvata come: " << (depthmapPath + "_backup.tif").toStdString() << endl;
if (QFile::copy( maskPath + "_backup.tif", maskPath))
cout << "Copia di backup salvata come: " << (maskPath + "_backup.tif").toStdString() << endl;

for (const QFileInfo &tiffFile : tiffFiles) {

Expand Down Expand Up @@ -118,8 +126,6 @@ int main(int argc, char *argv[]) {
QString outputTiffPath = base +"output_" + tiffFile.fileName();
cout << "Output TIFF Path: " << outputTiffPath.toStdString() << endl;



}
ortho.endIntegration();
ortho.saveDepth(qPrintable(depthmapPath));
Expand Down

0 comments on commit f6e2025

Please sign in to comment.