111,126
社区成员
发帖
与我相关
我的任务
分享
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;
}
private void medianFilter1(object sender,EventArgs e)
{
//boxTwo是要处理的图片(bitmap对象)
Bitmap srcBmp = (Bitmap)boxTwo.Clone();
BitmapData bmData = boxTwo.LockBits(new Rectangle(0, 0, boxTwo.Width, boxTwo.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData bmSrcData = srcBmp.LockBits(new Rectangle(0,0,srcBmp.Width,srcBmp.Height),
ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
int stride2 = bmData.Stride*2;
System.IntPtr scan0 = bmData.Scan0;
System.IntPtr srcScan0 = bmSrcData.Scan0;
unsafe
{
byte* p = (byte*)scan0;
byte* pSrc = (byte*)srcScan0;
int nWidth = boxTwo.Width - 2;
int nHeight = boxTwo.Height - 2;
int nOffset = stride - boxTwo.Width * 3;
int nPixel;
List<int> array = new List<int>();
for (int y = 0; y < nHeight; y++)
{
for (int x = 0; x < nWidth; x++)
{
/*清空数组*/
array.Clear();
array.Add(pSrc[2]);
array.Add(pSrc[5]);
array.Add(pSrc[8]);
array.Add(pSrc[2 + stride]);
array.Add(pSrc[5 + stride]);
array.Add(pSrc[8 + stride]);
array.Add(pSrc[2 + stride2]);
array.Add(pSrc[5 + stride2]);
array.Add(pSrc[8 + stride2]);
/*对数据进行大小排序*/
array.Sort();
nPixel = array[array.Count / 2];
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
/*对像素进行赋值*/
pSrc[5 + stride] = (byte)nPixel;
array.Clear();
array.Add(pSrc[1]);
array.Add(pSrc[4]);
array.Add(pSrc[7]);
array.Add(pSrc[1 + stride]);
array.Add(pSrc[4 + stride]);
array.Add(pSrc[7 + stride]);
array.Add(pSrc[1 + stride2]);
array.Add(pSrc[4 + stride2]);
array.Add(pSrc[7 + stride2]);
/*对数据进行大小排序*/
array.Sort();
nPixel = array[array.Count / 2];
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
/*对像素进行赋值*/
pSrc[4 + stride] = (byte)nPixel;
array.Clear();
array.Add(pSrc[0]);
array.Add(pSrc[3]);
array.Add(pSrc[6]);
array.Add(pSrc[0 + stride]);
array.Add(pSrc[3 + stride]);
array.Add(pSrc[6 + stride]);
array.Add(pSrc[0 + stride2]);
array.Add(pSrc[3 + stride2]);
array.Add(pSrc[6 + stride2]);
/*对数据进行大小排序*/
array.Sort();
nPixel = array[array.Count / 2];
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
/*对像素进行赋值*/
pSrc[3 + stride] = (byte)nPixel;
p += 3;
pSrc += 3;
}/*inner for*/
p += nOffset;
p += nOffset;
}/*outer for*/
}/*unsafe*/
boxTwo.UnlockBits(bmData);
srcBmp.UnlockBits(bmSrcData);
boxTwo = srcBmp;
}
int Radius = 4;
/*boxOne是要处理的图像*/
Bitmap source = (Bitmap)boxOne.Clone();
Bitmap tarBitmap = new Bitmap(boxOne.Width, boxOne.Height);
for (Int32 x = 0; x < source.Width; x++)
for (Int32 y = 0; y <source.Height; y++)
{
Int32 left = x - Radius > 0 ? x - Radius : 0;
Int32 top = y - Radius > 0 ? y - Radius : 0;
Int32 right = x + Radius > source.Width ? source.Width : x + Radius;
Int32 bottom = y + Radius > source.Height ? source.Height : y + Radius;
System.Collections.Generic.List<Int32> listColor = new System.Collections.Generic.List<Int32>();
for (Int32 i = left; i < right; i++)
for (Int32 j = top; j < bottom; j++)
{
if (!listColor.Contains(source.GetPixel(i, j).R)) listColor.Add(source.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));
}
这种代码的效率太低,考虑一下利用指针来进行数据操作,否则运算不会太快!