Added box blur

This commit is contained in:
Trygve 2024-04-28 22:30:37 +02:00
parent 882764a13d
commit 98a312094e
3 changed files with 47 additions and 3 deletions

View File

@ -1,7 +1,7 @@
#include <gdal/cpl_conv.h>
#include <iostream>
#include <stdexcept>
#include <omp.h>
#include <gdal/gdal.h>
#include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h>
@ -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; i<kernel_size; i++)
{
*(kernel + i) = 1.0;
}
float* blurred = (float *) CPLMalloc(sizeof(float)*this->width*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;
}

View File

@ -19,6 +19,7 @@ class HeightMap
HeightMap(const char* filepath);
float get_pixel(int x,int y);
void blur(float standard_deviation);
};
/**

View File

@ -80,8 +80,7 @@ constexpr std::tuple<double, double, double, double> 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<CellMap> 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++)