Symmetric Nearest Neighbor in C#
I showed the Kuwahara filter and while it's rather cool, there's actually a filter that works a bit better: symmetric nearest neighbor. The SNN filter works in a similar fashion to k-nearest neighbor algorithms (which I'm sure is surprising since it more or less is a KNN algorithm...). All the algorithm does, is it takes symmetric pairs (although you can use quadruples as well) of pixels, compares each one to the center pixel, and the one that is closer (color wise, not distance) gets added to the total for that center pixel. At the end we divide by the number of pixels we added together and we end up with the new center pixel... Anyway, I'm sure that code would probably help you out more than my poor explanation:
/// <summary>
/// Does smoothing using a SNN blur
/// </summary>
/// <param name="OriginalImage">Image to manipulate</param>
/// <param name="Size">Size of the aperture</param>
public static Bitmap SNNBlur(Bitmap OriginalImage, int Size)
{
Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
BitmapData NewData = Image.LockImage(NewBitmap);
BitmapData OldData = Image.LockImage(OriginalImage);
int NewPixelSize = Image.GetPixelSize(NewData);
int OldPixelSize = Image.GetPixelSize(OldData);
int ApetureMinX = -(Size / 2);
int ApetureMaxX = (Size / 2);
int ApetureMinY = -(Size / 2);
int ApetureMaxY = (Size / 2);
for (int x = 0; x < NewBitmap.Width; ++x)
{
for (int y = 0; y < NewBitmap.Height; ++y)
{
int RValue = 0;
int GValue = 0;
int BValue = 0;
int NumPixels = 0;
for (int x2 = ApetureMinX; x2 < ApetureMaxX; ++x2)
{
int TempX1 = x + x2;
int TempX2 = x - x2;
if (TempX1 >= 0 && TempX1 < NewBitmap.Width && TempX2 >= 0 && TempX2 < NewBitmap.Width)
{
for (int y2 = ApetureMinY; y2 < ApetureMaxY; ++y2)
{
int TempY1 = y + y2;
int TempY2 = y - y2;
if (TempY1 >= 0 && TempY1 < NewBitmap.Height && TempY2 >= 0 && TempY2 < NewBitmap.Height)
{
Color TempColor = Image.GetPixel(OldData, x, y, OldPixelSize);
Color TempColor2 = Image.GetPixel(OldData, TempX1, TempY1, OldPixelSize);
Color TempColor3 = Image.GetPixel(OldData, TempX2, TempY2, OldPixelSize);
if (Distance(TempColor.R, TempColor2.R, TempColor.G, TempColor2.G, TempColor.B, TempColor2.B) <
Distance(TempColor.R, TempColor3.R, TempColor.G, TempColor3.G, TempColor.B, TempColor3.B))
{
RValue += TempColor2.R;
GValue += TempColor2.G;
BValue += TempColor2.B;
}
else
{
RValue += TempColor3.R;
GValue += TempColor3.G;
BValue += TempColor3.B;
}
++NumPixels;
}
}
}
}
Color MeanPixel = Color.FromArgb(RValue / NumPixels,
GValue / NumPixels,
BValue / NumPixels);
Image.SetPixel(NewData, x, y, MeanPixel, NewPixelSize);
}
}
Image.UnlockImage(NewBitmap, NewData);
Image.UnlockImage(OriginalImage, OldData);
return NewBitmap;
}
private static double Distance(int R1, int R2, int G1, int G2, int B1, int B2)
{
return System.Math.Sqrt(((R1 - R2) * (R1 - R2)) + ((G1 - G2) * (G1 - G2)) + ((B1 - B2) * (B1 - B2)));
}
Please note that the code uses a couple of functions from my utility library. Namely it uses an image locking/unlocking function to speed things up a bit, which can be removed and a SetPixel/GetPixel function which can be replaced with the built in .Net equivalents. Anyway, the code above has two functions. The first is the actual SNN function. It takes a bitmap and also the size of the box around each pixel that you want to look at. The second function is simply a helper, that calculates the Euclidean distance between two pixels. The code is rather straight forward. You look at each pixel and the pixels around it, taking the values that are closest in color. You add up the values and then divide it by the number of pixels that you added together. Very basic but it does a great job at smoothing an item and yet preserving borders. Anyway, hopefully this helps you out. So give it a try, leave feedback, and happy coding.
Sign up for More Posts
I have no idea why you'd put yourself through that but if you'd like more posts from me then how about using my RSS feed? Take a look.
- Previous: Matrix Convolution Filters in C#
- Next: Bump Map Creation Using C#