close

區域生長演算法 使用stack
VB9
by JK

'------------------------------------

Imports System.IO
Imports System.Collections

Public Class Form3
Dim pic_path As String = ""
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.ForeColor = Color.Red
Application.DoEvents()
If pic_path = "" Then
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
pic_path = OpenFileDialog1.FileName
End If

End If
'Dim b_new As New IO.FileStream(pic_path, IO.FileMode.Append)
'Dim rd_b As New BinaryReader(b_new)
'Dim data=

' 二進位讀取器()
Dim photo As Byte()
Dim nn = 0, i = 0
Dim appearPosition(100) As Integer


Dim N = 256
photo = My.Computer.FileSystem.ReadAllBytes(pic_path)
Dim photo_2D(N - 1, N - 1) As Byte

For j = 0 To N - 1
For i = 0 To N - 1
Dim v = photo(j * 256 + i)
photo_2D(i, j) = v

Next
Next




Dim row_i = CInt(TextBox_x.Text)
Dim column_i = CInt(TextBox_y.Text)
'-----------局域生長-------------
Dim stack_temp As New Stack(Of UInt16)
Dim stack_Area As New Stack(Of UInt32)
Dim seed_ini = 256 * column_i + row_i

Dim seed = seed_ini
search4direction(seed, photo, photo_2D, stack_temp, stack_Area)
While stack_temp.Count 0
Dim new_point = stack_temp.Pop
seed = new_point
' MsgBox(new_point.ToString)
search4direction(seed, photo, photo_2D, stack_temp, stack_Area)

End While


'-----------/局域生長/-------------






'--------輸出成圖檔--------------------------
Dim pic_grow As New Bitmap(256, 256)
Dim pic_gray As New Bitmap(256, 256)

For j = 0 To N - 1
For i = 0 To N - 1
If (j * 256 + i) = seed_ini Then
pic_gray.SetPixel(i + 1, j, Color.FromArgb(255, 0, 0))
pic_gray.SetPixel(i - 1, j, Color.FromArgb(255, 0, 0))
pic_gray.SetPixel(i, j + 1, Color.FromArgb(255, 0, 0))
pic_gray.SetPixel(i, j - 1, Color.FromArgb(255, 0, 0))
Else
Dim v = photo_2D(i, j)
pic_gray.SetPixel(i, j, Color.FromArgb(v, v, v))
pic_grow.SetPixel(i, j, Color.FromArgb(v, v, v))

End If
Next
Next



Dim Ns = 256
While stack_Area.Count > 0
Dim position = stack_Area.Pop
Dim pos_i = position Mod N
Dim pos_j = position \ N

pic_grow.SetPixel(pos_i, pos_j, Color.FromArgb(255, 0, 0))
End While

PictureBox1.Image = pic_grow
PictureBox2.Image = pic_gray
'---------/輸出成圖檔/-----------------------------

Button1.ForeColor = Color.Green
End Sub



Public Sub search4direction(ByVal seed As Integer, ByRef photo() As Byte, ByRef photo_2D(,) As Byte, ByRef stack_temp As Stack(Of UInt16), ByRef stack_Area As Stack(Of UInt32))
'-----------局域生長-------------


' Dim seed = 256 * 1 + 22
Dim N = 256


Dim v1_seed = photo(seed)
Dim seed_i = seed Mod N
Dim seed_j = seed \ N
Dim v2_seed As Integer = photo_2D(seed_i, seed_j) '相當於 v1_seed
Dim err = CInt(TextBox_err.Text)

If seed_i 0 And seed_j 0 Then
'------檢查上下左右--------------------
Dim v_up As Integer = photo_2D(seed_i, seed_j + 1)
Dim new_index = (seed_j + 1) * N + seed_i

If Not stack_Area.Contains(new_index) Then ' 只處理在紀錄本裡沒有的點

If CInt(v_up - v2_seed) stack_Area.Push(new_index) '紀錄生長位置 ' 紀錄本存下新的點
stack_temp.Push(new_index) '紀錄當次點
End If
End If


Dim v_right As Integer = photo_2D(seed_i + 1, seed_j)
new_index = (seed_j) * N + (seed_i + 1)
If Not stack_Area.Contains(new_index) Then ' 只處理在紀錄本裡沒有的點
If (v_right - v2_seed) stack_Area.Push(new_index) '紀錄生長位置 ' 紀錄本存下新的點
stack_temp.Push(new_index) '紀錄當次點
End If
End If


Dim v_down As Integer = photo_2D(seed_i, seed_j - 1)
new_index = (seed_j - 1) * N + (seed_i)
If Not stack_Area.Contains(new_index) Then ' 只處理在紀錄本裡沒有的點
If (v_down - v2_seed) stack_Area.Push(new_index) '紀錄生長位置 ' 紀錄本存下新的點
stack_temp.Push(new_index) '紀錄當次點
End If
End If

Dim v_left As Integer = photo_2D(seed_i - 1, seed_j)
new_index = (seed_j) * N + (seed_i - 1)
If Not stack_Area.Contains(new_index) Then ' 只處理在紀錄本裡沒有的點
If (v_left - v2_seed) stack_Area.Push(new_index) '紀錄生長位置 ' 紀錄本存下新的點
stack_temp.Push(new_index) '紀錄當次點
End If
End If
'------/檢查上下左右/--------------------
End If






'-----------/局域生長/-------------
End Sub


End Class

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 prague12 的頭像
    prague12

    prague12

    prague12 發表在 痞客邦 留言(0) 人氣()