Hallo!
Photos, die in Grausstufen gewandelt werden, umfassen 256 Graustufen.
Eine bestimmte Fläche besteht dabei gewöhnlich nicht aus EINER
Graustufe, sondern sie variiert auch innerhalb einer Fläche.
Flächenbegrenzungen können deshalb nur über einen Schwellenwert
für die Grau-Differenz ermittelt werden, wobei der krit. Differenzwert umso
größer gewählt werden muss je stärker die Farbvarianz in der Fläche ist.
Hier eine triviale Funktion.
Public Shared Function GrauÜbergänge_Markieren _
(ByVal bmp_in As Bitmap,
ByVal ThresholdValue As Integer) As Bitmap
If bmp_in Is Nothing Then Return Nothing
'24 Bit-Bitmap erstellen
Dim gray As New Bitmap(bmp_in.Width, bmp_in.Height, _
Imaging.PixelFormat.Format24bppRgb)
Dim g As Graphics = Graphics.FromImage(gray)
g.DrawImage(bmp_in, 0, 0)
g.Dispose()
'MakeImageGrayscale(gray) optional, Funktion aus dem Net-Forum
Dim gray_rect As New Rectangle(0, 0, gray.Width, gray.Height)
' Bilddaten im Speicher sperren
Dim gray_data As Drawing.Imaging.BitmapData = _
gray.LockBits(gray_rect, Drawing.Imaging.ImageLockMode.ReadWrite, _
gray.PixelFormat)
' Array für Bitmapdaten in geeigneter Größe erstellen
Dim gray_bytes As Integer = gray_data.Stride * gray.Height
Dim gray_array(0 To gray_bytes - 1) As Byte
Dim marker_array(0 To gray_bytes - 1) As Byte
For i As Integer = 0 To marker_array.Length - 1
marker_array(i) = 255
Next i
' Die Bitmapdaten in das Array kopieren
Runtime.InteropServices.Marshal.Copy _
(gray_data.Scan0, gray_array, 0, gray_bytes)
Dim ystart As Integer = 0, yende As Integer = gray.Height - 1
Dim xstart As Integer = 0, xende As Integer = gray.Width - 1
Dim stride As Integer = gray_data.Stride
Dim centervalue As Byte
'DoppelSchleife (Indices = Pixelbezogen)
For y As Integer = ystart + 1 To yende - 1
For x As Integer = xstart + 1 To xende - 1
'Umrechnung auf eindimensionales Byte-Array
'(3 Bytes/Pixel in x-Richtung)
centervalue = gray_array(y * stride + x * 3)
'Schleife über Nachbarpixel
For y1 As Integer = y - 1 To y + 1
For x1 As Integer = x - 1 To x + 1
If Math.Abs(CInt(gray_array(y1 * stride + x1 * 3)) _
- centervalue) > ThresholdValue Then
marker_array(y * stride + x * 3) = centervalue
marker_array(y * stride + x * 3 + 1) = centervalue
marker_array(y * stride + x * 3 + 2) = centervalue
Exit For
End If
Next x1
Next y1
Next x
Next y
' Das Markerarray zurück-kopieren
Runtime.InteropServices.Marshal.Copy _
(marker_array, 0, gray_data.Scan0, gray_bytes)
With gray
.UnlockBits(gray_data)
End With
Return gray
End Function |