Use opencv to capture videos from USB cameras

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.