Skip to content

Commit

Permalink
modify depth functions with issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Erika committed Jan 23, 2025
1 parent 2fcf01f commit 1655afd
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 31 deletions.
121 changes: 91 additions & 30 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 @@ -999,6 +999,40 @@ 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 @@ -1023,14 +1057,19 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
}

GaussianGrid gaussianGrid;
gaussianGrid.minSamples = 3; // 1, 3, 5
gaussianGrid.sideFactor = 1; // 0.25, 0.5, 1
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++){
//if(x,y )
if(mask[x + y * width] != 0.0f){
continue;
}
Eigen::Vector3f r = pixelToRealCoordinates(x, y, ortho_z);
assert(fabs(z-r[2]) < 0.1f);
Eigen::Vector3f p = camera.camera.projectionToImage(r);
Expand All @@ -1046,16 +1085,19 @@ void OrthoDepthmap::integratedCamera(const CameraDepthmap& camera, const char *o
Eigen::Vector3f d = realToPixelCoord(r[0], r[1], h);

float w = camera.calculateWeight(px, py);

//p0 e p1 devono venire uguale e vedi se depth è ugusle, h dovrebbe venire simile

elevation[x + y * width] += w * d[2];
mask[x+ y * width] += w;
} else {

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

}else {
//elevation[x + y*width] = origin[2] + resolution[2] * elevation[x + y*width];
}
}
}
saveObj("testElev.obj");


}
/*_-----------------------------------------------------------------------------------------*/
void GaussianGrid::fitLinear(std::vector<float> &x, std::vector<float> &y, float &a, float &b) {
Expand Down Expand Up @@ -1107,7 +1149,7 @@ float GaussianGrid::bilinearInterpolation(float x, float y) {
}
//fit h = a+b*elev
void GaussianGrid::init(std::vector<Eigen::Vector3f> &cloud, std::vector<float> &source) {
int side = static_cast<int>(sqrt(cloud.size())) / 2;
int side = static_cast<int>(sideFactor * sqrt(cloud.size()));
sigma = 1.0f / side;
width = side;
height = side;
Expand Down Expand Up @@ -1220,7 +1262,7 @@ float GaussianGrid::value(float x, float y){
float pixelX = x * (width-1);
float pixelY = y * (height-1);

return bilinearInterpolation(pixelX, pixelY);
return bilinearInterpolation(pixelX, pixelY);

//return values[pixelX + pixelY * width];

Expand All @@ -1247,7 +1289,7 @@ void GaussianGrid::computeGaussianWeightedGrid(std::vector<Eigen::Vector3f> &dif
std::vector<int> count(width*height, 0);


float max_distance = 1.2 * sigma;
float max_distance = 2 * sigma;
for (auto &p : differences) {


Expand All @@ -1271,14 +1313,14 @@ void GaussianGrid::computeGaussianWeightedGrid(std::vector<Eigen::Vector3f> &dif
}
}
}
//chiama camere tutte e 4 e vedi come vengono
//chiama camere tutte e 4 e vedi come vengono
// pesare per il blanding funzione intervallo 0, 1 * 0, 1 0 ai bordi 1 al centro, che sia una funzione continua
//polinomio di 2 grado in x * pol 2 grado in y. derivata e peso a 0 sul bordo
//fai somma pesata e veedi come vieni
//funz target ritorna valore e peso

for (int i = 0; i < values.size(); i++) {
if(count[i] < 3)
if(count[i] < minSamples)
weights[i] = 0;
if (weights[i] != 0) {
values[i] /= (weights[i]);
Expand All @@ -1290,29 +1332,48 @@ void GaussianGrid::computeGaussianWeightedGrid(std::vector<Eigen::Vector3f> &dif
}
}*/
}
//
float Depthmap::calculateWeight(float x, float y) const{
//se < 1/4 è -8x^2, else se è < 3/4. 8*(x-1)^2, data una x mi ritorna una x+1

static float bell(float x){
if(x < 0.25f){
return 8.0f* x * x;
}
else if(x< 0.75f){
return -8.0f * x *x + 8.0f * x-1.0f;
}
else {
x = 1.0f - x;
return 8.0f * x * x;
}

x/=width;
y/= height;
}

float weightX =pow(cos(M_PI * (x-0.5f)), 2);
float weightY = pow(cos(M_PI * (y-0.5f)), 2);
float Depthmap::calculateWeight(float x, float y) const{

return weightX * weightY;
x /= width;
y /= height;

// float weightX =pow(cos(M_PI * (x-0.5f)), 2);
//float weightY = pow(cos(M_PI * (y-0.5f)), 2);
return bell(x)*bell(y);
//return weightX * weightY;
}
void OrthoDepthmap::beginIntegration(){
elevation.clear();
elevation.resize(width * height, 0);
mask.clear();
mask.resize(width * height, 0);
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]){
elevation[i] /= mask[i];
}
//if(mask[i] == 0.0f) {// && weights[i] != 0.0f){
elevation[i] =4; ///= weights[i];
}
}

Expand Down
4 changes: 4 additions & 0 deletions depthmap/depthmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Depthmap {
uint32_t width, height;
std::vector<float> elevation;
std::vector<float> mask;
std::vector<float> weights;

std::vector<Eigen::Vector3f> normals;


Expand Down Expand Up @@ -66,6 +68,8 @@ class GaussianGrid {
int width, height;
std::vector<float> values;
std::vector<float> weights;
float sideFactor = 0.5f; // corrective factor over the 1/sqrt(n) formula.
int minSamples = 3; // minimum number of samples needed in a pixel
float sigma;
float a, b;//coefficient of linear transform from source to point cloud space.

Expand Down
15 changes: 14 additions & 1 deletion depthmap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ int main(int argc, char *argv[]) {
QString output_points = base + "points_h.txt";
QString output_grid = base + "tgrid.png";


OrthoDepthmap ortho;

if(!ortho.load(qPrintable(depthmapPath), qPrintable(maskPath))){
Expand All @@ -65,6 +66,9 @@ int main(int argc, char *argv[]) {
return -1;
}

//doortho = 1 domec =0;


//ortho.computeNormals();
//ortho.saveNormals(qPrintable(base + "testcenterRel_copia/photogrammetry/original.png"));
//ortho.saveObj(qPrintable(base + "testcenterRel_copia/photogrammetry/original.obj"));
Expand All @@ -80,6 +84,10 @@ 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 @@ -114,7 +122,12 @@ int main(int argc, char *argv[]) {

}
ortho.endIntegration();
ortho.saveDepth(qPrintable("final_depth.tif"));
ortho.saveDepth(qPrintable(depthmapPath));
ortho.saveMask(qPrintable(maskPath));
ortho.saveObj("weightsElev2.obj");





//depthCam.camera.loadXml(orientationXmlPath);
Expand Down

0 comments on commit 1655afd

Please sign in to comment.