Newsgroup: it.comp.lang.visual-basic
Titolo: Movement Detection

Come posso rilevare se c'e' stato un movimento tra due frames catturati da
una webcam ?
grazie

 wrote in message
q46EgnR...@newsmst.mcis.galactica.it...


 ha scritto nel messaggio
news:q46EgnR...@newsmst.mcis.galactica.it...


 dei pixel, non sui colori RGB. Questa scelta va fatta soprattutto se e'
come posso recuperare il valore di luminosita' di un pixel con visual basic?
pensavo di poter recuperare il valore rgb, esiste una routine per
convertirlo in valore luminosita'?
grazie


Option Explicit
' Declares for RGB2HLS().
Const HSL_UNDEFINED = -1
Sub RGBToHSL(ByVal R As Single, ByVal G As Single, ByVal B As Single, H As
Single, S As Single, L As Single)
    Dim sMax As Single
    Dim sMin As Single
    Dim delta As Single
    R = R / 255
    G = G / 255
    B = B / 255
    sMax = sngMax(R, sngMax(G, B))
    sMin = sngMin(R, sngMin(G, B))
    L = (sMax + sMin) / 2
    If sMax = sMin Then
        S = 0
        H = 0 'undefined
        L = L * 240
        Exit Sub
    End If
    If L < 0.5 Then
        S = (sMax - sMin) / (sMax + sMin)
    Else
        S = (sMax - sMin) / (2 - sMax - sMin)
    End If
    If S < 0 Then S = 0
    delta = sMax - sMin
    If R = sMax Then
        H = (G - B) / delta
    ElseIf G = sMax Then
        H = 2 + (B - R) / delta
    ElseIf B = sMax Then
        H = 4 + (R - G) / delta
    End If
    H = H * 40
    If H < 0 Then H = H + 240
    S = S * 240
    L = L * 240
End Sub
Sub RGBClrToHSL(ByVal Clr As Long, H As Single, S As Single, L As Single)
Const kn2LowByteMask As Long = &HFF&
Dim sMax As Single
Dim sMin As Single
Dim delta As Single
Dim R!, G!, B!
R = (Clr And kn2LowByteMask) / 255
G = ((Clr \ &H100&) And kn2LowByteMask) / 255
B = ((Clr \ &H10000) And kn2LowByteMask) / 255
sMax = sngMax(R, sngMax(G, B))
sMin = sngMin(R, sngMin(G, B))
L = (sMax + sMin) / 2
If sMax = sMin Then
    S = 0
    H = 0 'undefined
    L = L * 240
    Exit Sub
End If
If L < 0.5 Then
    S = (sMax - sMin) / (sMax + sMin)
Else
    S = (sMax - sMin) / (2 - sMax - sMin)
End If
If S < 0 Then S = 0
delta = sMax - sMin
If R = sMax Then
    H = (G - B) / delta
ElseIf G = sMax Then
    H = 2 + (B - R) / delta
ElseIf B = sMax Then
    H = 4 + (R - G) / delta
End If
H = H * 40
If H < 0 Then H = H + 240
S = S * 240
L = L * 240
End Sub
Public Function sngMax(ByVal sng1 As Single, ByVal sng2 As Single) As Single
If sng2 > sng1 Then sngMax = sng2 Else sngMax = sng1
End Function
Public Function sngMin(ByVal sng1 As Single, ByVal sng2 As Single) As Single
If sng1 < sng2 Then sngMin = sng1 Else sngMin = sng2
End Function
Function SysColor(ByVal vbSysColor As SystemColorConstants)
    SysColor = GetSysColor(Val("&H" & Right(Hex(vbSysColor), 2)))
End Function
'// COLOR FUNCTIONS
////////////////////////////////////////////////////////////
' Separare le tre componenti fondamentali di colore
Sub GetRGB(ByVal Color As Long, ByRef R As Byte, ByRef G As Byte, ByRef B As
Byte)
    R = Color And &HFF&
    G = (Color And &HFF00&) \ &H100&
    B = (Color And &HFF0000) \ &H10000
End Sub
Function Red(ByVal Color As Long)
    Red = Color And &HFF&
End Function
Function Green(ByVal Color As Long)
    Green = (Color And &HFF00&) \ &H100&
End Function
Function Blue(ByVal Color As Long)
    Blue = (Color And &HFF0000) \ &H10000
