USB cameras are cheap and easy to use, while opencv also contains API functions for reading USB camera video data. In Windows XP, this API function is implemented using the Windows VFW module. Below, I will give
Reference code:
# Include "stdafx. H"
# Include "cv. H"
# Include "highgui. H"
# Include "iostream"
Using namespace STD;
Int main (INT argc, char * argv [])
{
Cvcapture * Cap = cvcreatecameracapture (0); // initialize the pointer captured by the camera
If (! CAP)
{
Cout <"create camera capture error..." <Endl;
System ("pause ");
Exit (-1 );
}
Cvnamedwindow ("IMG ");
Iplimage * IMG = NULL;
While (1)
{
Iplimage * tempimg = cvqueryframe (CAP); // capture the video frames of the camera and perform corresponding decoding operations
If (IMG = NULL)
{
IMG = cvcreateimage (cvgetsize (tempimg ),
Tempimg-> depth, tempimg-> nchannels );
}
Cvcopy (tempimg, IMG); // copy to external memory
If (IMG-> origin = ipl_origin_tl) // if the image origin is in the upper left corner, flip it along the X axis so that the origin is in the lower left corner
{
Cvflip (IMG, IMG );
}
Cvshowimage ("IMG", IMG );
Cvwaitkey (3 );
}
Cvdestroyallwindows ();
Cvreleaseimage (& IMG );
System ("pause ");
Return 0;
}