Home > other >  Visual Studio OpenCV use Videocapture cannot catch a built-in camera
Visual Studio OpenCV use Videocapture cannot catch a built-in camera

Time:10-12

Software version:
The Window 10
Visual Studio 2015 c + +
OpenCV 4.0.0

Computer:
MSI GF65 Thin at 95 d

Good elder brother, the younger brother is learning to use OpenCV image processing technology, now want to try to use OpenCV open laptop's built-in camera, the code is as follows:

#include
#include
H # include "Windows."
#include

using namespace std;

//OpenCV includes
# include "opencv2/core HPP"
# include "opencv2/highgui. HPP"
Using the namespace CV;

//OpenCV command line parser functions provides
//Keys accecpted by the command line parser
Const char * keys=
{
"{help h the usage? | | print this message}
""{@ video | | video file, if not defined the try to use webcamera}"
};

Int main (int arg c, const char * * argv)
{
CommandLineParser parser (arg c, argv, keys);
Parser. The about (" Chapter 2. V1.0.0 ");
//If the requires help show
If (parser. From the (" help "))
{
Parser. PrintMessage ();
return 0;
}

String videoFile=parser. Get (0);

//Check if params are correctly parsed his variables in
if (! Parser. Check ())
{
Parser. PrintErrors ();
return 0;
}

Int ID=0;
VideoCapture cap;//open the default camera
If (videoFile!="")
Cap. Open (videoFile);
The else {
Cap. Open (0);
Sleep(1000);
/* while (! Cap. IsOpened () & amp; & ID<{
=1000)ID++;
Cap. Open (ID);
} */
}

if (! Cap. IsOpened ()) {//check if we succeeded
OutputDebugStringA (" \ r \ n, always find a camera!! \ r \ n \ n ");
return -1;
}
NamedWindow (" Video ", 1);
For (;; )
{
Mat frame;
Cap & gt;> Frame;//get a new frame from camera
If (frame. The empty ())
return 0;
Imshow (" Video, "frame);
If (waitKey (30) & gt;=0) break;
}
//Release the camera or video cap
Cap. Release ();

return 0;
}

Mainly in the cap. The open (0), call cap. IsOpened () has been presented 0, online to find the solution for many, has tried to cap. Open (ID) of ID constantly changes, ranging from 1 to 700 tried, the result is the same, don't know if you have the same problems or know how to solve?

  • Related