OpenCV External USB Camera

Source: Internet
Author: User
Recently, we need to use OPENCV Computer Vision Library to read and display and save the video. As a result of the previous use of notebooks, the desktop external USB camera, there are a lot of problems, always show memory problems, Google, Baidu large half-day, summed up the way the great God proposal, finally solved the problem, the problem and code to share to everyone, avoid detours. The cause of the problem occurs: 1. The camera initialization takes time, enters the loop before Waitkey (2000), otherwise will appear the Flash back; 2. There is a decoding problem with the webcam video.

--------------------------------------"program description"-------------------------------------------
//		Program Description: OPENCV Computer Vision Library Getting Started learning
//		Program Description: Desktop using OPENCV external USB camera, read in video and display
/		dev Test operating system: Windows 7 64bit
//		IDE version for development test: Visual Studio
//		Development Test OPENCV version:  2.4.9
//		October 2017 Created by @ Fireman1994
//-------------------------------------------------------------------------------------------- ----

#include "cv.h"
#include "highgui.h"
#include "iostream"
using namespace std;

int main (int argc,char* argv[])
{
	cvcapture* cap;
	Cap=cvcapturefromcam (0);
	if (!cap)
	{
		cout<< "Create Camera Capture Error" <<endl;
		System ("pause");
		Exit ( -1);
	}
	Cvnamedwindow ("img", 1);
	iplimage* img;
	Before entering the loop, wait for a period of time for the camera to initialize, otherwise initialization is not completed, will flash out of the program
	Cvwaitkey (a);
	while (1)
	{
		img=cvqueryframe (CAP);//read-in video decode
		if (!img) break
		;
	
		Cvshowimage ("IMG", IMG);
		Cvwaitkey (3);
	}
	Cvreleasecapture (&CAP);
	Cvdestroyallwindows ();
	Cvreleaseimage (&img);
	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.