vb.net 教程 5-13 图像处理之像素处理 3
发布时间:2020-12-17 07:34:32  所属栏目:百科  来源:网络整理 
            导读: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) 求出
                
                
                
            | 5、灰度 灰度的算法有几种: a、最大值法: 原图像:颜色值color=(R,G,B) 求出R,G,B中最大的值:Y=Max(R,G,B) 新图像:color=(Y,Y,Y) 原图像:颜色值color=(R,G,B) 求出R,G,B的平均值:Y=(R+B+G)/3 新图像:color=(Y,Y,Y) 将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 教程 目录 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
