Accessing the webcam
To access a computer's webcam or camera device, you can simply call the open()
function on a cv::VideoCapture
object (OpenCV's method of accessing your camera device), and pass 0
as the default camera ID number. Some computers have multiple cameras attached, or they do not work with a default camera of 0
, so it is common practice to allow the user to pass the desired camera number as a command-line argument, in case they want to try camera 1
, 2
, or -1
, for example. We will also try to set the camera resolution to 640 x 480 using cv::VideoCapture::set()
to run faster on high-resolution cameras.
Note
Depending on your camera model, driver, or system, OpenCV might not change the properties of your camera. It is not important for this project, so don't worry if it does not work with your webcam.
You can put this code in the main()
function of your main.cpp
file:
auto cameraNumber = 0; if (argc> 1) cameraNumber = atoi(argv[1]); // Get access to the camera. cv::VideoCapture...