5、灰度
灰度的算法有几种:
a、最大值法:
原图像:颜色值color=(R,G,B)
求出R,G,B中最大的值:Y=Max(R,G,B)
新图像:color=(Y,Y,Y)
b、平均值法: 使用每个像素点的 R,G,B值等于原像素点的RGB值的平均值;
原图像:颜色值color=(R,G,B)
求出R,G,B的平均值:Y=(R+B+G)/3
新图像:color=(Y,Y,Y)
c.、加权平均值法:
将R,G,B分别乘上一个权重值,通常为Y=R * 0.3 + G * 0.59 + B * 0.11
新图像:color=(Y,Y,Y)
'灰度:均值法 Private Sub btnGray1_Click(sender As Object,e As EventArgs) Handles btnGray1.Click Dim pSourceColor As Color Dim pDestColor As Color Dim destImg As New Bitmap(sourceImg.Width,sourceImg.Height) Dim R,B As Integer Dim gray As Integer For i As Integer = 0 To sourceImg.Width - 1 For j As Integer = 0 To sourceImg.Height - 1 pSourceColor = sourceImg.GetPixel(i,j) R = pSourceColor.R G = pSourceColor.G B = pSourceColor.B gray = (R + G + B) / 3 pDestColor = Color.FromArgb(gray,gray,gray) destImg.SetPixel(i,j,pDestColor) Next Next picDest.Image = destImg End Sub
处理结果:
'灰度:指数加权法 Private Sub btnGray2_Click(sender As Object,e As EventArgs) Handles btnGray2.Click Dim pSourceColor As Color Dim pDestColor As Color Dim destImg As New Bitmap(sourceImg.Width,B As Integer Dim y As Integer For i As Integer = 0 To sourceImg.Width - 1 For j As Integer = 0 To sourceImg.Height - 1 pSourceColor = sourceImg.GetPixel(i,j) R = pSourceColor.R G = pSourceColor.G B = pSourceColor.B y = R * 0.3 + G * 0.59 + B * 0.11 pDestColor = Color.FromArgb(y,y,y) destImg.SetPixel(i,pDestColor) Next Next picDest.Image = destImg End Sub
处理结果:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net 教程 目录