Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I've been playing with vfw (video for windows) lately and have managed to successfully change the capture resolution of my laptop's web-cam.

The camera is a 1.3 megapixel job, supporting a max resolution of 1280 x 1024.

Whenever I select the camera using the system dialog, there are only 2 resolutions shown in the list - 320x240 & 640x480. Neither of these are 1.3Mpixels, so I decided to manually set the values.

I rechecked the specs for the laptop here[^] and saw that yes indeed, the camera's reported max res is 1280x1024.

I have a pair of functions I use for selecting either the lo-res or the hi-res capture mode. These both work flawlessly. I modified the values such that it would set 1280x1024.

This resulted in a black preview screen. After this point, each time I started the app I was shown the DeviceSelection dialog, even though I never explicitly asked for it. (prior to the dodgy video settings being sent, it would only appear when requested)

Furthermore, no matter how many times I tried to select the one and only camera it resolutely refused to work - still no preview image, single-frame captures to a bmp file also black.

This is quite similar to behaviour I've seen in web-cam motion-detection software in the past. Somehow I disovered ages ago that the *cough* *splutter* FaceBook "Upload a picture from my web-cam" feature would correctly 'reset' my camera and allow it to function once more. Prior to this discovery of FBs one redeeming quality, I would have to reset the computer to get video functioning again.

  • Previous apps would report that there were no available capture devices
  • My app would report the installed device, though selecting it had no apparent effect.


In each scenario, the flash-based camera capture thing at the aforementioned 'site' would re-initialize the camera correctly.


I realize that it's quite possible that the 1280x1024 mode is not YUY2, which would mean that my calculation of biSizeImage would likely be incorrect. But even setting either of the other two modes to BI_RGB didn't 'kill' the camera like setUltraRes does.

My question is - is there something obvious (or not) that I'm overlooking?


I'd have preferred to keep this post shorter, but don't know enough to know what (if any) of this information is irrelevant and unimportant. I really appreciate any & all insights/assistance. :)


Dialog Init Code:
C++
case WM_INITDIALOG:
            /*
             * TODO: Add code to initialize the dialog.
             */
//            camHwnd = capCreateCaptureWindow ("camera preview", WS_THICKFRAME|WS_EX_TOOLWINDOW, 0, 40, 320, 240, hwndDlg, 0); //create capture window
            camHwnd = createPreviewWindow(IDC_STATIC_CONTAINER, hwndDlg);

            capDriverConnect(camHwnd, 0);

            setLoRes();

            capPreviewScale(camHwnd, true);
            capPreviewRate(camHwnd, 66);
            capPreview(camHwnd, true);

            capGetVideoFormat(camHwnd, &bmInfo, sizeof(bmInfo));
            capWidth = bmInfo.bmiHeader.biWidth;
            capHeight = abs(bmInfo.bmiHeader.biHeight);
            return TRUE;


createPreviewWindowCode:
C++
HWND createPreviewWindow(int controlIdToReplace, HWND hwndDlg)
{
    RECT wndRect, ctlRect;
    RECT tRect;
    HWND tgt;
    POINT tl, br;
    int width, height, posX, posY;

    tgt = GetDlgItem(hwndDlg, controlIdToReplace);

    bool wasOk = GetWindowRect(tgt, &wndRect);
    tl.x = wndRect.left;
    tl.y = wndRect.top;
    br.x = wndRect.right;
    br.y = wndRect.bottom;
    ScreenToClient(hwndDlg, &tl);
    ScreenToClient(hwndDlg, &br);

    width = br.x - tl.x;
    height = br.y - tl.y;

    DestroyWindow(tgt);
    tgt = capCreateCaptureWindow ("camera preview", WS_CHILD|WS_BORDER|SS_SUNKEN, tl.x, tl.y, width, height, hwndDlg, controlIdToReplace); //create capture window
    return tgt;
}


Resolution setting code:
C++
// works okay
void setLoRes()
{
    capGetVideoFormat(camHwnd, &bmInfo, sizeof(bmInfo));
    bmInfo.bmiHeader.biWidth = 320;
    bmInfo.bmiHeader.biHeight = 240;
    bmInfo.bmiHeader.biCompression = 0x32595559;        // YUY2 compression
    bmInfo.bmiHeader.biSizeImage = 320 * 240 * 2;
    capSetVideoFormat(camHwnd, &bmInfo, sizeof(bmInfo));
}

// works okay
void setHiRes()
{
    capGetVideoFormat(camHwnd, &bmInfo, sizeof(bmInfo));
    bmInfo.bmiHeader.biWidth = 640;
    bmInfo.bmiHeader.biHeight = 480;
    bmInfo.bmiHeader.biCompression = 0x32595559;        // YUY2 compression
    bmInfo.bmiHeader.biSizeImage = 640 * 480 * 2;
    capSetVideoFormat(camHwnd, &bmInfo, sizeof(bmInfo));
}

