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)

 

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

    '灰度:均值法
    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, G, 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, sourceImg.Height)
        Dim R, G, 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, j, pDestColor)
            Next
        Next
        picDest.Image = destImg
    End Sub

处理结果:

 

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值