PhotoShop图像处理软件最引人之处莫过于它的滤镜功能,如果你掌握了一些VB的知识,那么你也能够设计出图像处理大师级的滤镜效果。
首先你必须了解VB中图像处理的一些基本知识。在VB中有两个操作像素的基本方法:PSet和Point。PSet生成像素;Point则读取像素值。并且显示器上所能显示的所有颜色都可以用RGB值来表示,VB中提供了RGB()函数,有三个变量RGB(Red,Green,Blue),如RGB(255,0,0)表示红色,RGB(255,255,0)表示黄色等。 VB中没有提供将一个像素点的颜色返回成RGB值的函数,但我们可以用以下方法获得某一点的RGB值:
Pi& = Picture.Point(X, Y)
Red = Pi& Mod 256
Green = ((pi& And &HFF00) / 256&) Mod 256&
Blue = (pi1& And &HFF0000) / 65536
有了以上知识,再通过一些滤镜算法便可以产生一些很不错的滤镜效果。在此以浮雕、木刻、油画和灯光共四个滤镜效果为例。
具体操作步骤如下:
新建一个窗体 Form1,在图体上建立一个Picture1图像框,将其AutoSize的值设为 Ture,然后用Picture1的LoadPicture()命令调入一幅图像,再通过各种事件调用过滤过程(这里以Picture1_Click()事件来调用)。
以下为具体代码:
Private Sub Form_Load()
Form1.AutoRedraw = True
Form1.ScaleMode = 3
Picture1.AutoRedraw = True
Picture1.ScaleMode = 3
Picture1.Picture = LoadPicture(图像文件全途径名)
End Sub
浮雕
浮雕的算法是在相邻像素的差值上加上一个常数,使黑暗区域增加一些亮度,我们可以取同一行、同一列或对角线上的相邻像素间的差值加上一个常数。
Private Sub Picture1_Click()
Dim pi1&, pi2&
Dim x, y
Dim A, B As Integer
Dim Red, Green, Blue As Integer
A = 1
B = 1
xx = Picture1.ScaleWidth
yy = Picture1.ScaleHeight
For x = 1 To xx - 2
For y = 1 To yy - 2
pi1& = Picture1.Point(x, y)
pi2& = Picture1.Point(x + A, y + B)
Red = Abs((pi1& Mod 256) - (pi2& Mod 256) + 128)
Green = Abs((((pi1& And &HFF00) / 256&) Mod 256&) - (((pi2& And &HFF00) / 256&) Mod 256&) + 128)
Blue = Abs(((pi1& And &HFF0000) / 65536) - ((pi2& And &HFF0000) / 65536) + 128)
Picture1.PSet (x, y), RGB(Red, Green, Blue)
Next y
Next x
Picture1.Refresh
End Sub
油画
油画滤镜的算法是:用当前点四周一定范围内任一点的颜色来代替当前点的颜色。
Private Sub Picture1_Click()
Dim pi&
Dim x, y
Dim A, B As Integer
Dim Red, Green, Blue As Integer
xx = Picture1.ScaleWidth
yy = Picture1.ScaleHeight
For x = 2 To xx - 3
For y = 2 To yy - 3
A = Rnd * 3 - 1
B = Rnd * 3 - 1
pi = Picture1.Point(x + A, y + B)
Red = (pi& Mod 256)
Green = (((pi& And &HFF00) / 256&) Mod 256&)
Blue = ((pi& And &HFF0000) / 65536)
Picture1.PSet (x, y), RGB(Red, Green, Blue)
Next y
DoEvents
Next x
Picture1.Refresh
End Sub
木刻(图三)
这个滤镜的算法相对简单一点。只需判断当前点是浅色还是深色(即三颜色元素的平均值是否大于128),浅色用白色RGB(255,255,255)代替;深色用黑色RGB(0,0,0)代替。
Private Sub Picture1_Click()
Dim pi&
Dim x, y
Dim A, B As Integer
Dim Red, Green, Blue As Integer
A = 1
B = 1
xx = Picture1.ScaleWidth
yy = Picture1.ScaleHeight
For x = 0 To xx
For y = 0 To yy
pi = Picture1.Point(x, y)
Red = (pi& Mod 256)
Green = (((pi& And &HFF00) / 256&) Mod 256&)
Blue = ((pi& And &HFF0000) / 65536)
If (Red + Green + Blue) / 3 < 128 Then
Picture1.PSet (x, y), RGB(0, 0, 0)
Else
Picture1.PSet (x, y), RGB(255, 255, 255)
End If
Next y
Next x
Picture1.Refresh
End Sub
灯光
灯光滤镜的算法很多,这里介绍一种小口径灯光滤镜,具体算法是取一点为光源(这里以30,40为例),从光线末端开始向光源点逐渐增加亮度(向白色接近)。代码为:
Private Sub Picture1_Click()
Dim pi1&, pi2&
Dim x, y
Dim A, B As Integer
Dim Red, Green, Blue As Integer
A = 30
B = 40
xx = Picture1.ScaleWidth
yy = Picture1.ScaleHeight
For x = 1 To xx
For y = 1 To yy
pi1 = Picture1.Point(x, y)
If Sqr((A - x) * (A - x) + (B - y) * (B - y)) - 40 < 0 Then
Red = ((pi1& Mod 256) + 200 * (1 - (Sqr((A - x) * (A - x) + (B - y) * (B - y)) + 1) / 40))
Green = ((((pi1& And &HFF00) / 256&) Mod 256&) + 200 * (1 - (Sqr((A - x) * (A - x) + (B - y) * (B - y)) + 1) / 40))
Blue = (((pi1& And &HFF0000) / 65536) + 200 * (1 - (Sqr((A - x) * (A - x) + (B - y) * (B - y)) + 1) / 40))
If Red < 0 Then Red = 0
If Red > 255 Then Red = 255
If Green < 0 Then Green = 0
If Green > 255 Then Green = 255
If Blue < 0 Then Blue = 0
If Blue > 255 Then Blue = 255
Picture1.PSet (x, y), RGB(Red, Green, Blue)
End If
Next y
Next x
Picture1.Refresh
End Sub
文章来源于领测软件测试网 https://www.ltesting.net/