
正文
关于vb.net实现图像处理的信息
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
VB.net如何对picturebox1的图片进行高斯模糊?
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
''' summary
''' Summary description for TextShadow
''' /summary
''' remarks/remarks
Public Class gaoshiBLUR
Public newbmp As Bitmap
StructLayout(LayoutKind.Explicit) Structure rgbA
FieldOffset(0) Public R As Byte
FieldOffset(1) Public G As Byte
FieldOffset(2) Public B As Byte
FieldOffset(3) Public A As Byte
FieldOffset(0) Public col As Integer
End Structure
Private m_radius As Integer = 5
''' summary
''' 高斯卷积矩阵
''' /summary
''' remarks/remarks
Private gaussMatrix As Integer()
''' summary
''' 卷积核
''' /summary
''' remarks/remarks
Private nuclear As Integer = 0
''' summary
''' 模糊半径
''' /summary
''' value/value
''' returns/returns
''' remarks/remarks
Public Property Radius() As Integer
Get
Return m_radius
End Get
Set(ByVal Value As Integer)
If (m_radius Value) Then
m_radius = Value
MakeGaussMatrix()
End If
End Set
End Property
''' summary
''' 高斯模糊
''' /summary
''' param name="bmp"要处理的图像/param
''' remarks/remarks
Public Sub MaskShadow(ByVal bmp As Bitmap)
If nuclear = 0 Then MakeGaussMatrix()
Dim rt As Rectangle = New Rectangle(0, 0, bmp.Width, bmp.Height)
' 克隆临时位图,作为卷积源
Dim tmp As Bitmap = bmp.Clone()
Dim dest As BitmapData = bmp.LockBits(rt, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
Dim source As BitmapData = tmp.LockBits(rt, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)
Debug.Print(Radius)
Debug.Print(dest.Width.ToString)
Debug.Print(nuclear)
Dim Number As Integer = (bmp.Height * dest.Stride - 1) / 4 ' 图像数据元素的个数,注意.net中数组下标是从0开始的
Dim bmpdata(Number) As Integer
Dim tmpdata(Number) As Integer
Dim TMPrgb(Number) As rgbA '临时参考颜色
Dim BMPrgb(Number) As rgbA '计算结果后的颜色
'ReDim bmpdata(Number)
'ReDim TMPrgb(Number)
Marshal.Copy(source.Scan0, tmpdata, 0, Number)
Dim i As Long
Dim j As Long
j = Number
Dim w, h As Long
Dim yi, xi As Long
Dim iw, ih, iiw, iih, iii As Long
Dim k As Long
Dim nn As Integer = (Radius * 2 + 1) ^ 2
Dim n As Integer = Radius * 2 + 1
w = bmp.Width
h = bmp.Height
' System.Array.Copy(TMPrgb, tmpdata, j)
For i = 0 To j
TMPrgb(i).col = tmpdata(i)
Next i
i = 0
For i = 0 To j
ih = Int(i / w)
iw = i - ih * w
Dim r As Double = 0
Dim g As Double = 0
Dim b As Double = 0
Dim a As Double = 0
Dim weight As Double
For k = 0 To nn
'需要解决的是周边遍历颜色值然后相加(r=r1*weight1+r2*weight2+r3*weight3+r4*weight4+r5*weight5+...r*weight)
yi = Int(k / n)
xi = k - yi * n
yi -= Radius
xi -= Radius
iiw = iw + xi
iih = ih + yi
'yi = Int(k / n)
'xi = k - yi * n
'iih = ih + yi - Radius
'iiw = iw + xi - Radius
If (iiw 0 OrElse iih 0) Or (iiw w - 1 OrElse iih h - 1) Then
iiw = iw
iih = ih
iii = i
Else
iii = iih * w
iii += iiw
End If
weight = gaussMatrix(k) / 1000
r += TMPrgb(iii).R * weight
g += TMPrgb(iii).G * weight
b += TMPrgb(iii).B * weight
a += TMPrgb(iii).A * weight
Next
'TMPrgb(i).col = tmpdata(i)
'r = TMPrgb(i).R * weight
'g = TMPrgb(i).G * weight
'b = TMPrgb(i).B * weight
BMPrgb(i).R = IIf(r 255, 255, r)
BMPrgb(i).G = IIf(g 255, 255, g)
BMPrgb(i).B = IIf(b 255, 255, b)
BMPrgb(i).A = IIf(a 255, 255, a)
bmpdata(i) = BMPrgb(i).col
'Debug.Print(TMPrgb(i).R)
Next
Marshal.Copy(bmpdata, 0, dest.Scan0, Number)
tmp.UnlockBits(source)
bmp.UnlockBits(dest)
newbmp = bmp.Clone
tmp.Dispose()
' End Try
End Sub
''' summary
''' 高斯卷积矩阵
''' /summary
''' remarks/remarks
Protected Sub MakeGaussMatrix()
Dim Q As Double = Radius / 2
If (Q = 0.0) Then Q = 0.1
Dim n As Integer = Radius * 2 + 1
Dim index As Integer = 0
nuclear = 0
ReDim gaussMatrix(n * n)
Dim x As Integer
Dim y As Integer
For x = -Radius To Radius
For y = -Radius To Radius
gaussMatrix(index) = Math.Round(Math.Exp(-((x * x + y * y)) / (2.0 * Q * Q)) / (2.0 * Math.PI * Q * Q) * 1000.0)
nuclear += gaussMatrix(index)
index += 1
Next
Next
End Sub
End Class
使用方法.
Dim bmp As Bitmap = PictureBox1.Image.Clone
Dim x As New gaoshiBLUR
x.Radius = 30
x.MaskShadow(bmp)
PictureBox2.Image = x.newbmp.Clone
相关问答
Q1: 如何正确掌握VB.NET操作缩放图像
在VB.NET操作缩放图像中的显示和保存缩放图像,用到Image和Graphics类,在VSDotNet2K3下面Reference里自动添加了引用System.Drawing,直接用就行。
实现VB.NET操作缩放图像代码如下:DimimgAsImageImage=Image.FromFile
(D:\Image\tstImage.jpg)
''tstImage是原先的图片DimgrfxAsGraphics=Me
.CreateGraphics
grfx.DrawImage(img,0,0,img.Width*
3,img.Height*3)''在Form里显示
DimimgnewAsNewSystem.Drawing.Bitmap
(img,img.Height*3,img.Width*3)
''新建一个放大的图片
imgnew.Save(D:\Image\tstNewImage.jpg,
System.Drawing.Imaging.ImageFormat.Jpeg)
''保存放大后图片
你可以建一个Form,然后在Form里拖进一个Button,把上面的代码放在Button_Click事件里面源码天空
,执行就行了。
对上面VB.NET操作缩放图像代码的解释:
1.要获取Graphics对象只能从某一事件的参数中获取或者使用窗体和控件对象的CreateGraphics方法来获取-----上面代码使用Me.CreateGraphics来引用这个对象。
2.加载一个图片用Image类的FromFile或者FromStream方法
3.用DrawImage来显示一个图片,该方法有30多个重载方法,可以查MSDN了解细节。
4.保存时的一个问题:我们必须先建一个对象,用于存缩放图像。
Q2: 几种经典的二值化方法及其vb.net实现
图像二值化的目的是最大限度的将图象中感兴趣的部分保留下来,在很多情况下,也是进行图像分析、特征提取与模式识别之前的必要的图像预处理过程。这个看似简单的问题,在过去的四十年里受到国内外学者的广泛关注,产生了数以百计的阈值选取方法,但如同其他图像分割算法一样,没有一个现有方法对各种各样的图像都能得到令人满意的结果。
本文针对几种经典而常用的二值发放进行了简单的讨论并给出了其vb.net 实现。
1、P-Tile法
Doyle于1962年提出的P-Tile (即P分位数法)可以说是最古老的一种阈值选取方法。该方法根据先验概率来设定阈值,使得二值化后的目标或背景像素比例等于先验概率,该方法简单高效,但是对于先验概率难于估计的图像却无能为力。
2、OTSU 算法(大津法)
OSTU算法可以说是自适应计算单阈值(用来转换灰度图像为二值图像)的简单高效方法。1978 OTSU年提出的最大类间方差法以其计算简单、稳定有效,一直广为使用。
3、迭代法(最佳阀值法)
(1). 求出图象的最大灰度值和最小灰度值,分别记为Zl和Zk,令初始阈值为:
(2). 根据阈值TK将图象分割为前景和背景,分别求出两者的平均灰度值Z0和ZB:
式中,Z(i,j)是图像上(i,j)点的象素值,N(i,j)是(i,j)点的权值,一般取1。
(3). 若TK=TK+1,则所得即为阈值,否则转2,迭代计算。
4、一维最大熵阈值法
它的思想是统计图像中每一个灰度级出现的概率 ,计算该灰度级的熵 ,假设以灰度级T分割图像,图像中低于T灰度级的像素点构成目标物体(O),高于灰度级T的像素点构成背景(B),那么各个灰度级在本区的分布概率为:
O区: i=1,2……,t
B区: i=t+1,t+2……L-1
上式中的 ,这样对于数字图像中的目标和背景区域的熵分别为:
对图像中的每一个灰度级分别求取W=H0 +HB,选取使W最大的灰度级作为分割图像的阈值,这就是一维最大熵阈值图像分割法。
Q3: 在vb.net环境下图像处理,用什么建立3D
首先,还是谈谈图像像素时数据获取方面吧,.net中的图像相关类基本上都是基于GDI+的,因此,图像数据的获取其实也是调用GDI+的一些函数。这个函数就是LockBits,在vb.net中彩色图像数据的快速获取 一文中,vb.net实现图像处理我们是调用vb.net实现图像处理了Marshal.Copy把LockBits锁定的内存数据拷贝到数据中,然后对数组中的值进行处理。这样做主要的原因是VB.NET不好直接访问内存(Marshal.ReadByte之类的函数不适合用于大型的循环中)。那么,这就造成了2个不好的事情,第一vb.net实现图像处理:在同一时间需要2倍于图像数据量的内存,第二:内存数据拷贝到数据,以及处理后再把数组的数据拷贝会内存中都是会减低速度的。作为一种改进,我们应该充分利用LockBits的功能。LockBits中的LockMode中有一种模式为ImageLockMode.UserInputBuffer,该模式下需要用户先申请内存,然后在把图像数据按照相关格式填充如这个内存中。这样,就可以先定义个数组,然后把图像数据填充到这个数组中,就避免了来回拷贝的耗时了,简单示例代码如下:
Dim BmpData As New BitmapData
Stride = ((Bmp.Width * 3 + 3) And HFFFFFFFC)
Dim PixleValue(Stride * Bmp.Height) As Byte
Dim Hanlde As GCHandle = GCHandle.Alloc(PixleValue, GCHandleType.Pinned)
BmpData.Scan0 = Hanlde.AddrOfPinnedObject()
Q4: 在VB.NET下面如何显示和保存缩放图像
实现代码如下:
Dim img As Image = Image.FromFile("D:\Image\tstImage.jpg") 'tstImage是原先的图片
Dim grfx As Graphics = Me.CreateGraphics
grfx.DrawImage(img, 0, 0, img.Width * 3, img.Height * 3) '在Form里显示
Dim imgnew As New System.Drawing.Bitmap(img, img.Height * 3, img.Width * 3) '新建一个放大的图片
imgnew.Save("D:\Image\tstNewImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) '保存放大后图片
你可以建一个Form,然后在Form里拖进一个Button,把上面的代码放在Button_Click事件里面,执行就行了。
对上面代码的解释:
1.要获取Graphics对象只能从某一事件的参数中获取或者使用窗体和控件对象的CreateGraphics方法来获取-----上面代码使用Me.CreateGraphics来引用这个对象。
2.加载一个图片用Image类的FromFile或者FromStream方法
3.用DrawImage来显示一个图片,该方法有30多个重载方法,可以查MSDN了解细节。
4.保存时的一个问题:我们必须先建一个对象,用于存缩放图像。
关于vb.net实现图像处理和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








