版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
说实在的,由于效率问题,vb一直对于处理视频、图像之类都处于劣势。网上介绍c++处理图像的网页很多,书也很多。相对的,vb的比较少。
当然,并不意味着vb不能用来处理图像,这篇开始,我介绍一些主要的图像处理的方法,在后面的教程中,我还要介绍一些提高效率的方法。
但是还是要先请从像素法开始,因为这个方法更好理解算法。
vb.net处理图像,主要使用到Bitmap 类的 GetPixel() 方法和SetPixel()方法。
大致的窗体,上面的图片框显示原图片,下面的图片框显示处理后的图片。
1、前期准备
a、由于图片需要多次处理,定义一个窗体级的bitmap对象
Dim sourceImg As Bitmap
b、载入图片代码,考虑到窗体载入的时候比paint早,为了简化,在按下按钮时显示在图片框内,同时简化使用固定图片演示。
Private Sub formMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
sourceImg = Image.FromFile("d:\15.jpg")
End Sub
Private Sub btnLoadimg_Click(sender As Object, e As EventArgs) Handles btnLoadimg.Click
picSource.Image = sourceImg
End Sub
2、RGB通道的分离:
使用GetPixel()在原图片上取色,然后使用SetPixel(),
原图像:颜色值color=RGB
处理后的图像:
红色通道:color=RRR
绿色通道:color=GGG
蓝色通道:color=BBB
代码如下:
'红色通道
Private Sub btnRed_Click(sender As Object, e As EventArgs) Handles btnRed.Click
Dim pSourceColor As Color
Dim pDestColor As Color
Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)
Dim R, G, B 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
pDestColor = Color.FromArgb(R, R, R)
destImg.SetPixel(i, j, pDestColor)
Next
Next
picDest.Image = destImg
End Sub
显示如下:
'绿色通道
Private Sub btnGreen_Click(sender As Object, e As EventArgs) Handles btnGreen.Click
Dim pSourceColor As Color
Dim pDestColor As Color
Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)
Dim R, G, B 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
pDestColor = Color.FromArgb(G, G, G)
destImg.SetPixel(i, j, pDestColor)
Next
Next
picDest.Image = destImg
End Sub
显示如下:
'蓝色通道
Private Sub btnBlue_Click(sender As Object, e As EventArgs) Handles btnBlue.Click
Dim pSourceColor As Color
Dim pDestColor As Color
Dim destImg As New Bitmap(sourceImg.Width, sourceImg.Height)
Dim R, G, B 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
pDestColor = Color.FromArgb(B, B, B)
destImg.SetPixel(i, j, pDestColor)
Next
Next
picDest.Image = destImg
End Sub
显示如下:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参vb.net教程目录
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。