Results 1 to 7 of 7

Thread: [RESOLVED] How to loop inside a Picture which is captured from my webcam?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    24

    Resolved [RESOLVED] How to loop inside a Picture which is captured from my webcam?

    Hi

    I am currently having problems on how to loop inside a picture which is being captured by a webcam.

    it is already "bitmap" in form when i copied to from the clipboard to the picturebox?

    i already figured out how to capture images from my webcam, but my problem now is to get the color inside the image being captured (the image is inside a picturebox)
    i tried hard figuring this out. i have found alot of codes like: color under mouse, and color picker. but are they related to what i am making now?
    sorry for my bad english.
    I hope you guys can help me with this.


    Here is my code:
    List1.Clear
    px = Picture4.Width / Screen.TwipsPerPixelX / 2
    py = Picture4.Height / Screen.TwipsPerPixelY / 2

    For i = 1 To Picture4.Width / Screen.TwipsPerPixelX Step 3
    For j = 1 To Picture4.Height / Screen.TwipsPerPixelY Step 3
    'Convert to RGB
    'Picture4.Circle (px * Screen.TwipsPerPixelX, py * Screen.TwipsPerPixelY), 10, RGB(255, 0, 0)
    r = (GetPixel(Picture4.hdc, i, j) Mod 256)
    b = Int(GetPixel(Picture4.hdc, i, j) / 65536)
    g = (GetPixel(Picture4.hdc, i, j) - (b * 65536) - r) / 256

    List1.AddItem "RGB=" & r & " " & g & " " & b



    Next

    Next


    End Sub

    is this the right way to get the image's color in rgb???
    Last edited by skemb321; Jan 6th, 2011 at 03:10 PM.

    interest controls curiosity.
    conscience controls karma.
    you alone can control yourself.
    know yourself and be above those who are lost.
    skemb321
    hehehe

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to loop inside a Picture which is captured from my webcam?

    Welcome to the forums

    Changing your picturebox scalemode to pixels during the loop can make things a bit easier
    Code:
    ...
    ' add another variable for color so you don't have to use GetPixel 3 times
    Dim lColor As Long
    
    Picture4.ScaleMode = vbPixels ' change it back to twips (or whatever scalemode you are using) later 
    For i = 0 to Picture4.ScaleWidth - 1 ' << use inside width (ScaleWidth)
       For j = 0 To Picture4.ScaleHeight - 1 ' << inside height
            lColor = GetPixel(Picture4.hDC, i, j)
            r = (lColor And &HFF&)
            g = (lColor And &HFF00&) \ &H100
            b = (lColor And &HFF0000) \ &H10000
            List1.AddItem "RGB = "  & r & " " & g & " " & b ' listing can be quite large!
        Next
    Next
    Picture4.ScaleMode = vbTwips ' reset back to original scalemode
    I used Hex values but you can use decimal if you prefer. To get decimal value from hex: Val(hexValue)

    There are even faster ways to get the color data if interested. Search forum for GetDIBits API usage
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    24

    Re: How to loop inside a Picture which is captured from my webcam?

    hello, sir!
    sorry for the late reply.

    hmmm,
    why do we have to minus 1 from the scalewidth and scaleheight sir?

    is it wrong to just loop it from 1 to picture4.scalewidth (or to picture4.scalewidth)?


    thank you so much.
    Last edited by skemb321; Jan 6th, 2011 at 03:20 PM.

    interest controls curiosity.
    conscience controls karma.
    you alone can control yourself.
    know yourself and be above those who are lost.
    skemb321
    hehehe

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    24

    Re: How to loop inside a Picture which is captured from my webcam?

    UPDATE!!!

    I've tested your code sir and it works just fine with mine here is the code

    vb Code:
    1. Private Sub Command1_Click()
    2.     Dim px As Long
    3.     Dim py As Long
    4.     Dim point As Long
    5.     Dim r As Long
    6.     Dim g As Long
    7.     Dim b As Long
    8.  
    9.    
    10.    
    11.         Picture4.ScaleMode = vbPixels
    12.         List1.Clear
    13.          Picture4.DrawWidth = 1
    14.          px = Picture4.ScaleWidth / Screen.TwipsPerPixelX / 2
    15.          py = Picture4.ScaleHeight / Screen.TwipsPerPixelY / 2
    16.          
    17.         For i = 0 To Picture4.ScaleWidth - 1
    18.             For j = 0 To Picture4.ScaleHeight - 1
    19.                     'Convert to RGB
    20.                    
    21.                     r = (GetPixel(Picture4.hdc, i, j) Mod 256)
    22.                     b = Int(GetPixel(Picture4.hdc, i, j) / 65536)
    23.                     g = (GetPixel(Picture4.hdc, i, j) - (b * 65536) - r) / 256
    24.                     If r > 250 And r < 255 Then
    25.                         Picture4.Circle (i, j), 2, RGB(0, 255, 0)
    26.                         List1.AddItem "RGB=" & r & " " & g & " " & b
    27.                     End If
    28.              Next
    29.          Next
    30.          
    31.       Picture4.ScaleMode = vbTwips ' reset to original scale mode
    32.        
    33. End Sub
    Last edited by skemb321; Jan 6th, 2011 at 03:40 PM.

    interest controls curiosity.
    conscience controls karma.
    you alone can control yourself.
    know yourself and be above those who are lost.
    skemb321
    hehehe

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to loop inside a Picture which is captured from my webcam?

    Quote Originally Posted by skemb321 View Post
    hello, sir!
    sorry for the late reply.

    hmmm,
    why do we have to minus 1 from the scalewidth and scaleheight sir?

    is it wrong to just loop it from 1 to picture4.scalewidth (or to picture4.scalewidth)?


    thank you so much.
    The reason is simple. The scalewidth & scaleheight starts at 0 not 1, so adjust by using -1.

    And regarding your newest code, it would be quite a bit faster if you used the logic I posted. Speed will be increased by not calling GetPixel 3 times for every pixel. Also using integer division (\) is faster than 'real' division (/)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: How to loop inside a Picture which is captured from my webcam?

    It always starts at 0,0. A Width of 4 would be 0,1,2,3. This is not without good reason as it simplifies the internal maths involved so it's worth getting used to.
    W o t . S i g

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    24

    Re: How to loop inside a Picture which is captured from my webcam?

    Quote Originally Posted by Milk View Post
    It always starts at 0,0. A Width of 4 would be 0,1,2,3. This is not without good reason as it simplifies the internal maths involved so it's worth getting used to.
    tnx!

    finally, got the idea now.

Tags for this Thread

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