From 98a312094ea4c8676fba77173c55d132a2edf310 Mon Sep 17 00:00:00 2001 From: Trygve Date: Sun, 28 Apr 2024 22:30:37 +0200 Subject: [PATCH] Added box blur --- src/HeightMap.cpp | 44 +++++++++++++++++++++++++++++++++++++++++- src/contour_creator.hh | 1 + src/main.cpp | 5 +++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/HeightMap.cpp b/src/HeightMap.cpp index f4b4975..017a0a4 100644 --- a/src/HeightMap.cpp +++ b/src/HeightMap.cpp @@ -1,7 +1,7 @@ #include #include #include - +#include #include #include "gdal/gdal_priv.h" #include @@ -48,4 +48,46 @@ float HeightMap::get_pixel(int x, int y) int offset = ((this->width * y) + x); //std::cout << " offset: " << offset << " " << x << ","<< y; return *(this->data + offset); +} + +void HeightMap::blur(float standard_deviation) +{ + // standard_deviation does not do anything yet. This is currently a simple box blur + int kernel_height = 5; + int kernel_width = 5; + int kernel_size = kernel_height*kernel_width; + float* kernel = (float*) CPLMalloc(sizeof(float)*kernel_size); + for (int i=0; iwidth*this->height); + #pragma omp parallel + { + #pragma omp for + for (int i = 0; i < this->height * this->width; i++) + { + float blurred_pixel = 0; + for (int j = 0; j < kernel_size; j++) + { + int x = j%kernel_width - kernel_width/2 + i%this->width; + int y = j/kernel_width - kernel_height/2 + i/this->width; + if (x<0 || x>=this->width) + { + x = 0; + } + + if (y<0 || y>=this->height) + { + y = 0; + } + blurred_pixel += this->get_pixel(x, y); + } + *(blurred + i) = blurred_pixel/float(kernel_size); + } + } + free(this->data); + this->data = blurred; + blurred = nullptr; } \ No newline at end of file diff --git a/src/contour_creator.hh b/src/contour_creator.hh index 7ab598b..87d5066 100644 --- a/src/contour_creator.hh +++ b/src/contour_creator.hh @@ -19,6 +19,7 @@ class HeightMap HeightMap(const char* filepath); float get_pixel(int x,int y); + void blur(float standard_deviation); }; /** diff --git a/src/main.cpp b/src/main.cpp index 15049e5..bea1db9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -80,8 +80,7 @@ constexpr std::tuple marching_squares_lookup(int case 12: return {0, 0.5, 1, 0.5}; case 13: return {0.5, 0, 1, 0.5}; case 14: return {0, 0.5, 0.5, 0}; - } - + }; } void write_output_file(std::vector cellmaps, const char *filepath) { @@ -185,6 +184,8 @@ int main(int argc, const char* argv[]) std::cout << "x: " << map.width << " y: " << map.height << "\n"; std::cout << "max: " << map.max << " min: " << map.min << "\n"; + map.blur(0.8); + auto cellmap = produce_cellmap(&map, 40); /* for (int y = 0; y < cellmap.height; y++)