<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      在VB6.0中完成大面積不規則區域的填充

      [摘要]一、引言 區域填充是指先將區域內的一個像素 ,一般稱為種子點賦予給定的顏色和輝亮,然后將該顏色擴展到整個區域內的過程。 二、已有的填充算法及缺點 1.掃描線法 掃描線法可以實現已知多邊形域邊界的填充...
      一、引言
      區域填充是指先將區域內的一個像素 ,一般稱為種子點賦予給定的顏色和輝亮,然后將該顏色擴展到整個區域內的過程。
      二、已有的填充算法及缺點
      1.掃描線法
      掃描線法可以實現已知多邊形域邊界的填充,多邊形域可以是凹的、凸的、還可以是帶孔的。該填充方法是按掃描線的順序,計算掃描線與待填充區域的相交區間,再用要求的顏色顯示這些區間的像素,即完成填充工作。這里區間的端點通過計算掃描線與多邊形邊界線的交點獲得。所以待填充區域的邊界線必須事先知道,因此它的缺點是無法實現對未知邊界的區域填充。
      2.邊填充算法
      邊填充的基本思想是:對于每一條掃描線和每條多邊形邊的交點,將該掃描線上交點右方的所有像素取補。對多邊形的每條邊作些處理,多邊形的順序隨意。該算法適用于具有幀緩沖器的圖形系統,按任意順序處理多邊形的邊。處理每條邊時,僅訪問與該邊有交的掃描線上交點右方的像素。所有的邊都被處理之后,按掃描線順序讀出幀緩沖器的內容,送入顯示設備。該算法的優點是簡單,缺點是對于復雜圖形,每一像素可能被訪問多次,重要的是必須事先知道待填充多邊形的邊界,所以在填充未知邊界的區域時不適用。
      3.遞歸算法
      遞歸算法的優點是編程實現時,語言簡潔。但在VB6.0實際編程實現時,這種遞歸算法填充稍稍大一些的圖形就會出現堆棧溢出現象,據我們的實踐證明,遞歸算法只能連續遞歸深度在2090次左右,也就是說,如果待填充的圖形大于二千多個像素那么堆棧溢出。下面給出八連通填充方法的VB程序實現(四連通算法同理)。
      Public Sub area(p, q As Integer)
      If ((imagepixels(0, p, q) = red1) And (imagepixels(1, p, q) = green1) And (imagepixels(2, p, q) = blue1)) Then
      imagepixels(0, p, q) = 0: imagepixels(2, p, q) = 0: imagepixels(1, p, q) = 0
      Picture1.PSet (p, q), RGB(0, 0, 0)
      Call area(p + 1, q): Call area(p, q + 1)
      Call area(p - 1, q): Call area(p, q - 1)
      Call area(p + 1, q + 1): Call area(p + 1, q - 1)
      Call area(p - 1, q + 1): Call area(p - 1, q - 1)
      Else: Exit Sub
      End If
      End Sub
      三、 算法的基本思想
      本算法采用兩個隊列(FIFO)filled和unfilled來實現區域填充。設計步驟如下:
      1. 找出該區域內部任意一點,作為填充種子。
      2. 填充該點,并把該點存入隊列filled。
      3. 按逆時針,判斷該點的上、右、下、左鄰像素是否在filled隊列內。如果在filled,說明該相鄰點已填充,若不在filled隊列內,則判斷該相鄰點在未填充隊列unfilled,如果不在則將該相鄰點存入unfilled。
      4. 判斷未填充隊列是否為空,若不空,則從隊列unfilled中取出頭元素,轉向第三步。若為空則表示已完成所有像素填充,結束程序。
      四、 程序實現及說明
      本算法定義的隊列突破了遞歸算法中受堆棧空間大小的限制的束縛,因為它直接占用內存空間,與堆棧大小無關。以下源程序在Window 2000環境下用VB6.0編程實現。
      建立如圖所示標準窗體并畫上控件-2個CommandButton控件和一個PictureBox控件,調整大小,并設置控件的屬性。
      4.1 通用聲明
      Dim Xx As Integer, Yy As Integer
      Dim Array1(9000, 2), Array2(9000, 2) As Integer
      4.2 采集
      Private Sub Command1_Click()
      Picture1.MousePointer = 2
      End Sub
      4.3 選取種子
      Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      Xx = X '選擇并記錄種子點的位置
      Yy = Y
      End Sub
      4.4 區域填充
      Private Sub Command2_Click()
      Dim i, j, k As Integer, BoundPoint1, BoundPoint2 As Integer
      Dim Flag As Boolean, Pixel As Long
      Dim Red, Green, Blue As Integer, Bound As Boolean
      Flag = True '初始化
      i = Xx: j = Yy: BoundPoint1 = 1
      Array1(1, 1) = i
      Array1(1, 2) = j
      '搜索邊界點
      Do While BoundPoint1 > 0
      BoundPoint2 = 0
      For k = 1 To BoundPoint1
      i = Array1(k, 1)
      j = Array1(k, 2)
      '搜索右點
      Pixel& = Picture1.Point(i, j + 1)
      Call IsBound(Pixel&, Bound)
      If Not Bound Then
      BoundPoint2 = BoundPoint2 + 1
      Array2(BoundPoint2, 1) = i
      Array2(BoundPoint2, 2) = j + 1
      Picture1.PSet (i, j + 1), RGB(255, 255, 255)
      End If
      '搜索左鄰點
      Pixel& = Picture1.Point(i, j - 1)
      Call IsBound(Pixel&, Bound)
      If Not Bound Then
      BoundPoint2 = BoundPoint2 + 1
      Array2(BoundPoint2, 1) = i
      Array2(BoundPoint2, 2) = j - 1
      Picture1.PSet (i, j - 1), RGB(255, 255, 255)
      End If
      '搜索上鄰點
      Pixel& = Picture1.Point(i - 1, j)
      Call IsBound(Pixel&, Bound)
      If Not Bound Then
      BoundPoint2 = BoundPoint2 + 1
      Array2(BoundPoint2, 1) = i - 1
      Array2(BoundPoint2, 2) = j
      Picture1.PSet (i - 1, j), RGB(255, 255, 255)
      End If
      '搜索下鄰點
      Pixel& = Picture1.Point(i + 1, j)
      Call IsBound(Pixel&, Bound)
      If Not Bound Then
      BoundPoint2 = BoundPoint2 + 1
      Array2(BoundPoint2, 1) = i + 1
      Array2(BoundPoint2, 2) = j
      Picture1.PSet (i + 1, j), RGB(255, 255, 255)
      End If
      Next k
      '數組array2 中的數據傳給array1
      BoundPoint1 = BoundPoint2
      For k = 1 To BoundPoint1
      Array1(k, 1) = Array2(k, 1)
      Array1(k, 2) = Array2(k, 2)
      Next k
      Picture1.Refresh
      Loop
      End Sub
      Public Sub IsBound(P As Long, Bound As Boolean) '判斷P是否為邊界點
      Red = P& Mod 256
      Bound = False
      Green = ((P& And &HFF00) / 256&) Mod 256&
      Blue = (P& And &HFF0000) / 65536
      If Red = 255 And Green = 255 And Blue = 255 Then
      Bound = True
      End If
      End Sub
      五、 結束語
      本算法實現了在對填充區域的形狀、大小均未知的情況下,以種子點開始向四周對該區域進行“擴散式”的填充。本算法解決了傳統的遞歸算法在填充較大區域時(本例中填充區約9800Pixels)堆棧溢出的缺點。我們的實驗結果顯示,本算法就填充區域大小和運算速度而言,都遠遠超過了傳統的遞歸算法。


      日韩精品一区二区三区高清