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

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

浮雕的算法:

下一行的红绿蓝颜色分量减去上一行的红绿蓝颜色分量,再加上128

如果小于0,那么等于0,如果大于255,那么等于255

 

R=R(i-1,j)-R(i,j)+128

G=G(i-1,j)-G(i,j)+128

B=B(i-1,j)-B(i,j)+128

主要代码如下:

    '浮雕1
    Private Sub btnEmboss_Click(sender As Object, e As EventArgs) Handles btnEmboss.Click
        Dim pSourceColor1 As Color
        Dim pSourceColor2 As Color
        Dim pDestColor As Color

        Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)
        Dim R, G, B As Integer
        Dim R1, G1, B1 As Integer
        Dim R2, G2, B2 As Integer

        For i As Integer = 1 To sourceImg.Width - 1
            For j As Integer = 0 To sourceImg.Height - 1
                pSourceColor1 = sourceImg.GetPixel(i, j)
                R1 = pSourceColor1.R
                G1 = pSourceColor1.G
                B1 = pSourceColor1.B

                pSourceColor2 = sourceImg.GetPixel(i - 1, j)
                R2 = pSourceColor2.R
                G2 = pSourceColor2.G
                B2 = pSourceColor2.B

                R = R1 - R2 + 128
                G = G1 - G2 + 128
                B = B1 - B2 + 128

                If R < 0 Then R = 0
                If R > 255 Then R = 255
                If G < 0 Then G = 0
                If G > 255 Then G = 255
                If B < 0 Then B = 0
                If B > 255 Then B = 255

                pDestColor = Color.FromArgb(R, G, B)
                destImg.SetPixel(i, j, pDestColor)
            Next
        Next
        picDest.Image = destImg
    End Sub

 

运行效果如下:

我还看到了另外一个算法:

 

下一行的红绿蓝颜色分量减去上一行的红绿蓝颜色分量的绝对值,再加上128

如果小于0,那么等于0,如果大于255,那么等于255

 

R=Abs(R(i-1,j)-R(i,j))+128

G=Abs(G(i-1,j)-G(i,j))+128

 

B=Abs(B(i-1,j)-B(i,j))+128

主要代码如下:

 

    '浮雕2
    Private Sub btnEmboss2_Click(sender As Object, e As EventArgs) Handles btnEmboss2.Click
        Dim pSourceColor1 As Color
        Dim pSourceColor2 As Color
        Dim pDestColor As Color

        Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)
        Dim R, G, B As Integer
        Dim R1, G1, B1 As Integer
        Dim R2, G2, B2 As Integer

        For i As Integer = 1 To sourceImg.Width - 1
            For j As Integer = 0 To sourceImg.Height - 1
                pSourceColor1 = sourceImg.GetPixel(i, j)
                R1 = pSourceColor1.R
                G1 = pSourceColor1.G
                B1 = pSourceColor1.B

                pSourceColor2 = sourceImg.GetPixel(i - 1, j)
                R2 = pSourceColor2.R
                G2 = pSourceColor2.G
                B2 = pSourceColor2.B

                R = Math.Min(Math.Abs(R1 - R2) + 128, 255)
                G = Math.Min(Math.Abs(G1 - G2) + 128, 255)
                B = Math.Min(Math.Abs(B1 - B2) + 128, 255)

                pDestColor = Color.FromArgb(R, G, B)
                destImg.SetPixel(i, j, pDestColor)
            Next
        Next
        picDest.Image = destImg
    End Sub

其中Math.Min(A,B)将返回A和B中较小的值。

 

 

 

 

运行效果如下:

 

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

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

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值