End Function
' Funzione di trasformazione del colorspace da RGB a HSL.
' (RGB = Red, Green, Blue) (HSL = Hue, Saturation, Luminance)
'
' INPUT:
'         R,G,B in range [0,1]
' OUTPUT:
'         H in range [0,360] (if S = 0, H = UNDEFINED, see constant)
'         L in range [0,1]
'         S in range [0,1]
Sub RGB2HLS(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte, ByRef H As
Double, ByRef L As Double, ByRef S As Double)
    Dim Max As Double
    Dim Min As Double
    Dim delta As Double
    ' Get maximum value between R, G and B.
    If (R > G) Then Max = R Else Max = G
    If (Max < B) Then Max = B
    ' Get minimum value between R, G and B.
    If (R < G) Then Min = R Else Min = G
    If (Min > B) Then Min = B
    L = (Max - Min) / 2                ' Luminance
    If (Max = Min) Then
        ' Color is gray.
        S = 0
        H = HSL_UNDEFINED
    Else
        ' Calculate Saturation.
        If (L <= 0.5) Then
            S = (Max - Min) / (Max + Min)
        Else
            S = (Max - Min) / (2 - Max - Min)
        End If
        ' Calculate Hue.
        delta = Max - Min
        If (R = Max) Then
            H = (G - B) / delta            ' between yellow and magenta.
        ElseIf (G = Max) Then
            H = 2 + (B - R) / delta        ' between cyan and yellow.
        Else ' (B = max)
            H = 4 + (R - G) / delta        ' between magenta and cyan.
        End If
        ' Convert Hue to degree in range [0..360].
        H = H * 60
        If (H < 0) Then H = H + 360
    End If
End Sub
Function HSL2RGB(ByVal Hue As Integer, ByVal Sat As Integer, ByVal Lum As
Integer) As Long
    If Hue > 239 Or Hue < 0 Then Exit Function
    If Sat > 239 Or Sat < 0 Then Exit Function
    If Lum > 239 Or Lum < 0 Then Exit Function
    Dim R As Integer, G As Integer, B As Integer
    ' Hue
    If Hue <= 40 Then
        R = 255
        G = CInt(Hue / 40 * 256)
        B = 0
    ElseIf Hue > 40 And Hue <= 80 Then
        R = CInt((1 - (Hue - 40) / 40) * 256)
        G = 255
        B = 0
    ElseIf Hue > 80 And Hue <= 120 Then
        R = 0
        G = 255
        B = CInt((Hue - 80) / 40 * 256)
    ElseIf Hue > 120 And Hue <= 160 Then
        R = 0
        G = CInt((1 - (Hue - 120) / 40) * 256)
        B = 255
    ElseIf Hue > 160 And Hue <= 200 Then
        R = CInt((Hue - 160) / 40 * 256)
        G = 0
        B = 255
    ElseIf Hue > 200 Then
        R = 255
        G = 0
        B = CInt((1 - (Hue - 200) / 40) * 256)
    End If
    ' Saturation
    R = R + (240 - Sat) / 240 * (128 - R)
    G = G + (240 - Sat) / 240 * (128 - G)
    B = B + (240 - Sat) / 240 * (128 - B)
    ' Luminance
    If Lum < 120 Then
        R = (R / 120) * Lum
        G = (G / 120) * Lum
        B = (B / 120) * Lum
    Else
        R = Lum * ((256 - R) / 120) + 2 * R - 256
        G = Lum * ((256 - G) / 120) + 2 * G - 256
        B = Lum * ((256 - B) / 120) + 2 * B - 256
    End If
    ' adjust R,G,B range [0..255].
    If R < 0 Then R = 0
    If R > 255 Then R = 255
    If G < 0 Then G = 0
    If G > 255 Then G = 255
    If B < 0 Then B = 0
    If B > 255 Then B = 255
    HSL2RGB = RGB(R, G, B)
End Function
' Funzione di trasformazione del color-space da RGB a GrayScale (luminosita'
Y).
'
' INPUT:
'         R,G,B in range [0,1]
' OUTPUT:
'         GrayScale in range [0,255]
'
' NOTE: l'occhio umano non e' sensibile allo stesso modo ai tre colori,
' generalmente si usa il 45-55% del verde, il 30-40% del rosso e il
' 10-20% del blu. La piu' usata generalmente e' 0.299*R + 0.587*G + 0.114*B
Sub RGB2GrayScale(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte, ByRef
GrayScale As Byte)
    GrayScale = 0.299 * R + 0.587 * G + 0.114 * B
End Sub
Sub HSLToRGB(ByVal H As Single, ByVal S As Single, ByVal L As Single, R As
Single, G As Single, B As Single)
    Dim m1 As Single, m2 As Single
    H = H / 240
    S = S / 240
    L = L / 240
    If S = 0 Then
        R = L * 255
        G = L * 255
        B = L * 255
        Exit Sub
    End If
    If L < 0.5 Then
        m2 = L * (1! + S)
        'm2 = L + L * S
    Else
        m2 = L + S - L * S
        'm2 = (L - L * S) + S
        'm2 = L * (1! - S) + S
    End If
    m1 = 2! * L - m2
    R = HueToRGB(m1, m2, H + 1! / 3!) * 255
    G = HueToRGB(m1, m2, H) * 255
    B = HueToRGB(m1, m2, H - 1! / 3!) * 255
