webcam problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patr0805
    New Member
    • Sep 2007
    • 48

    webcam problem

    I have made a picturebox and a coding witch takes a picture in the picturebox, but the problem is that I need to save it as a image file before I can use getpixel, so when I use a save picturebox as image then VB.NET returns an error with image not exists,
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Can you show us what you've attempted so far, that doesn't work?

    Comment

    • Robbie
      New Member
      • Mar 2007
      • 180

      #3
      I know what Patr's talking about, and I'd like to know how to do this too.
      Debasisdas has made an example of how to capture from a webcam and "display the live video in a picturebox". However, what Windows is actually doing is displaying the picture on top of the picture box (known as overlaying). Therefore the picturebox actually still is empty, meaning that GetPixel() will fail to give back colours of the camera's images, and SavePicture() will obviously also fail to save the images.

      We need a way to actually set the contents of the picturebox, and the easiest way seems to be by saving an image from the camera and loading it into the picturebox, where it can then be used as any other loaded image file. The downside to this is that the frame rate would probably be very low (probably no more than 4 fps or so) due to overheads from the file system (or possibly the hard disk itself, reading/writing the file over and over).

      EDIT: I'm using VB6 and Windows XP, by the way, and this still happens. However, I tried using MCI to play a video in a picture box (this being related to webcams because MCI also just overlays in XP), on an extremely old computer running Windows 98, and GetPixel() worked - I think that may be because GetPixel() works differently on 98 compared to XP, rather than anything else.

      EDIT2: Stupidly, it didn't occur to me at the time to try SavePicture(), which would confirm whether it was just GetPixel() being different on 98, or if 98 was actually placing the picture into the picturebox
      .
      Last edited by Robbie; Jan 31 '08, 02:18 PM. Reason: Mentioned VB version

      Comment

      • patr0805
        New Member
        • Sep 2007
        • 48

        #4
        that is exactly what I am talking about.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Thanks for the input, Robbie.

          I don't know the answer, but it occurs to me there may be another workaround. Not a really elegant way to go, but possibly faster. That is, use the API call (I forget the details, this is probably going back over 5 years) which is typically used by screensavers to grab the desktop.

          When you write a screensaver, typically the first thing you do (at least in the simple examples I coded, way back) is to grab a snapshot of the current display, so you can play with it. It should be possible either do this then throw away the part you don't want, or if possible, grab just the part where your picturebox lives.

          If interested, you should be able to find plenty of examples by searching for info on VB and screen savers.

          Comment

          • kingchi9
            New Member
            • Feb 2008
            • 1

            #6
            Ok ok the answer is actually very simple. i made an instant messanger with webcam and voice about a year ago. for the image thing just get the handle of the picturebox.

            camHwnd = capCreateCaptur eWindow(CamWnd, 0, 0, 0, 320, 240, me.hwnd, 0)

            Then all you have to do is save like this:

            SavePicture Picture1.pictur e, Path

            Then Reload the picture like this:

            Picture1.pictur e = loadpicture(path )

            thats is.

            if thats what you were doing that should werk. im testing right now and after getting the picture reloaded into the picturebox i am able to use getpixel function on it.

            P.S. the frame rate on my webcam is about twice as good as the frame rate of yahoo instant messanger's webcam. my computer specs. are: xp pro, AMD 64 moblie Athlon, 1gig ram. like i said the saving and opening of the image does slow the framerate but if you broad casting over the internet your fps will well over exceed your streaming speeds.

            Comment

            • rfoshaug
              New Member
              • Feb 2008
              • 4

              #7
              Hi, I'm having the exact same problem. I am trying to detect certain pixels from a video streaming from a webcam (don't really need to detect the entire webcam image, just 10 or 20 pixels of it to see if they change).

              But I can't get it working! I've followed this thread and this is the code I've ended up with:


              This is the code in a module (.bas file):
              Code:
              Option Explicit
              
              Public Const ws_child As Long = &H40000000
              Public Const ws_visible As Long = &H10000000
              Global Const WM_USER = 1024
              Global Const wm_cap_driver_connect = WM_USER + 10
              Global Const wm_cap_set_preview = WM_USER + 50
              Global Const WM_CAP_SET_PREVIEWRATE = WM_USER + 52
              Global Const WM_CAP_DRIVER_DISCONNECT As Long = WM_USER + 11
              Public Const WM_CAP_DLG_VIDEOFORMAT As Long = WM_USER + 41
              Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
              Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias "capCreateCaptureWindowA" (ByVal a As String, ByVal b As Long, ByVal c As Integer, ByVal d As Integer, ByVal e As Integer, ByVal f As Integer, ByVal g As Long, ByVal h As Integer) As Long


              This is the code in my form:

              Code:
              Option Explicit
              'General Declaration
              Dim hwdc As Long
              Dim startcap As Boolean
               
              Private Sub cmdCapture_Click()
              Dim temp As Long
               
                hwdc = capCreateCaptureWindow("Debasis Das", ws_child Or ws_visible, 0, 0, 320, 240, Picture1.hWnd, 0)
                If (hwdc <> 0) Then
                  temp = SendMessage(hwdc, wm_cap_driver_connect, 0, 0)
                  temp = SendMessage(hwdc, wm_cap_set_preview, 1, 0)
                  temp = SendMessage(hwdc, WM_CAP_SET_PREVIEWRATE, 30, 0)
                  startcap = True
                  Else
                  MsgBox ("No Webcam found")
                End If
              End Sub
               
              Private Sub cmdClose_Click()
                Dim temp As Long
                If startcap = True Then
                temp = SendMessage(hwdc, WM_CAP_DRIVER_DISCONNECT, 0&, 0&)
                startcap = False
                End If
              End Sub
              
              Private Sub cmdexit_Click()
                Unload Me
              End Sub
              
              Private Sub cmdVideoFormat_Click()
                  Dim temp As Long
                  If startcap = True Then
                      temp = SendMessage(hwdc, WM_CAP_DLG_VIDEOFORMAT, 0&, 0&)
                  End If
              End Sub
              
              
              Private Sub Timer1_Timer()
                  Dim camwnd As String
                  Dim CamHwnd As String
                  Dim Path As String
              
              [I]    camwnd = Me.hWnd[/I]
              [I]    CamHwnd = capCreateCaptureWindow(camwnd, 0, 0, 0, 320, 240, Me.hWnd, 0)[/I]
                  
                  Path = "C:\Test.jpg"
                  
                  SavePicture Picture1.Picture, Path
                  Picture1.Picture = LoadPicture(Path)
              
                  Me.lblColor = Me.Picture1.Point(100, 100) 'Just a test, trying to get a pixel value
              End Sub

              The idea is that the timer will periodically (several times per second) check some pixel (in this example at x=100, y=100) and for now just print the value of this pixel in a label in the form.

              But the program crashes at the SavePicture line, saying "Invalid property value", and then VB6 itself crashes.

              I think that we're really close here. I've tried to fill the "camwnd" variable with both the name and hWnd of both the form and the picturebox, but there's no difference in the outcome.


              Does anyone know how I can get the value of certain pixels in the video stream (preferrably without having to save it to a file and load it again)?

              Thanks. :-)
              Last edited by Killer42; Mar 3 '08, 01:26 AM. Reason: Italicised lines 45-46 which have since been found to be in error.

              Comment

              • rfoshaug
                New Member
                • Feb 2008
                • 4

                #8
                Well, I just made the strangest breakthrough.

                I removed the saving and loading of the image file, and replaced "Picture1.h wnd" with "Me.HWnd" in the capCreateCaptur eWindow command at line 9 in my code, and... it works!!!

                I have no idea why or how, but it works. The image is now drawn directly on the form's surface, and I can still use Me.Picture1.Poi nt to get the value, which can then be split up in it's Red, Green and Blue components.

                But I had to move the picturebox to position 0,0 (upper left corner in the form) and make it the same size as the picture from the webcam (320x240 pixels in my case). I also set the ScaleMode of both the form and picturebox to "Pixel".

                I really don't have any idea why this works, but I don't really care as long as it does. :-)

                Comment

                • rfoshaug
                  New Member
                  • Feb 2008
                  • 4

                  #9
                  BTW, if anyone's interested, this is the sub I use to get Red, Green and Blue from the color value:

                  [CODE=vb]
                  Public Function get_rgb(Color, rd, gr, bl)
                  Dim tmpColor
                  If Color < 0 Then Color = 0

                  tmpColor = Color / 65536
                  bl = Int(tmpColor)
                  tmpColor = (tmpColor - Int(tmpColor)) * 256

                  gr = Int(tmpColor)
                  tmpColor = (tmpColor - Int(tmpColor)) * 256

                  rd = Round(tmpColor)
                  End Function
                  [/CODE]

                  You put in the color you get from the webcam (Long data type), and three variables for Red, Green, and Blue (normally data type Byte). These variables will then be filled with RGB values.
                  Last edited by Killer42; Feb 28 '08, 02:39 AM. Reason: Change CODE tag to CODE=vb

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Thanks for sharing that with us, rfoshaug. I've had to split colours like this a few times, too. I'd like to make a couple of small comments about this version.
                    • Ideally, you should work with Long data type wherever possible, for performance reasons. It might seem crazy, but it's actually slightly faster to work with Long than with Byte. (On a 32 bit system, that is. 64 bit may be different).
                    • Integer division is faster, too. Try using \ instead of /.
                    • You're doing the same calculation twice, unnecessarily. In line 7, for instance, you should use bl rather than Int(tmpColor).


                    This is all just nitpicking, of course - if the routine works, great. :)


                    EDIT: Oops! The integer division thing (\) might not work with your method.

                    Comment

                    • rfoshaug
                      New Member
                      • Feb 2008
                      • 4

                      #11
                      I've just discovered that these lines:

                      camwnd = Me.hWnd
                      CamHwnd = capCreateCaptur eWindow(camwnd, 0, 0, 0, 320, 240, Me.hWnd, 0)

                      are not needed in the timer sub, and actually cause lockups and problems, so I'm removing them from the code in my earlier post.

                      Edit: It seems I can't edit that older post. Anyway, it's line 45 and 46 in the first code I posted, in the Timer sub.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by rfoshaug
                        I've just discovered ....
                        I've edited it and put those two lines in italics. At least they will look slightly different, so when people get to this message they should know which lines you mean.

                        It might be better to just use the Reply function to copy the code from that message and re-post it here without those lines.

                        Comment

                        Working...