Skip to content

Commit a945ce6

Browse files
committed
fixed gaussian formula in depthmap filter for inscriptions
1 parent 7e6be43 commit a945ce6

File tree

1 file changed

+56
-26
lines changed

1 file changed

+56
-26
lines changed

normal_integration/main.cpp

+56-26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "../src/bni_normal_integration.h"
66

7-
#include "../src/getopt.h"
7+
#include "getopt.h"
88
extern int opterr;
99

1010
#include <math.h>
@@ -53,31 +53,41 @@ void readBlob(QString filename, int &w, int &h, vector<float> &height) {
5353
file.read((char *)&height[0], w*h*4);
5454
}
5555

56-
void averageFilter(int window, int w, int h, vector<float> &height, vector<float> &filtered) {
56+
void averageFilter(float sigma, int w, int h, vector<float> &height, vector<float> &filtered) {
5757

58+
int window = ceil(3*sigma);
5859
filtered.resize(w*h);
5960

61+
double a = 1.0 / (sigma * sqrt(2 * M_PI));
62+
double b = -1/(2*pow(sigma, 2));
6063
for(int y = 0; y < h; y++) {
6164
for(int x = 0; x < w; x++) {
6265
int pos = x + y*w;
63-
double weight = 0;
66+
double weight = 0.0;
6467
double average = 0.0;
6568

6669
for(int dy = -window; dy <= window; dy++) {
70+
6771
if(y + dy < 0 || y + dy >= h)
6872
continue;
6973
for(int dx = -window; dx <= window; dx++) {
74+
75+
//TEST AND IN CASE REMOVE.
76+
if(dx == 0 && dy == 0)
77+
continue;
78+
7079
if(x + dx < 0 || x + dx >= w)
7180
continue;
7281

7382
int dpos = x + dx + (y + dy)*w;
74-
double sigma = window/2.0;
75-
double gaussian = exp(-0.5*(dx*dx + dy*dy)/sigma*sigma)/(sigma*2*sqrt(M_PI));
83+
double gaussian = a*exp(b*(dx*dx +dy*dy));
7684
average += gaussian * height[dpos];
7785
weight += gaussian;
7886
}
7987
}
8088
double diff = height[pos] - average/weight;
89+
//if(diff > 0.2)
90+
// diff = 0.2;
8191

8292
filtered[pos] = diff;
8393
}
@@ -162,34 +172,53 @@ int main(int argc, char *argv[]) {
162172
}
163173
opterr = 0;
164174

175+
//**********************************
165176
std::vector<float> height;
166-
int W, H;
177+
#define HEIGHTMAP = 1
178+
#ifdef HEIGHTMAP
179+
int W, H;
167180
readBlob("heightmap.ply.blob", W, H, height);
168-
std::vector<float> filtered;
169-
averageFilter(10, W, H, height, filtered);
170-
//colorize:
171181

172-
auto boundaries = findMinMaxPercentilesWithHistogram(filtered, 0.05, 100);
173-
float min = boundaries.first;
174-
float max = boundaries.second;
175182

176-
auto minMax = std::minmax_element(filtered.begin(), filtered.end());
177-
float minValue = *minMax.first;
178-
float maxValue = *minMax.second;
179-
cout << "Real min: " << minValue << " percent min: " << min << endl;
180-
cout << "Real max: " << maxValue << " percent max: " << max << endl;
183+
std::vector<float> filtered;
181184
QImage img(W, H, QImage::Format_ARGB32);
182-
for(int y = 0; y < H; y++) {
183-
for(int x = 0; x < W; x++) {
184-
float v = 2*(filtered[x + y*W] - min)/(max - min);
185-
if(v > 1) v = 1;
186-
int l = (int)(v*255.0f);
187-
img.setPixel(x, y, qRgb(l, l, l));
185+
186+
for(float filter_sigma = 1.0f; filter_sigma < 4.0f; filter_sigma+= 0.5) {
187+
cout << "Sigma: " << filter_sigma << endl;
188+
averageFilter(filter_sigma, W, H, height, filtered);
189+
//colorize:
190+
auto minMax = std::minmax_element(filtered.begin(), filtered.end());
191+
float minValue = *minMax.first;
192+
float maxValue = *minMax.second;
193+
194+
double low_percentile = 0.1;
195+
double high_percentile = 65;
196+
auto boundaries = findMinMaxPercentilesWithHistogram(filtered, low_percentile, high_percentile);
197+
float min = boundaries.first;
198+
//float max = boundaries.second;
199+
200+
//float min = minValue + low_percentile*(maxValue - minValue)/100.0f;
201+
float max = minValue + high_percentile*(maxValue - minValue)/100.0f;
202+
203+
204+
205+
//min = minValue;
206+
//max = maxValue;
207+
//cout << "Real min: " << minValue << " percent min: " << min << endl;
208+
//cout << "Real max: " << maxValue << " percent max: " << max << endl;
209+
for(int y = 0; y < H; y++) {
210+
for(int x = 0; x < W; x++) {
211+
float v = 2*(filtered[x + y*W] - min)/(max - min);
212+
if(v > 1) v = 1;
213+
int l = (int)(v*255.0f);
214+
img.setPixel(x, y, qRgb(l, l, l));
215+
}
188216
}
217+
218+
img.save(QString("heightmap_%1_%2.jpg").arg(filter_sigma).arg(low_percentile));
189219
}
190-
img.save("heightmap.jpg");
191220
return 0;
192-
221+
#endif
193222
QString ply;
194223
float k = 2; //discontinuity propensity
195224
int max_solver_iterations = 5000;
@@ -254,7 +283,8 @@ int main(int argc, char *argv[]) {
254283
}
255284

256285
if(!ply.isNull()) {
257-
savePly(ply, w, h, height_map);
286+
cout<<"print"<<endl;
287+
savePly(ply, w, h, height_map);
258288
saveBlob(ply + ".blob", w, h, height_map);
259289
}
260290
}

0 commit comments

Comments
 (0)