vb.net 教程 5-16 图像处理之ImageAttributes 类1 伽玛、阈值和颜色替换

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
ImageAttributes 类:包含有关在呈现时如何操作位图和图元文件颜色的信息

ImageAttributes使用简单,先是通过ImageAttributes 提供的方法快速处理图片颜色,再使用Graphics的DrawImage()方法指定imageAttributes参数即可。

 

以下用代码方式来说明ImageAttributes 的一些方法,使用代码前,必须 Imports System.Drawing.Imaging

 

改变图像伽玛值,注意典型值为 gamma 参数是从 1.0 到 2.2 ; 但是,往往在 0.1 到 5.0 的值可能会证明在某些情况下很有用,代码中使用的是0.5:

    '伽玛
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)

        Dim imageAttributes As New ImageAttributes()
        imageAttributes.SetGamma(0.5F)
        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

运行效果:

 

改变图像阈值,阈值大小应介于 0 和 1,代码中使用的是0.7:

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)

        Dim imageAttributes As New ImageAttributes()
        imageAttributes.SetThreshold(0.7F)
        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 Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)

        Dim mycolormap(0) As ColorMap
        mycolormap(0) = New ColorMap()

        mycolormap(0).OldColor = Color.Black
        mycolormap(0).NewColor = Color.Red

        Dim imageAttributes As New ImageAttributes()
        imageAttributes.SetRemapTable(mycolormap)
        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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用 tesseract 进行 OCR 的 VB.NET 代码示例: ```vb Imports System Imports System.IO Imports System.Drawing Imports System.Drawing.Imaging Imports System.Diagnostics Imports System.Runtime.InteropServices Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim inputFile As String = "test.png" Dim outputFile As String = "output.txt" Dim tesseractPath As String = "C:\Program Files (x86)\Tesseract-OCR\tesseract.exe" ' Convert the input image to grayscale Dim bmp As New Bitmap(inputFile) Dim grayBmp As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format8bppIndexed) Using g As Graphics = Graphics.FromImage(grayBmp) Dim colorMatrix As New ColorMatrix(New Single()() _ {New Single() {0.3F, 0.3F, 0.3F, 0, 0}, New Single() {0.59F, 0.59F, 0.59F, 0, 0}, New Single() {0.11F, 0.11F, 0.11F, 0, 0}, New Single() {0, 0, 0, 1, 0}, New Single() {0, 0, 0, 0, 1}}) Dim ia As New ImageAttributes() ia.SetColorMatrix(colorMatrix) g.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), _ 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia) End Using ' Save the grayscale image to a file grayBmp.Save("temp.tif", Imaging.ImageFormat.Tiff) ' Run tesseract on the grayscale image Dim processInfo As New ProcessStartInfo() processInfo.FileName = tesseractPath processInfo.Arguments = "temp.tif " & outputFile & " -l eng" processInfo.CreateNoWindow = True processInfo.UseShellExecute = False processInfo.RedirectStandardOutput = True processInfo.RedirectStandardError = True Dim process As Process = Process.Start(processInfo) process.WaitForExit() ' Load the output text file and display the result Dim outputText As String = File.ReadAllText(outputFile) MessageBox.Show(outputText) ' Delete the temporary files File.Delete("temp.tif") File.Delete(outputFile) End Sub End Class ``` 请注意,您需要在计算机上安装 tesseract OCR 引擎,并相应地更新 `tesseractPath` 变量。此外,您也可以根据需要更改语言设置

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值