vb.net 教程 5-16 图像处理之ImageAttributes 类2 颜色矩阵3

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
继续上一节的讲解,看看之前 《vb.net 教程 5-16 图像处理之ImageAttributes 类1 颜色矩阵1》中讲到的其他几个特殊矩阵的使用。

    '仅绿色分量
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        Dim imageAttributes As New ImageAttributes()
        '仅绿色分量
        Dim imgMatrixElement()() As Single = {
          New Single() {0, 0, 0, 0, 0},
          New Single() {0, 1, 0, 0, 0},
          New Single() {0, 0, 0, 0, 0},
          New Single() {0, 0, 0, 1, 0},
          New Single() {0, 0, 0, 0, 0}
        }

        Dim imgMatrix As New ColorMatrix(imgMatrixElement)
        imageAttributes.SetColorMatrix(imgMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)

        Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)
        Dim g As Graphics = Graphics.FromImage(destImg)
        g.DrawImage(sourceImg, New Rectangle(0, 0, sourceImg.Width, sourceImg.Height), 0, 0, sourceImg.Width, sourceImg.Height,
                    GraphicsUnit.Pixel, imageAttributes)

        picDest.Image = destImg
    End Sub

 

    '仅蓝色分量
    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        Dim imageAttributes As New ImageAttributes()
        '仅蓝色分量
        Dim imgMatrixElement()() As Single = {
          New Single() {0, 0, 0, 0, 0},
          New Single() {0, 0, 0, 0, 0},
          New Single() {0, 0, 1, 0, 0},
          New Single() {0, 0, 0, 1, 0},
          New Single() {0, 0, 0, 0, 0}
        }

        Dim imgMatrix As New ColorMatrix(imgMatrixElement)
        imageAttributes.SetColorMatrix(imgMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)

        Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)
        Dim g As Graphics = Graphics.FromImage(destImg)
        g.DrawImage(sourceImg, New Rectangle(0, 0, sourceImg.Width, sourceImg.Height), 0, 0, sourceImg.Width, sourceImg.Height,
                    GraphicsUnit.Pixel, imageAttributes)

        picDest.Image = destImg
    End Sub

 

    '灰度:指数加权法
    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
        Dim imageAttributes As New ImageAttributes()
        '灰度
        Dim imgMatrixElement()() As Single = {
          New Single() {0.3, 0.3, 0.3, 0, 0},
          New Single() {0.59, 0.59, 0.59, 0, 0},
          New Single() {0.11, 0.11, 0.11, 0, 0},
          New Single() {0, 0, 0, 1, 0},
          New Single() {0, 0, 0, 0, 0}
        }

        Dim imgMatrix As New ColorMatrix(imgMatrixElement)
        imageAttributes.SetColorMatrix(imgMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)

        Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)
        Dim g As Graphics = Graphics.FromImage(destImg)
        g.DrawImage(sourceImg, New Rectangle(0, 0, sourceImg.Width, sourceImg.Height), 0, 0, sourceImg.Width, sourceImg.Height,
                    GraphicsUnit.Pixel, imageAttributes)

        picDest.Image = destImg
    End Sub

 

 

    '逆反
    Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
        Dim imageAttributes As New ImageAttributes()
        '逆反
        Dim imgMatrixElement()() As Single = {
          New Single() {-1, 0, 0, 0, 0},
          New Single() {0, -1, 0, 0, 0},
          New Single() {0, 0, -1, 0, 0},
          New Single() {0, 0, 0, 1, 0},
          New Single() {1, 1, 1, 0, 0}
        }

        Dim imgMatrix As New ColorMatrix(imgMatrixElement)
        imageAttributes.SetColorMatrix(imgMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)

        Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)
        Dim g As Graphics = Graphics.FromImage(destImg)
        g.DrawImage(sourceImg, New Rectangle(0, 0, sourceImg.Width, sourceImg.Height), 0, 0, sourceImg.Width, sourceImg.Height,
                    GraphicsUnit.Pixel, imageAttributes)

        picDest.Image = destImg
    End Sub


 

 

 

 

 

 

 

传入的矩阵不同,输出图像的颜色不同。

 

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

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值