CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2007
    Posts
    27

    HSV or RGB for Lips detection?

    Dear all,

    I have HSV algorithm posted under "color detection algorithm" but i failed to locate the lips as i did by using RGB algorithm. I set the H to 0.1 but still with the same result.
    My color image is in pgm format, so i didn't use ind2rgb but straight away to call colorDetectHSV(), will this be a problem?
    This is the colorDetectHSV():
    Code:
    function I = colorDetectHSV(RGB, hsvVal, tol)
    
    HSV = rgb2hsv(RGB);
    
    % find the difference between required and real H value:
    diffH = abs(HSV(:,:,1) - hsvVal(1));
    
    [M,N,t] = size(RGB);
    I1 = zeros(M,N); I2 = zeros(M,N); I3 = zeros(M,N);
    
    T1 = tol(1);
    
    I1( find(diffH < T1) ) = 1;
    
    if (length(tol)>1)
        % find the difference between required and real S value:
        diffS = abs(HSV(:,:,2) - hsvVal(2));    
        T2 = tol(2);
        I2( find(diffS < T2) ) = 1;    
        if (length(tol)>2)
            % find the difference between required and real V value:
            difV = HSV(:,:,3) - hsvVal(3);    
            T3 = tol(3);
            I3( find(diffS < T3) ) = 1;
            I = I1.*I2.*I3;
        else
            I = I1.*I2;
        end
    else
        I = I1;    
    end
    
    subplot(2,1,1),imshow(RGB); title('Original Image');
    subplot(2,1,2),imshow(I,[]); title('Detected Areas');
    This is my main program:
    Code:
    a = imread('m-013-1.pgm');
    b = colorDetectHSV(a, [0.1 1.0 0.5], [0.5 0.5 0.5]);
    The original images is in my attachment below "image.zip".
    However, i failed to allocate lips which is in red color. Please help, any comment will be appreciated.

    Thank you.

    Best regards,
    Tommy
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: HSV or RGB for Lips detection?

    Maybe target color is off? What do you mean by 'failed to locate'?
    "Programs must be written for people to read, and only incidentally for machines to execute."

  3. #3
    Join Date
    Nov 2007
    Posts
    27

    Re: HSV or RGB for Lips detection?

    I mean nothing has changed for red even i change the first value of colorDetectHSV() for red. I mean i can't even get the mouth like what i did in RGB. It's like a mess, can't differentiate the mouth from face skin no matter how i change the parameters.

    Please try with the image i attached, then you will see.
    Please help. Thanks

  4. #4
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: HSV or RGB for Lips detection?

    Sorry, don't know this language, don't have time for that.
    If it's not a one-off assignment, you should read some machine learning theory.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  5. #5
    Join Date
    Nov 2007
    Posts
    27

    Re: HSV or RGB for Lips detection?

    Never mind, it is ok. I hope someone can give me a helping hand. I have studied actually. Just wondering whether there is something wrong inside the codes.

    Thanks.

  6. #6
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: HSV or RGB for Lips detection?

    I am also not familiar with HSV and your script. Anyway from the link below, I believe that when Hue = 300 to 360 or 0 to 60, the color should be red. You may like to identify pixels that is dominantly red and check if the shape forms the lip.

    http://ilab.usc.edu/wiki/index.php/H...SV_Color_Space
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: HSV or RGB for Lips detection?

    Here are some formulas which translate, and draws 49 distinct colors
    Code:
    Option Explicit
    
    Private Type HSL
        Hue As Long
        Saturation As Long
        Luminance As Long
    End Type
    
    Private Function HSLToRGB(ByVal Hue As Long, ByVal Saturation As Long, ByVal Luminance As Long) As Long
    Dim R As Long, G As Long, B As Long
    Dim lMax As Long, lMid As Long, lMin As Long
    Dim Delta As Single
    lMax = (Luminance * 255) / 100
    lMin = (100 - Saturation) * lMax / 100
    Delta = (lMax - lMin) / 60
    Select Case Hue
    Case 0 To 60
        lMid = (Hue - 0) * Delta + lMin
        R = lMax: G = lMid: B = lMin
    Case 60 To 120
        lMid = -(Hue - 120) * Delta + lMin
        R = lMid: G = lMax: B = lMin
    Case 120 To 180
        lMid = (Hue - 120) * Delta + lMin
        R = lMin: G = lMax: B = lMid
    Case 180 To 240
        lMid = -(Hue - 240) * Delta + lMin
        R = lMin: G = lMid: B = lMax
    Case 240 To 300
        lMid = (Hue - 240) * Delta + lMin
        R = lMid: G = lMin: B = lMax
    Case 300 To 360
        lMid = -(Hue - 360) * Delta + lMin
        R = lMax: G = lMin: B = lMid
    End Select
    HSLToRGB = B * &H10000 + G * &H100& + R
    End Function
    
    Private Function RGBToHSL(ByVal RGBValue As Long) As HSL
    Dim R As Long, G As Long, B As Long
    Dim lMax As Long, lMin As Long
    Dim Delta As Single
    R = RGBValue And &HFF
    G = (RGBValue And &HFF00&) \ &H100&
    B = (RGBValue And &HFF0000) \ &H10000
    If R > G Then
        lMax = R: lMin = G
    Else
        lMax = G: lMin = R
    End If
    If B > lMax Then
        lMax = B
    ElseIf B < lMin Then
        lMin = B
    End If
    RGBToHSL.Luminance = lMax * 100 / 255
    If lMax > lMin Then
        RGBToHSL.Saturation = (lMax - lMin) * 100 / lMax
        Delta = 60 / (lMax - lMin)
        Select Case lMax
        Case R
        If B > G Then
            RGBToHSL.Hue = Delta * (G - B) + 360
        Else
            RGBToHSL.Hue = Delta * (G - B)
        End If
        Case G
            RGBToHSL.Hue = Delta * (B - R) + 120
        Case B
            RGBToHSL.Hue = Delta * (R - G) + 240
        End Select
    End If
    End Function
    
    Private Sub cmdGo_Click()
      Dim Color As HSL
      Color = RGBToHSL(Val("&H" & txtRGB.Text))
      For Color.Hue = 0 To 49
          Picture1(Color.Hue).BackColor = HSLToRGB(Color.Hue, Color.Saturation, Color.Luminance)
      Next Color.Hue
    End Sub
    
    Sub form_load()
      Dim colour As Long, red As Byte, green As Byte, blue As Byte
      Dim str As String
         red = 11
         green = 22
         blue = 33
      
      str = "Red : " & red & vbCrLf
      str = str & "Green : " & green & vbCrLf
      str = str & "Blue : " & blue & vbCrLf
      MsgBox str
      colour = RGB(red, green, blue)
      blue = (colour And &HFF0000) \ &H10000
      green = (colour And &HFF00&) \ &H100&
      red = colour And &HFF
      str = "Red : " & red & vbCrLf
      str = str & "Green : " & green & vbCrLf
      str = str & "Blue : " & blue & vbCrLf
      MsgBox str
      txtRGB.Text = RGB(100, 100, 100)  ' colour
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Nov 2007
    Posts
    27

    Re: HSV or RGB for Lips detection?

    dglienna: Do you mean these codes can help me to extract red region as well as the lips from an image?
    My original image is in RGB. If HSL can detect lips and ignore others, i am going to use it. However, i am not expert in Matlab because i use it to test algorithm before doing C. Do you mind to show me which part can do the job as mentioned?
    As far as i know, RGBToHSL() can be used to convert my rgb image to hsl color space. form_load() and cmdGo_Click() i have no idea. Do you mind to allocate a few minutes to educate me on this?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured