计算机的基本功能是计算,信息加工处理本质上都是通过计算完成的。现有一图像模糊处理的算法,以10*10像素图像的模糊处理为例,具体算法如下:
1)各像素点的颜色信息值(介于0至255之间的一个正整数,由计算机随机生成),存储至数组a中,a的排列方式如图a所示。
图a |
2)给定模糊处理对分模糊中心位置p和模糊半径r(r>1)。
①确定模糊中心点在如下数阵中的位置。若模糊中心为36时,该模糊中心在数阵中对应位置为第4行第6列;
②根据中心点和模糊半径确定模糊处理的像素点的范围
3)依序将模糊处理范围内的像素点进行模糊处理。模糊处理的方式为:每个像素点的新颜色信息值为该像素点上下左右相邻四个像素点颜色信息值的平均值(取整数)。
图b |
程序运行时,列表框List1中显示图像模糊前的信息,在文本框Text1中输入模糊的中心点p,在文本框Text2中输入模糊的半径r,单击命令按钮Comman的后在列表框List2中输出模糊后的图像信息。程序运行界面如图b所示,椭圆标记的为模糊中心p,矩形框表示的为模糊范围。
实现上述功能的VB程序如下。请回答下列问题:
Const n = 10
Dim a(1 To 10 * 10) As Integer
Private Sub Command1_Click()
Dim p As Integer, r As Integer
Dim x As Integer, y As Integer
Dim i1 As Integer, i2 As Integer, j1 As Integer, j2 As Integer
Dim i As Integer, j As Integer
Dim k As Integer, k1 As Integer, k2 As Integer, k3 As Integer, k4 As Integer
Dim s As String
p = Val(Text1.Text)
r = Val(Text2.Text)
x = (p - 1) \ n + 1
①
i1 = x - r + 1: i2 = x + r - 1
j1 = y - r + 1: j2 = y + r - 1
If i1 < 1 Then i1 = 1: If i2 > n Then i2 = n
If j1 < 1 Then j1 = 1: If j2 > n Then j2 = n
For i = i1 To i2
For j = j1 To j2
k = (i - 1) * n + j
k1 = (i - 2) * n + j
k2 = i * n + j
k3 = (i - 1) * n + j - 1
k4 = (i - 1) * n + j + 1
a(k) = (a(k1) + a(k2) + a(k3) + a(k4)) \ 4
Next j
Next i
s = ""
For i = 1 To n * n
s = s + adj(a(i), 5)
If i Mod n = 0 Then
List2.AddItem s
s = ""
End If
Next i
End Sub
Private Sub Form_Load()
Dim i As Integer, j As Integer
Dim s As String
For i = 1 To n
s = ""
For j = 1 To n
②
s = s + adj(a((i - 1) * n + j), 5)
Next j
List1.AddItem s
Next i
End Sub
Function adj(x As Integer, y As Integer) As String
Dim t As String, i As Integer
t = Str(x)
For i = 1 To y - Len(t)
t = " " + t
Next i
③
End Function