vb.net 教程 5-13 图像处理之像素处理 3

前端之家收集整理的这篇文章主要介绍了vb.net 教程 5-13 图像处理之像素处理 3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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)


下面代码使用第二种和第三种方法

  1. '灰度:均值法
  2. Private Sub btnGray1_Click(sender As Object,e As EventArgs) Handles btnGray1.Click
  3. Dim pSourceColor As Color
  4. Dim pDestColor As Color
  5.  
  6. Dim destImg As New Bitmap(sourceImg.Width,sourceImg.Height)
  7. Dim R,B As Integer
  8. Dim gray As Integer
  9. For i As Integer = 0 To sourceImg.Width - 1
  10. For j As Integer = 0 To sourceImg.Height - 1
  11. pSourceColor = sourceImg.GetPixel(i,j)
  12. R = pSourceColor.R
  13. G = pSourceColor.G
  14. B = pSourceColor.B
  15. gray = (R + G + B) / 3
  16. pDestColor = Color.FromArgb(gray,gray,gray)
  17. destImg.SetPixel(i,j,pDestColor)
  18. Next
  19. Next
  20. picDest.Image = destImg
  21. End Sub

处理结果:


  1. '灰度:指数加权法
  2. Private Sub btnGray2_Click(sender As Object,e As EventArgs) Handles btnGray2.Click
  3. Dim pSourceColor As Color
  4. Dim pDestColor As Color
  5.  
  6. Dim destImg As New Bitmap(sourceImg.Width,B As Integer
  7. Dim y As Integer
  8. For i As Integer = 0 To sourceImg.Width - 1
  9. For j As Integer = 0 To sourceImg.Height - 1
  10. pSourceColor = sourceImg.GetPixel(i,j)
  11. R = pSourceColor.R
  12. G = pSourceColor.G
  13. B = pSourceColor.B
  14. y = R * 0.3 + G * 0.59 + B * 0.11
  15. pDestColor = Color.FromArgb(y,y,y)
  16. destImg.SetPixel(i,pDestColor)
  17. Next
  18. Next
  19. picDest.Image = destImg
  20. End Sub

处理结果:

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

猜你在找的VB相关文章