// turns vid camera into a brick
void setUltraRes()
{
    capGetVideoFormat(camHwnd, &bmInfo, sizeof(bmInfo));
    bmInfo.bmiHeader.biWidth = 1280;
    bmInfo.bmiHeader.biHeight = 1024;
    bmInfo.bmiHeader.biCompression = 0x32595559;        // YUY2 compression
    bmInfo.bmiHeader.biSizeImage = 1280 * 1024 * 2;
    capSetVideoFormat(camHwnd, &bmInfo, sizeof(bmInfo));
}
Posted
Updated 16-Jun-12 19:10pm
v2
Comments
Sandeep Mewara 17-Jun-12 1:48am    
5 to question composition and explanation.
enhzflep 17-Jun-12 2:12am    
Thanks Sandeep.
Well, we've certainly seen enough examples of poorly asked questions. The answer's important to me and everybody else's time is important to them. Don't enjoy spending time to solve a question only to get a "oh yeh - forgot to say something" type response. (that totally changes the context or apparent avenue of investigation)
Sandeep Mewara 17-Jun-12 2:58am    
Agree totally. If I could upvote comments, 5 to it too.
arasaro92 4-Mar-13 8:07am    
how my neural network take picture and recognize the character i have error during recognizing character can you help me to know how to make my project able to recognize characters via camera?

1 solution

Are you going to do anything with the video you are capturing this way, or are you just looking for a way to reset the camera to its maximum resolution?

If you indeed want to capture the raw video in YUY2 format and "do something" with it (such as motion detection or object tracking), then I believe the maximum resolution you can get is 720x576, but it has been many years since I used VFW, so please don't shoot me if I am wrong.

You are receiving the raw, uncompressed video and the specs for that are rooted in NTSC/PAL analog video. I do recall reading about the capDlgVideoCompression() macro, but I do not know if that actually gives you options to select any CODEC registered on your system or supported by the camera itself.

Soren Madsen
 
Share this answer
 
Comments
enhzflep 17-Jun-12 2:32am    
The plan was to save to a png jpg without the intermediate save to bmp step. It's also possible to use the frame callback function to desaturate or colorize or overlay each frame as it is made available.

The important thing to me is just to get the camera to resume producing an output (of any kind!) - it really takes the jam out of my doughnut when I have to reset the pc to get video output once more.

Thanks for the 720x576 tip - now that you mention it, I seem to recall reading something that seemed to indicate you could get higher resolution in some circumstances than others.

But that's something of a curiosity to me at the moment. Using FB to reset the camera takes my doughnut and leaves me with jam on my hands. I just wish to resume video output.

Mmm hmm. Quite a neat system, in that like DolbyD and Stereo, it was backwards compatible with existing systems - YUV, that is.
The codec's (YUY2 - YUV422) no big deal, it's just a matter of
index = pixelNumber * 2
y = data[index];
u = data[index+1];
v = data[index+3];
Overlaying it and removing color information is fairly trivial.

For what it's worth - had toyed with the idea of using the camera as a barcode-scanner. Though at only 640x480 it's of rather limited use.

Also considered using a neural network to surreptitiously take pictures of the users face during games against the cpu. This way, facial expressions could be scanned and recognized as a means to detect the pressure the player was under. This could then be used by the computer to initiate taunts or to increase or decrease the difficulty of game-play. Think of a chess game that seems easy until the human player starts to seem overly confident, at which point the cpu player suddenly gets better. I think this could make some simple games far more engaging, since they would be far more emotionally reactive, just like a game of tennis, where the changing of who has the upper-hand can keep the match exciting.
SoMad 17-Jun-12 2:48am    
It sounds like you do have some ambitious plans for this. I am not much of a chess player, but that does sound like an interesting (and difficult) project.

You should not dismiss resolutions of 640x480 so quickly. There are Video Content Analysis solutions out there that work very well at that resolution - including facial recognition.
Have you ever checked out this article? Face Detection C++ Library with Skin and Motion Analysis[^]

Soren Madsen
enhzflep 17-Jun-12 3:01am    
I thought it could change the way that we interact with video games. I suppose someone already has a patent on it (or will do soon)

Yes indeed. The higher res was really more important for barcode/fingerprint scanning.
Too bad I can't vote for your comment - that was exactly the article I had in mind to re-read for this project. Along with one that starts with ALIP or something similar - one that categorizes photos according to their content. Reckon I could just grab a bunch of photos of different facial expressions and throw them at the neural network to train it what different expressions look like.

http://www.codeproject.com/Articles/31879/Automatic-Linguistic-Indexing-of-Pictures-ALIP-By
SoMad 17-Jun-12 3:13am    
I remember that article - the guy is really good.

Soren Madsen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900