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