C#图像处理(二值化,灰阶)
2012-08-17 14:26
351 查看
1. #region 灰阶
2. /// <summary>
3. /// 灰阶
4. /// </summary>
5. /// <param name="b">Bitmap对象</param>
6. /// <returns></returns>
7. public Bitmap Gray(Bitmap b)
8. {
9. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
10.
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
11.
int stride = bmData.Stride;
12.
System.IntPtr Scan0 = bmData.Scan0;
13.
unsafe
14.
{
15.
byte* p = (byte*)(void*)Scan0;
16.
int nOffset = stride - b.Width * 3;
17.
byte red, green, blue;
18.
for (int y = 0; y < b.Height; ++y)
19.
{
20.
for (int x = 0; x < b.Width; ++x)
21.
{
22.
blue = p[0];
23.
green = p[1];
24.
red = p[2];
25.
p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
26.
p += 3;
27.
}
28.
p += nOffset;
29.
}
30.
}
31.
b.UnlockBits(bmData);
32.
return b;
33.
}
34.
#endregion
35.
36.
#region 固定阈值法二值化模块
37.
38.
public Bitmap Threshoding(Bitmap b, byte threshold)
39.
{
40.
int width = b.Width;
41.
int height = b.Height;
42.
BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
43.
unsafe
44.
{
45.
byte* p = (byte*)data.Scan0;
46.
int offset = data.Stride - width * 4;
47.
byte R, G, B, gray;
48.
for (int y = 0; y < height; y++)
49.
{
50.
for (int x = 0; x < width; x++)
51.
{
52.
R = p[2];
53.
G = p[1];
54.
B = p[0];
55.
gray = (byte)((R * 19595 + G * 38469 + B * 7472) >> 16);
56.
if (gray >= threshold)
57.
{
58.
p[0] = p[1] = p[2] = 255;
59.
}
60.
else
61.
{
62.
p[0] = p[1] = p[2] = 0;
63.
}
64.
p += 4;
65.
}
66.
p += offset;
67.
}
68.
b.UnlockBits(data);
69.
return b;
70.
}
71.
72.
}
73.
#endregion
74.
75.
#region Otsu阈值法二值化模块
76.
/// <summary>
77.
/// Otsu阈值
78.
/// </summary>
79.
/// <param name="b">位图流</param>
80.
/// <returns></returns>
81.
public Bitmap OtsuThreshold(Bitmap b)
82.
{
83.
// 图像灰度化
84.
// b = Gray(b);
85.
int width = b.Width;
86.
int height = b.Height;
87.
byte threshold = 0;
88.
int[] hist = new int[256];
89.
90.
int AllPixelNumber = 0, PixelNumberSmall = 0, PixelNumberBig = 0;
91.
double MaxValue, AllSum = 0, SumSmall = 0, SumBig, ProbabilitySmall, ProbabilityBig, Probability;
92.
93.
BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
94.
unsafe
95.
{
96.
byte* p = (byte*)data.Scan0;
97.
int offset = data.Stride - width * 4;
98.
99.
for (int j = 0; j < height; j++)
100. {
101. for (int i = 0; i < width; i++)
102. {
103. hist[p[0]]++;
104.
105.
106. p += 4;
107. }
108. p += offset;
109. }
110. b.UnlockBits(data);
111.
112. }
113. //计算灰度为I的像素出现的概率
114. for (int i = 0; i < 256; i++)
115. {
116.
117. AllSum += i * hist[i]; // 质量矩
118. AllPixelNumber += hist[i]; // 质量
119.
120. }
121.
122. MaxValue = -1.0;
123. for (int i = 0; i < 256; i++)
124. {
125. PixelNumberSmall += hist[i];
126. PixelNumberBig = AllPixelNumber - PixelNumberSmall;
127. if (PixelNumberBig == 0)
128. {
129. break;
130. }
131.
132. SumSmall += i * hist[i];
133. SumBig = AllSum - SumSmall;
134. ProbabilitySmall = SumSmall / PixelNumberSmall;
135. ProbabilityBig = SumBig / PixelNumberBig;
136. Probability = PixelNumberSmall * ProbabilitySmall * ProbabilitySmall + PixelNumberBig * ProbabilityBig * ProbabilityBig;
137. if (Probability > MaxValue)
138. {
139. MaxValue = Probability;
140. threshold = (byte)i;
141. }
142.
143. }
144.
145. return this.Threshoding(b, threshold);
146. } // end of OtsuThreshold 2
147. #endregion
2. /// <summary>
3. /// 灰阶
4. /// </summary>
5. /// <param name="b">Bitmap对象</param>
6. /// <returns></returns>
7. public Bitmap Gray(Bitmap b)
8. {
9. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
10.
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
11.
int stride = bmData.Stride;
12.
System.IntPtr Scan0 = bmData.Scan0;
13.
unsafe
14.
{
15.
byte* p = (byte*)(void*)Scan0;
16.
int nOffset = stride - b.Width * 3;
17.
byte red, green, blue;
18.
for (int y = 0; y < b.Height; ++y)
19.
{
20.
for (int x = 0; x < b.Width; ++x)
21.
{
22.
blue = p[0];
23.
green = p[1];
24.
red = p[2];
25.
p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
26.
p += 3;
27.
}
28.
p += nOffset;
29.
}
30.
}
31.
b.UnlockBits(bmData);
32.
return b;
33.
}
34.
#endregion
35.
36.
#region 固定阈值法二值化模块
37.
38.
public Bitmap Threshoding(Bitmap b, byte threshold)
39.
{
40.
int width = b.Width;
41.
int height = b.Height;
42.
BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
43.
unsafe
44.
{
45.
byte* p = (byte*)data.Scan0;
46.
int offset = data.Stride - width * 4;
47.
byte R, G, B, gray;
48.
for (int y = 0; y < height; y++)
49.
{
50.
for (int x = 0; x < width; x++)
51.
{
52.
R = p[2];
53.
G = p[1];
54.
B = p[0];
55.
gray = (byte)((R * 19595 + G * 38469 + B * 7472) >> 16);
56.
if (gray >= threshold)
57.
{
58.
p[0] = p[1] = p[2] = 255;
59.
}
60.
else
61.
{
62.
p[0] = p[1] = p[2] = 0;
63.
}
64.
p += 4;
65.
}
66.
p += offset;
67.
}
68.
b.UnlockBits(data);
69.
return b;
70.
}
71.
72.
}
73.
#endregion
74.
75.
#region Otsu阈值法二值化模块
76.
/// <summary>
77.
/// Otsu阈值
78.
/// </summary>
79.
/// <param name="b">位图流</param>
80.
/// <returns></returns>
81.
public Bitmap OtsuThreshold(Bitmap b)
82.
{
83.
// 图像灰度化
84.
// b = Gray(b);
85.
int width = b.Width;
86.
int height = b.Height;
87.
byte threshold = 0;
88.
int[] hist = new int[256];
89.
90.
int AllPixelNumber = 0, PixelNumberSmall = 0, PixelNumberBig = 0;
91.
double MaxValue, AllSum = 0, SumSmall = 0, SumBig, ProbabilitySmall, ProbabilityBig, Probability;
92.
93.
BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
94.
unsafe
95.
{
96.
byte* p = (byte*)data.Scan0;
97.
int offset = data.Stride - width * 4;
98.
99.
for (int j = 0; j < height; j++)
100. {
101. for (int i = 0; i < width; i++)
102. {
103. hist[p[0]]++;
104.
105.
106. p += 4;
107. }
108. p += offset;
109. }
110. b.UnlockBits(data);
111.
112. }
113. //计算灰度为I的像素出现的概率
114. for (int i = 0; i < 256; i++)
115. {
116.
117. AllSum += i * hist[i]; // 质量矩
118. AllPixelNumber += hist[i]; // 质量
119.
120. }
121.
122. MaxValue = -1.0;
123. for (int i = 0; i < 256; i++)
124. {
125. PixelNumberSmall += hist[i];
126. PixelNumberBig = AllPixelNumber - PixelNumberSmall;
127. if (PixelNumberBig == 0)
128. {
129. break;
130. }
131.
132. SumSmall += i * hist[i];
133. SumBig = AllSum - SumSmall;
134. ProbabilitySmall = SumSmall / PixelNumberSmall;
135. ProbabilityBig = SumBig / PixelNumberBig;
136. Probability = PixelNumberSmall * ProbabilitySmall * ProbabilitySmall + PixelNumberBig * ProbabilityBig * ProbabilityBig;
137. if (Probability > MaxValue)
138. {
139. MaxValue = Probability;
140. threshold = (byte)i;
141. }
142.
143. }
144.
145. return this.Threshoding(b, threshold);
146. } // end of OtsuThreshold 2
147. #endregion
相关文章推荐
- C#图像处理(二值化,灰阶)
- 图像处理之常见二值化方法汇总
- .NET Core 图像处理(图像二值化,图像灰度化,图像灰度反转)
- 图像处理之常见二值化方法汇总
- 【OpenCV图像处理入门学习教程二】不同阈值二值化图像
- c#图像处理-二值化
- Matlab图像处理学习笔记(一):二值化、开操作、连通区域提取、重心、ROI
- 图像处理之常见二值化方法汇总
- WPF:图像处理(*)二值化演示程序及源代码下载
- Opencv实现图像的灰度处理,二值化,阀值选择
- 图像处理------常见二值化方法汇总 分类: 视频图像处理 2015-07-24 14:51 33人阅读 评论(0) 收藏
- 【数字图像处理】图像二值化,腐蚀膨胀,开闭运算,击中击不中变换
- OpenCV实现彩色图像转换为灰度图及二值化处理
- 2.2 BMP文件 灰度化 二值化 亮度调整有关BMP图像处理的底层函数
- 图像处理之常见二值化方法汇总
- 图像处理------常见二值化方法汇总
- 图像处理: 灰度化,二值化,反色
- 图像处理之二值化图像
- android图像处理之图像二值化
- 图像处理------常见二值化方法汇总