当前位置: 高中信息技术 / 综合题
  • 1. (2021·浙江模拟) 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan),以获得更好的压缩比。给定一个n×n的矩阵,Z字形扫描的过程如下图所示:

    经过扫描后得到的数据结果为:

    57

    45

    0

    23

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    -30

    1

    0

    0

    0

    0

    0

    -16

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    “57,45,0,0,0,0,23,0,-30,-16,0,0,1,0,0,0,0,…,0,0”,数据元素个数为64个。进一步经过压缩后,最终得到行程编码:“57, 1, 45, 1, 0, 4, 23, 1, 0, 1,-30, 1,-16, 1, 0, 2, 1, 1, 0, 51”,数据元素个数为20个。

    1. (1) 行程编码压缩最坏情况下,数据压缩后数据元素的数量将是原来的倍。
    2. (2) 根据上述扫描算法,其 VB 代码实现如下,请在划线处填入合适的代码。

      Dim a(0 To 1000) As Integer     '存储原矩阵数据,按行优先存储

      Dim b(0 To 1000) As Integer     '存储Z形扫描后数据

      Dim c(0 To 1000) As Integer     '存储行程编码压缩后数据

      Dim n As Integer

      ‘矩阵导入代码略,以行优先存储在a数组中,如例子中数据存储顺序为“57,45,0,23,0,0…”

      Private Sub Command2_Click()

          Dim choice As Integer     ' 1:向右移动 ;2:向右上移动;3向下移动 4向左下移动

          Dim row As Integer, col As Integer, i As Integer, j As Integer

          Dim pre As Integer, count As Integer

          choice = 1: row = 0: col = 0: i = 0

          Do While (row <> n - 1 Or col <> n - 1)

              b(i) = a(row * n + col):i = i + 1     

              If choice = 1 Then

                  

                  If row = 0 Then choice = 4 Else choice = 2

              ElseIf choice = 2 Then

                  row = row - 1: col = col + 1

                  If  Then

                      choice = 1

                  ElseIf col = n - 1 Then

                      choice = 3

                  End If

              ElseIf choice = 3 Then

                  row = row + 1

                  If col = 0 Then choice = 2 Else choice = 4

                  ElseIf choice = 4 Then

                      row = row + 1: col = col - 1

                  If row = n - 1 Then

                      choice = 1

                  ElseIf col = 0 Then

                      choice = 3

                  End If

              End If

          Loop

          b(i) = a(n * n - 1):j = 0: pre = b(0): count = 0

          For i = 0 To n * n - 1                 '输出Z形序列,并进行行程压缩

              If pre = b(i) Then

                  count = count + 1

              Else

                  c(j) = pre: c(j + 1) = count

                  

                  pre = b(i):j = j + 2

              End If

          Next i

          c(j) = pre: c(j + 1) = count

          Text1.Text = ""

          For i = 0 To j + 1

              Text1.Text = Text1.Text + Str(c(i)) + ","

          Next i

      End Sub

微信扫码预览、分享更方便