OpenCV external USB camera method
- 2020-06-07 04:57:07
- OfStack
Recently, we need to use OpenCV computer vision library to read, display and save videos. Since I used the laptop directly before, this time I connected USB camera to the desktop, so there was a big problem. It always showed the memory problem. Google and baidu spent most of the day summarizing various Suggestions, and finally solved the problem.
Reasons for the problem:
1. It takes time to initialize the camera. Before entering the loop, waitKey(2000), or it will flash back;
2. There are decoding problems in the camera video.
//-------------------------------------- [Procedure Description] -------------------------------------------
// Procedure Description: OpenCV Introduction to Computer vision library
// Program description: Desktop utilization OpenCV external USB The camera, read in the video and display it
// Operating system for development and test: Windows 7 64bit
// Development test use IDE Version: Visual Studio 2010
// Development test use OpenCV Version: 2.4.9
// 2017 years 10 month 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;
// Wait before entering the loop 1 A period of time to initialize the camera, otherwise the initialization is not complete, will directly flash out of the program
cvWaitKey(2000);
while(1)
{
img=cvQueryFrame(cap);// Read in the video decode
if(!img)
break;
cvShowImage("img",img);
cvWaitKey(3);
}
cvReleaseCapture(&cap);
cvDestroyAllWindows();
cvReleaseImage(&img);
return 0;
}