End Sub
Function HSLToRGBClr(ByVal H As Single, ByVal S As Single, ByVal L As
Single) As Long
    Dim m1 As Single, m2 As Single
    H = H / 240
    S = S / 240
    L = L / 240
    If S = 0 Then
      HSLToRGBClr = RGB(Int(L * 255), Int(L * 255), Int(L * 255))
      Exit Function
    End If
    If L < 0.5 Then
        m2 = L * (1! + S)
        'm2 = L + L * S
    Else
        m2 = L + S - L * S
        'm2 = (L - L * S) + S
        'm2 = L * (1! - S) + S
    End If
    m1 = 2! * L - m2
    'Use Int instead of CInt because the windows color dialog always rounds
down.
    HSLToRGBClr = RGB(Int(HueToRGB(m1, m2, H + 1! / 3!) * 255), _
        Int(HueToRGB(m1, m2, H) * 255), Int(HueToRGB(m1, m2, H - 1! / 3!) *
255))
End Function
Private Function HueToRGB(m1 As Single, m2 As Single, H As Single) As Single
    If (H < 0) Then H = 1! + H
    If (H > 1) Then H = H - 1!
    If (6! * H < 1) Then
        HueToRGB = m1 + (m2 - m1) * H * 6!
    ElseIf (2! * H < 1) Then
        HueToRGB = m2
    ElseIf (3! * H < 2!) Then
    'This color was the Max.
        HueToRGB = m1 + (m2 - m1) * (4! - 6! * H)
        'HueToRGB = m1 + (m2 - m1) * ((2! / 3!) - H) * 6! 'Same as above
    Else
        HueToRGB = m1
    End If
End Function
Sub CMYKToRGB(ByVal C As Single, ByVal M As Single, ByVal Y As Single, ByVal
K As Single, R As Single, G As Single, B As Single)
    If (C + K) < 255 Then R = 255 - (C + K) Else R = 0
    If (M + K) < 255 Then G = 255 - (M + K) Else G = 0
    If (Y + K) < 255 Then B = 255 - (Y + K) Else B = 0
End Sub
Sub RGBToCMYK(ByVal R As Single, ByVal G As Single, ByVal B As Single, C As
Single, M As Single, Y As Single, K As Single)
    C = 255 - R
    M = 255 - G
    Y = 255 - B
    If C < M Then K = C Else K = M
    If Y < K Then K = Y
    If K > 0 Then
        C = C - K
        M = M - K
        Y = Y - K
    End If
End Sub
Sub RGBToYUV(ByVal R As Single, ByVal G As Single, ByVal B As Single, Y As
Single, U As Single, V As Single)
    Y = 0.299 * R + 0.587 * G + 0.114 * B
    U = (-0.169 * R) + (-0.331 * G) + (0.5 * B)
    V = (0.5 * R) + (-0.419 * G) + (-0.081 * B)
End Sub

 dei pixel, non sui colori RGB. Questa scelta va fatta soprattutto se e'



beh, a rigor di logica, dato che i valori di R G e B rappresentano
l'intensità dei tre colori base, per avere la luminosità media di un punto
suppongo basti fare una semplice media aritmetica dei tre valori;
    Lum = (R + G + B) / 3
non so se in particolari situazioni si debba considerare "più luminoso" un
colore piuttosto che un altro; facendo tale ipotesi, basterebbe applicare un
coefficente di guadagno ad ogni componente
    Const kr = 1        ' il rosso viene considerato normale
    Const kg = 1.5    ' il verde è messo in evidenza
    Const kb = 0.8    ' il blu viene attenuato
    Lum = ((kr * R) + (kg * G) + (kb * B)) / 3
non so quali potrebbero essere i valori "giusti".... l'unica cosa che posso
dirti è di fare delle prove (magari su immagini bitmap gia acquisite,
tralasciando in un primo momento il discorso realtime)
buona fortuna;
ciao

 per la sensibilita'.
se ettore m.a. e' d'accordo, renderebbe felice ettore m. inviando il prg al
webmaster ubi del sito comune.
(ettore (m.): come vedi, io ci provo...)
se per sicurezza lo inviasse anche a
p...@cedoc.mo.it
farebbe un grande piacere anche a pal.
grazie in anticipo,
a.

 sono a disposizione per inviarlo a richiesta.
me lo invii? :)
grazie
Roby

 --
grazie 1000
ciao

 una risposta invece che direttamente al messaggio a cui si vuole
rispondere.
ci provo:
THIS IS THE JARGON FILE, VERSION 4.1.4, 17 JUN 1999
in html, ma ^f "pigg" / "pig" trova solo: "snarf" - This term was mainstream
in the late 1960s, meaning `to eat piggishly'.
hai altre fonti da suggerire ?
(email se lunga e/o fuori posto)
grazie, a.

> THIS IS THE JARGON FILE, VERSION 4.1.4, 17 JUN 1999
v-4.2.2: 
http://www.tuxedo.org/~esr/jargon/jargon.html

 v-4.2.2: 
http://www.tuxedo.org/~esr/jargon/jargon.html
grazie, trovato.
ma cercare "pigg" da' gli stessi risultati.
il jargon resta cmq un must have.