|
4 | 4 |
|
5 | 5 | #include "../src/bni_normal_integration.h"
|
6 | 6 |
|
7 |
| -#include "../src/getopt.h" |
| 7 | +#include "getopt.h" |
8 | 8 | extern int opterr;
|
9 | 9 |
|
10 | 10 | #include <math.h>
|
@@ -53,31 +53,41 @@ void readBlob(QString filename, int &w, int &h, vector<float> &height) {
|
53 | 53 | file.read((char *)&height[0], w*h*4);
|
54 | 54 | }
|
55 | 55 |
|
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) { |
57 | 57 |
|
| 58 | + int window = ceil(3*sigma); |
58 | 59 | filtered.resize(w*h);
|
59 | 60 |
|
| 61 | + double a = 1.0 / (sigma * sqrt(2 * M_PI)); |
| 62 | + double b = -1/(2*pow(sigma, 2)); |
60 | 63 | for(int y = 0; y < h; y++) {
|
61 | 64 | for(int x = 0; x < w; x++) {
|
62 | 65 | int pos = x + y*w;
|
63 |
| - double weight = 0; |
| 66 | + double weight = 0.0; |
64 | 67 | double average = 0.0;
|
65 | 68 |
|
66 | 69 | for(int dy = -window; dy <= window; dy++) {
|
| 70 | + |
67 | 71 | if(y + dy < 0 || y + dy >= h)
|
68 | 72 | continue;
|
69 | 73 | for(int dx = -window; dx <= window; dx++) {
|
| 74 | + |
| 75 | + //TEST AND IN CASE REMOVE. |
| 76 | + if(dx == 0 && dy == 0) |
| 77 | + continue; |
| 78 | + |
70 | 79 | if(x + dx < 0 || x + dx >= w)
|
71 | 80 | continue;
|
72 | 81 |
|
73 | 82 | 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)); |
76 | 84 | average += gaussian * height[dpos];
|
77 | 85 | weight += gaussian;
|
78 | 86 | }
|
79 | 87 | }
|
80 | 88 | double diff = height[pos] - average/weight;
|
| 89 | + //if(diff > 0.2) |
| 90 | + // diff = 0.2; |
81 | 91 |
|
82 | 92 | filtered[pos] = diff;
|
83 | 93 | }
|
@@ -162,34 +172,53 @@ int main(int argc, char *argv[]) {
|
162 | 172 | }
|
163 | 173 | opterr = 0;
|
164 | 174 |
|
| 175 | +//********************************** |
165 | 176 | std::vector<float> height;
|
166 |
| - int W, H; |
| 177 | +#define HEIGHTMAP = 1 |
| 178 | +#ifdef HEIGHTMAP |
| 179 | + int W, H; |
167 | 180 | readBlob("heightmap.ply.blob", W, H, height);
|
168 |
| - std::vector<float> filtered; |
169 |
| - averageFilter(10, W, H, height, filtered); |
170 |
| - //colorize: |
171 | 181 |
|
172 |
| - auto boundaries = findMinMaxPercentilesWithHistogram(filtered, 0.05, 100); |
173 |
| - float min = boundaries.first; |
174 |
| - float max = boundaries.second; |
175 | 182 |
|
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; |
181 | 184 | 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 | + } |
188 | 216 | }
|
| 217 | + |
| 218 | + img.save(QString("heightmap_%1_%2.jpg").arg(filter_sigma).arg(low_percentile)); |
189 | 219 | }
|
190 |
| - img.save("heightmap.jpg"); |
191 | 220 | return 0;
|
192 |
| - |
| 221 | +#endif |
193 | 222 | QString ply;
|
194 | 223 | float k = 2; //discontinuity propensity
|
195 | 224 | int max_solver_iterations = 5000;
|
@@ -254,7 +283,8 @@ int main(int argc, char *argv[]) {
|
254 | 283 | }
|
255 | 284 |
|
256 | 285 | if(!ply.isNull()) {
|
257 |
| - savePly(ply, w, h, height_map); |
| 286 | + cout<<"print"<<endl; |
| 287 | + savePly(ply, w, h, height_map); |
258 | 288 | saveBlob(ply + ".blob", w, h, height_map);
|
259 | 289 | }
|
260 | 290 | }
|
0 commit comments