58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Windows;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
namespace DSPS_Lab3;
|
|
|
|
public static class Ex
|
|
{
|
|
public static float[,] GenerateSpatialKernel(int size, float sigma)
|
|
{
|
|
var kernel = new float[size, size];
|
|
float sum = 0;
|
|
var half = size / 2;
|
|
|
|
// Use <= half to ensure the full kernel is covered
|
|
for (var y = -half; y <= half; y++)
|
|
{
|
|
for (var x = -half; x <= half; x++)
|
|
{
|
|
// Optional: Bounds check for odd/even sizes
|
|
if (y + half >= size || x + half >= size) continue;
|
|
|
|
var val = (float)Math.Exp(-(x * x + y * y) / (2 * sigma * sigma));
|
|
kernel[y + half, x + half] = val;
|
|
sum += val;
|
|
}
|
|
}
|
|
|
|
for (var y = 0; y < size; y++)
|
|
for (var x = 0; x < size; x++)
|
|
kernel[y, x] /= sum;
|
|
|
|
return kernel;
|
|
}
|
|
|
|
public static float[,] PadAndShiftKernel(float[,] smallKernel, int imgWidth, int imgHeight)
|
|
{
|
|
var fullKernel = new float[imgHeight, imgWidth];
|
|
int kHeight = smallKernel.GetLength(0);
|
|
int kWidth = smallKernel.GetLength(1);
|
|
int radY = kHeight / 2;
|
|
int radX = kWidth / 2;
|
|
|
|
for (int y = 0; y < kHeight; y++)
|
|
{
|
|
for (int x = 0; x < kWidth; x++)
|
|
{
|
|
// Circular shift: maps kernel center to (0,0)
|
|
int targetY = (y - radY + imgHeight) % imgHeight;
|
|
int targetX = (x - radX + imgWidth) % imgWidth;
|
|
fullKernel[targetY, targetX] = smallKernel[y, x];
|
|
}
|
|
}
|
|
return fullKernel;
|
|
}
|
|
} |