如何通过高斯平滑(C++)来平滑二维坐标的向量?
How to smooth vector of 2d coordinates by gaussian smoothing (c++)?
我有一个 std::vector 的 2d 或 3d 坐标。我需要对其应用高斯平滑,以获得平滑的曲线。
怎么做?我可以平滑浮点值的向量,但我不知道如何平滑多维值。
好的,我只是独立地平滑了每个坐标并且它起作用了:
void PathControllerGaussian::smooth_path_gaussian(FuturePath & path, unsigned future_steps)
{
smooth_dimension_gaussian(path.pos, 0, future_steps);
smooth_dimension_gaussian(path.pos, 1, future_steps);
smooth_dimension_gaussian(path.pos, 2, future_steps);
}
void PathControllerGaussian::smooth_dimension_gaussian(PathVec & vec, unsigned dim_index, unsigned vec_size)
{
float sum;
for (unsigned i = gaussian_kernel_size / 2; i < vec_size - gaussian_kernel_size / 2; ++i)
{
sum = 0;
for (int j = 0; j < gaussian_kernel_size; ++j)
{
sum += vec[i + j - gaussian_kernel_size / 2][dim_index] * gaussian_kernel[j];
}
vec[i][dim_index] = sum;
}
}
float PathControllerGaussian::gaussian(float value, float sigma)
{
return 1 / sqrt(2 * M_PI * sigma * sigma) * std::exp(- (value * value) / (2 * sigma * sigma));
}
void PathControllerGaussian::prepare_gaussian_kernel(unsigned size, float sigma)
{
gaussian_kernel_size = size;
gaussian_sigma = sigma;
gaussian_kernel.clear();
int mid_point = gaussian_kernel_size / 2;
for (int i = 0; i < gaussian_kernel_size; ++i)
{
gaussian_kernel.push_back(gaussian(i - mid_point, gaussian_sigma));
}
}
我有一个 std::vector 的 2d 或 3d 坐标。我需要对其应用高斯平滑,以获得平滑的曲线。
怎么做?我可以平滑浮点值的向量,但我不知道如何平滑多维值。
好的,我只是独立地平滑了每个坐标并且它起作用了:
void PathControllerGaussian::smooth_path_gaussian(FuturePath & path, unsigned future_steps)
{
smooth_dimension_gaussian(path.pos, 0, future_steps);
smooth_dimension_gaussian(path.pos, 1, future_steps);
smooth_dimension_gaussian(path.pos, 2, future_steps);
}
void PathControllerGaussian::smooth_dimension_gaussian(PathVec & vec, unsigned dim_index, unsigned vec_size)
{
float sum;
for (unsigned i = gaussian_kernel_size / 2; i < vec_size - gaussian_kernel_size / 2; ++i)
{
sum = 0;
for (int j = 0; j < gaussian_kernel_size; ++j)
{
sum += vec[i + j - gaussian_kernel_size / 2][dim_index] * gaussian_kernel[j];
}
vec[i][dim_index] = sum;
}
}
float PathControllerGaussian::gaussian(float value, float sigma)
{
return 1 / sqrt(2 * M_PI * sigma * sigma) * std::exp(- (value * value) / (2 * sigma * sigma));
}
void PathControllerGaussian::prepare_gaussian_kernel(unsigned size, float sigma)
{
gaussian_kernel_size = size;
gaussian_sigma = sigma;
gaussian_kernel.clear();
int mid_point = gaussian_kernel_size / 2;
for (int i = 0; i < gaussian_kernel_size; ++i)
{
gaussian_kernel.push_back(gaussian(i - mid_point, gaussian_sigma));
}
}