日期:2014-05-17 浏览次数:21170 次
public Bitmap Medianfilter(Bitmap bitmap,Int32 Radius)
{
Bitmap tarBitmap = new Bitmap(bitmap.Width, bitmap.Height);
for (Int32 x = 0; x < bitmap.Width; x++)
for (Int32 y = 0; y < bitmap.Height; y++)
{
Int32 left = x - Radius > 0 ? x - Radius : 0;
Int32 top = y - Radius > 0 ? y - Radius : 0;
Int32 right = x + Radius > bitmap.Width ? bitmap.Width : x + Radius;
Int32 bottom = y + Radius > bitmap.Height ? bitmap.Height : y + Radius;
List<Int32> listColor = new List<int>();
for(Int32 i = left; i < right; i ++)
for (Int32 j = top; j < bottom; j++)
{
if (!listColor.Contains(bitmap.GetPixel(i, j).R)) listColor.Add(bitmap.GetPixel(i, j).R);
}
listColor.Sort();
Int32 intMedian;
if (listColor.Count == 0) continue;
if (listColor.Count % 2 == 0)
intMedian = (listColor[listColor.Count / 2] + listColor[listColor.Count / 2 - 1]) / 2;
else
intMedian = listColor[(listColor.Count - 1) / 2];
tarBitmap.SetPixel(x, y, Color.FromArgb(intMedian, intMedian, intMedian));
}
return tarBitmap;
}