public Bitmap ImageFilterGS(Bitmap OriginalImage, Double[,] Filter)
{
Bitmap image = OriginalImage;
Bitmap image2 = new Bitmap(image.Width, image.Height);
if (Filter.GetLength(0) != Filter.GetLength(1))
{
throw new Exception("Filter mask must be square.");
}
if (Filter.GetLength(0) % 2 == 0)
{
throw new Exception("Filter mask dimension must be odd.");
}
if (Filter.GetLength(0) < 3)
{
throw new Exception("Filter mask dimension must be greater or equal 3.");
}
int range = (int)Math.Floor(Convert.ToDouble(Filter.GetLength(0) / 2));
for (int m = range; m < image.Width - range; m++)
{
for (int n = range; n < image.Height - range; n++)
{
Double[,] roi = new Double[Filter.GetLength(0), Filter.GetLength(0)];
int tmpi = 0;
int tmpj = 0;
int newValue = 0;
for (int i = m - range; i < m + range + 1; i++)
{
for (int j = n - range; j < n + range + 1; j++)
{
Color pixel = image.GetPixel(i, j);
Double p = Convert.ToDouble((pixel.R * 0.3) + (pixel.G * 0.59) + (pixel.B * 0.11));
roi[tmpi, tmpj] = p;
tmpj++;
}
tmpi++;
tmpj = 0;
}
for (int k = 0; k < Filter.GetLength(0); k++)
{
for (int l = 0; l < Filter.GetLength(0); l++)
{
newValue += (int)(roi[k, l] * Filter[k, l]);
}
}
//newValue = (int)(Convert.ToDouble(newValue)/9);
if (newValue > 255) newValue = 255;
if (newValue < 0) newValue = 0;
image2.SetPixel(m, n, Color.FromArgb(255, newValue, newValue, newValue));
}
}
return image2;
}