1 //===================
2 //作者:aaaSoft
3 //日期:2009年12月11日
4 //论坛:http://www.scbeta.com/bbs
5 //说明:原创文章,转载请注明出处并保留作者信息
6 //===================
7
8 /// <summary>
9 /// 中值滤波算法处理
10 /// </summary>
11 /// <param name="bmp">原始图片</param>
12 /// <param name="bmp">是否是彩色位图</param>
13 /// <param name="windowRadius">过滤半径</param>
14 public Bitmap ColorfulBitmapMedianFilterFunction(Bitmap srcBmp, int windowRadius, bool IsColorfulBitmap)
15 {
16 if (windowRadius < 1)
17 {
18 throw new Exception("过滤半径小于1没有意义");
19 }
20 //创建一个新的位图对象
21 Bitmap bmp = new Bitmap(srcBmp.Width, srcBmp.Height);
22
23 //存储该图片所有点的RGB值
24 byte[,] mR,mG,mB;
25 mR = new byte[srcBmp.Width, srcBmp.Height];
26 if (IsColorfulBitmap)
27 {
28 mG = new byte[srcBmp.Width, srcBmp.Height];
29 mB = new byte[srcBmp.Width, srcBmp.Height];
30 }
31 else
32 {
33 mG = mR;
34 mB = mR;
35 }
36
37 for (int i = 0; i <= srcBmp.Width - 1; i++)
38 {
39 for (int j = 0; j <= srcBmp.Height - 1; j++)
40 {
41 mR[i, j] = srcBmp.GetPixel(i, j).R;
42 if (IsColorfulBitmap)
43 {
44 mG[i, j] = srcBmp.GetPixel(i, j).G;
45 mB[i, j] = srcBmp.GetPixel(i, j).B;
46 }
47 }
48 }
49
50 mR = MedianFilterFunction(mR, windowRadius);
51 if (IsColorfulBitmap)
52 {
53 mG = MedianFilterFunction(mG, windowRadius);
54 mB = MedianFilterFunction(mB, windowRadius);
55 }
56 else
57 {
58 mG = mR;
59 mB = mR;
60 }
61 for (int i = 0; i <= bmp.Width - 1; i++)
62 {
63 for (int j = 0; j <= bmp.Height - 1; j++)
64 {
65 bmp.SetPixel(i, j, Color.FromArgb(mR[i, j], mG[i, j], mB[i, j]));
66 }
67 }
68 return bmp;
69 }
70
71 /// <summary>
72 /// 对矩阵M进行中值滤波
73 /// </summary>
74 /// <param name="m">矩阵M</param>
75 /// <param name="windowRadius">过滤半径</param>
76 /// <returns>结果矩阵</returns>
77 private byte[,] MedianFilterFunction(byte[,] m, int windowRadius)
78 {
79 int width = m.GetLength(0);
80 int height = m.GetLength(1);
81
82 byte[,] lightArray = new byte[width, height];
83
84 //开始滤波
85 for (int i = 0; i <= width - 1; i++)
86 {
87 for (int j = 0; j <= height - 1; j++)
88 {
89 //得到过滤窗口矩形
90 Rectangle rectWindow = new Rectangle(i - windowRadius, j - windowRadius, 2 * windowRadius + 1, 2 * windowRadius + 1);
91 if (rectWindow.Left < 0) rectWindow.X = 0;
92 if (rectWindow.Top < 0) rectWindow.Y = 0;
93 if (rectWindow.Right > width - 1) rectWindow.Width = width - 1 - rectWindow.Left;
94 if (rectWindow.Bottom > height - 1) rectWindow.Height = height - 1 - rectWindow.Top;
95 //将窗口中的颜色取到列表中
96 List<byte> windowPixelColorList = new List<byte>();
97 for (int oi = rectWindow.Left; oi <= rectWindow.Right - 1; oi++)
98 {
99 for (int oj = rectWindow.Top; oj <= rectWindow.Bottom - 1; oj++)
100 {
101 windowPixelColorList.Add(m[oi, oj]);
102 }
103 }
104 //排序
105 windowPixelColorList.Sort();
106 //取中值
107 byte middleValue = 0;
108 if ((windowRadius * windowRadius) % 2 == 0)
109 {
110 //如果是偶数
111 middleValue = Convert.ToByte((windowPixelColorList[windowPixelColorList.Count / 2] + windowPixelColorList[windowPixelColorList.Count / 2 - 1]) / 2);
112 }
113 else
114 {
115 //如果是奇数
116 middleValue = windowPixelColorList[(windowPixelColorList.Count - 1) / 2];
117 }
118 //设置为中值
119 lightArray[i, j] = middleValue;
120 }
121 }
122 return lightArray;
123 }
2 //作者:aaaSoft
3 //日期:2009年12月11日
4 //论坛:http://www.scbeta.com/bbs
5 //说明:原创文章,转载请注明出处并保留作者信息
6 //===================
7
8 /// <summary>
9 /// 中值滤波算法处理
10 /// </summary>
11 /// <param name="bmp">原始图片</param>
12 /// <param name="bmp">是否是彩色位图</param>
13 /// <param name="windowRadius">过滤半径</param>
14 public Bitmap ColorfulBitmapMedianFilterFunction(Bitmap srcBmp, int windowRadius, bool IsColorfulBitmap)
15 {
16 if (windowRadius < 1)
17 {
18 throw new Exception("过滤半径小于1没有意义");
19 }
20 //创建一个新的位图对象
21 Bitmap bmp = new Bitmap(srcBmp.Width, srcBmp.Height);
22
23 //存储该图片所有点的RGB值
24 byte[,] mR,mG,mB;
25 mR = new byte[srcBmp.Width, srcBmp.Height];
26 if (IsColorfulBitmap)
27 {
28 mG = new byte[srcBmp.Width, srcBmp.Height];
29 mB = new byte[srcBmp.Width, srcBmp.Height];
30 }
31 else
32 {
33 mG = mR;
34 mB = mR;
35 }
36
37 for (int i = 0; i <= srcBmp.Width - 1; i++)
38 {
39 for (int j = 0; j <= srcBmp.Height - 1; j++)
40 {
41 mR[i, j] = srcBmp.GetPixel(i, j).R;
42 if (IsColorfulBitmap)
43 {
44 mG[i, j] = srcBmp.GetPixel(i, j).G;
45 mB[i, j] = srcBmp.GetPixel(i, j).B;
46 }
47 }
48 }
49
50 mR = MedianFilterFunction(mR, windowRadius);
51 if (IsColorfulBitmap)
52 {
53 mG = MedianFilterFunction(mG, windowRadius);
54 mB = MedianFilterFunction(mB, windowRadius);
55 }
56 else
57 {
58 mG = mR;
59 mB = mR;
60 }
61 for (int i = 0; i <= bmp.Width - 1; i++)
62 {
63 for (int j = 0; j <= bmp.Height - 1; j++)
64 {
65 bmp.SetPixel(i, j, Color.FromArgb(mR[i, j], mG[i, j], mB[i, j]));
66 }
67 }
68 return bmp;
69 }
70
71 /// <summary>
72 /// 对矩阵M进行中值滤波
73 /// </summary>
74 /// <param name="m">矩阵M</param>
75 /// <param name="windowRadius">过滤半径</param>
76 /// <returns>结果矩阵</returns>
77 private byte[,] MedianFilterFunction(byte[,] m, int windowRadius)
78 {
79 int width = m.GetLength(0);
80 int height = m.GetLength(1);
81
82 byte[,] lightArray = new byte[width, height];
83
84 //开始滤波
85 for (int i = 0; i <= width - 1; i++)
86 {
87 for (int j = 0; j <= height - 1; j++)
88 {
89 //得到过滤窗口矩形
90 Rectangle rectWindow = new Rectangle(i - windowRadius, j - windowRadius, 2 * windowRadius + 1, 2 * windowRadius + 1);
91 if (rectWindow.Left < 0) rectWindow.X = 0;
92 if (rectWindow.Top < 0) rectWindow.Y = 0;
93 if (rectWindow.Right > width - 1) rectWindow.Width = width - 1 - rectWindow.Left;
94 if (rectWindow.Bottom > height - 1) rectWindow.Height = height - 1 - rectWindow.Top;
95 //将窗口中的颜色取到列表中
96 List<byte> windowPixelColorList = new List<byte>();
97 for (int oi = rectWindow.Left; oi <= rectWindow.Right - 1; oi++)
98 {
99 for (int oj = rectWindow.Top; oj <= rectWindow.Bottom - 1; oj++)
100 {
101 windowPixelColorList.Add(m[oi, oj]);
102 }
103 }
104 //排序
105 windowPixelColorList.Sort();
106 //取中值
107 byte middleValue = 0;
108 if ((windowRadius * windowRadius) % 2 == 0)
109 {
110 //如果是偶数
111 middleValue = Convert.ToByte((windowPixelColorList[windowPixelColorList.Count / 2] + windowPixelColorList[windowPixelColorList.Count / 2 - 1]) / 2);
112 }
113 else
114 {
115 //如果是奇数
116 middleValue = windowPixelColorList[(windowPixelColorList.Count - 1) / 2];
117 }
118 //设置为中值
119 lightArray[i, j] = middleValue;
120 }
121 }
122 return lightArray;
123 }