Farbverteilung von einem Bild bestimmen (1D-Hue-Histogramm)
Zum Code
Auch in diesem Beispiel wird wieder ein Histogramm erstellt, jedoch lediglich eins vom "Hue"-Wert der Pixel. Dabei wird das Bild zuerst vom RGB-Farbraum in den HSV-Farbraum umgerechnet, danach wird ein Histogramm (Verteilung der Werte) für die Farbe erstellt und graphisch als Balkendiagramm dargestellt. Der Hue-Wert wird in Grad von 0° bis 360° angegeben. Im HSV-System gibt H die Farbe an, S die Sättigung und V die Helligkeit.

History
15.05.2003 Hinzugefügt

Autor: Dominik Auras <Dominik_auf_vbinside.de>

Code aus Form1.frm
Option Explicit

Private Sub Command1_Click()
Dim histogram(359) As Long, x As Long, y As Long, r As Long
Dim max As Long, factor As Double, curr As Long, g As Long, b As Long
Dim color As Long
Dim h As Long, s As Long, v As Long

For y = 0 To Picture1.ScaleHeight
For x = 0 To Picture1.ScaleWidth
color = Picture1.Point(x, y)
b = color And &HFF
g = (color \ 2 ^ 8) And &HFF
r = (color \ 2 ^ 16) And &HFF

RGBtoHSV r, g, b, h, s, v
Debug.Assert h <= 359
Debug.Assert h >= 0

histogram(h) = histogram(h) + 1
Next x
Next y

Debug.Print "Erstellt"

For x = 0 To 359
If histogram(x) > max Then
max = histogram(x)
End If
Next x

factor = 200 / max

x = 1
For y = 0 To 359
curr = histogram(y) * factor
Picture2.Line (x, Picture2.ScaleHeight - 1)-(x + 1, _
Picture2.ScaleHeight - curr - 1), RGB(255, 0, 0), BF

x = x + 2
Next y

Debug.Print "Max:", max
Debug.Print "Factor:", factor
Debug.Print "Gezeichnet"
End Sub

Private Sub Form_Load()
Picture1.Picture = LoadPicture(App.Path & "\weights.bmp")
End Sub

Sub RGBtoHSV(r_1 As Long, g_1 As Long, b_1 As Long, hue As Long, _
sat As Long, value As Long)
Dim min As Double, max As Double, delta As Double
Dim r As Double, g As Double, b As Double
Dim h As Double, s As Double, v As Double

r = r_1 / 255
g = g_1 / 255
b = b_1 / 255

min = 1
If min > r Then
min = r
End If
If min > g Then
min = g
End If
If min > b Then
min = b
End If

If max < r Then
max = r
End If
If max < g Then
max = g
End If
If max < b Then
max = b
End If

v = max
delta = max - min

If delta = 0 Then
h = 0
s = 0
Exit Sub
End If

If max <> 0 Then
s = delta / max
Else
s = 0
h = -1
Exit Sub
End If

If r = max Then
h = (g - b) / delta
ElseIf g = max Then
h = 2 + (b - r) / delta
Else
h = 4 + (r - g) / delta
End If

h = h * 60

If h < 0 Then
h = h + 360
End If

hue = h
sat = s * 100
value = v * 100
End Sub